• Stars
    star
    152
  • Rank 244,685 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 7 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Manage state inside storybook stories

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}` })
  }
});

More Repositories

1

audio-effects

A javascript library to create audio effects using the web-audio-api
TypeScript
122
star
2

oscilloscope.js

A small javascript plugin to create an oscilloscope of an audio-context
JavaScript
33
star
3

deep-dive

A deep dive into the web-audio API
JavaScript
26
star
4

storybook-styles

Add custom styles to the storybook preview panel
JavaScript
24
star
5

diorama

A set of React.js components to create easy en extendable presentations.
JavaScript
20
star
6

pedalboard

A react client on top of https://github.com/Sambego/audio-effects
JavaScript
17
star
7

annoying-recruiters

A gmail filter to send recruiter mails directly to the trash bin
8
star
8

rsa-demo

A simple demonstration of how RSA encryption works, this is not a perfect/complete implementation
JavaScript
7
star
9

frequency-calculator

Calculate the frequency of a music note
JavaScript
5
star
10

nestjs-auth0-api

The source code for https://www.youtube.com/watch?v=JzndSOAb6SQ
TypeScript
5
star
11

atom-synth

Create your personal soundtrack as you type.
JavaScript
4
star
12

js-not-java

Dear recruiters, JavaScript is not Java
HTML
3
star
13

meow

😸😸😸😸
JavaScript
1
star
14

rsa-playground

The RSA algorithm explained, visually.
TypeScript
1
star
15

synthesizer

Web-based synthesizer built with JavaScript and the web-audio-api
JavaScript
1
star
16

cypress-auth0-spa

JavaScript
1
star
17

1990

Passwords are so 1990
JavaScript
1
star
18

fga-demo-api

A small API, demonstrating Auth0 FGA
Go
1
star
19

pedalboard-presentation

A presentation about the web-audio-api and web-midi-api
JavaScript
1
star