npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sambego/storybook-state

v2.0.1

Published

Manage state in storybook stories

Downloads

37,098

Readme

Storybook state

Warning

Storybook recently introduced a new way of building addons, the latest version of storybook-state will work with version of storybook > v5.2.*. For older versions of storybook use v1.3.6 of this addon.


Getting Started

npm install --save-dev @sambego/storybook-state

First you will need to create a new store, to save the state and handle updates. You can add all properties which your component expects, and the State component will propagate them to your component. Once you've created the store, you can wrap your components in a State component and pass along the store.

In the example below we create a modal which will expect an active property. When clicking on the button we will update the store, which in turn will update the property active on the modal;

Display and update using a State component

import React from "react";
import { storiesOf } from "@storybook/react";
import { State, Store } from "@sambego/storybook-state";

const store = new Store({
  active: false
});

const SimpleModal = props => (
  <div>
    <State store={store}>
      <Modal>Modal content</Modal>
    </State>
    <Button onClick={() => store.set({ active: !store.get("active") })} />
  </div>
);

export default { title: "Modal" };
export const Simple = () => SimpleModal;

Display and update using a State decorator

import React from "react";
import { addDecorator, addParameters } from "@storybook/react";
import { Store, withState } from "@sambego/storybook-state";

const SimpleCounter = props => {
  return [
    <p> Count: {props.count} </p>,
    <button onClick={props.handleCountUpdate}> {props.count} </button>
  ];
};

const store = new Store({
  count: 0,
  handleCountUpdate: () => store.set({ count: store.get("count") + 1 })
});

addDecorator(withState());
addParameters({
  state: {
    store
  }
});

export default { title: "Counter" };
export const Simple = () => SimpleCounter;

Store

The store has a few methods you can use to get and update the state.

When creating a new instance, you can pass along the initial state.

const store = new Store({
  foo: "bar",
  active: true,
  items: ["item 1", "item 2"]
});

You can retrieve a state from the store by using the get() method.

store.get("foo"); // will return 'bar'
store.get("active"); // will return true
store.get("items"); // will return ['item 1', 'item 2']

You can update the store by using the set() method.

store.set({
  active: false,
  bar: "foo"
});

You can subscribe to changes in the store by using the subscribe() method. You can register a callback, which will have the updated state as the first parameter whenever the state updates.

store.subscribe(state => // Do something with the updated state.

State component

The state component accepts one property, an instance of Store. All properties that depend on the state, or should update on state changes, should be added in the Store, and will be propagated to your component by the <State /> component.

<State store={store}>
  <StateDependendComponent />
</State>

The state also allows a function as a child so you can pass the state to any prop of your components.

const store = new Store({
  active: true
});

<State store={store}>
  {state => [
    <ElementOne active={state.active} />,
    <ElementTwo checked={state.active} />
  ]}
</State>;

You can also manipulate the state before passing it to the children via the parseState property.

<State store={store} parseState={state => ({ ...state, id: `#${state.uuid}` })}>
  <StateDependendComponent />
</State>

When using the withState decorator, you can pass along the state parser function as a parameter.

addDecorator(withState());
addParameters({
  state: {
    store,
    parseState: state => ({ ...state, count: `foo-${state.count}` })
  }
});