@storybook/addon-contexts
Storybook Addon Contexts
Last updated 2 months ago by shilman .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @storybook/addon-contexts 
SYNC missed versions from official npm registry.

Storybook Addon Contexts

Storybook Addon Contexts is an addon for driving your components under dynamic contexts in Storybook.

???? Why you need this?

Real world users expects your application being customizable, that is why often your components are polymorphic: they need to adapt themselves under different contextual environments. Imagine your components can speak Chinese, English, or even French, and they change their skin tone under dark or light theme. Yeah, you want to make sure a component looks great in all scenarios.

A good practice to write maintainable components is separate the presentation and its business logic. Storybook is a great place for exercising the visualization and interaction of your components, which may depend on some contexts. Often enough, you will find it become very tedious to wrap each component deeply with its contextual environments before you can really write the main story. You even start to write extra components or factory functions just to make your life easier. How about changing the context of your story dynamically?! There was simply no good way so you ended up writing stories like an accountant.

That is why you need this. An elegant way to wrap your component stories and change their contextual environment directly and dynamically in Storybook UI! Kind of like a dependency injection, eh! The best bit is you define it once then apply it everywhere.

✅ Features

  1. Define a single global file for managing contextual environments (a.k.a. containers) for all of your stories declaratively. No more repetitive setups or noisy wrapping, making your stories more focused and readable.
  2. Support dynamic contextual props switching from Storybook toolbar at runtime. You can slice into different environments (e.g. languages or themes ) to understand how your component is going to respond.
  3. Library agnostic: no presumption on what kind of components you want to wrap around your stories. You can even use it to bridge with your favorite routing, state-management solutions, or even your own React Context provider.
  4. Offer chainable and granular configurations. It is even possible to fine-tune at per story level.
  5. Visual regression friendly. You can use this addon to drive the same story under different contexts to smoke test important visual states.

???? Requirements

Make sure the version of your Storybook is above v5. For the full list of the current supported frameworks, see Addon / Framework Support Table.

???? Getting started

To get it started, add this package into your project:

yarn add -D @storybook/addon-contexts

within .storybook/main.js:

module.exports = {
  addons: ['@storybook/addon-contexts/register']
}

To load your contextual setups for your stories globally, add the following lines into preview.js file (you should see it near your addon.js file):

import { addDecorator } from '@storybook/[framework]';
import { withContexts } from '@storybook/addon-contexts/[framework]';
import { contexts } from './configs/contexts'; // we will define the contextual setups later in API section

addDecorator(withContexts(contexts));

Alternatively, like other addons, you can use this addon only for a given set of stories:

import { withContexts } from '@storybook/addon-contexts/[framework]';
import { contexts } from './configs/contexts';

export default {
  title: 'Component With Contexts',
  decorators: [withContexts(contexts)],
};

Finally, you may want to create new contextual environments or disable default setups at the story level. To create a new contextual environment at the story level:

export const defaultView = () => <div />; // sample story in CSF format
defaultView.story = {
  parameters: {
    contexts: [{ /* contextual environment defined using the API below */ }]
  }
};

To disable a default setup at the story level:

export const defaultView = () => <div />;
defaultView.story = {
  parameters: {
    contexts: [
      {
         title: '[title of contextual environment defined in contexts.js]'
         options: { disable: true }
      }
    ]
  }
};

To override the default option for a default setup at the story level, see this suggestion.

⚙️ Setups

Overview

It is recommended to have a separate file for managing your contextual environment setups. Let's add a file named contexts.js first. Before diving into API details, here is an overview on the landscape. For example (in React), to inject component theming contexts to both styled-components and material-ui theme providers in stories:

export const contexts = [
  {
    icon: 'box', // a icon displayed in the Storybook toolbar to control contextual props
    title: 'Themes', // an unique name of a contextual environment
    components: [
      // an array of components that is going to be injected to wrap stories
      /* Styled-components ThemeProvider, */
      /* Material-ui ThemeProvider, */
    ],
    params: [
      // an array of params contains a set of predefined `props` for `components`
      { name: 'Light Theme', props: { theme /* : your light theme */ } },
      { name: 'Dark Theme', props: { theme /* : your dark theme */ }, default: true },
    ],
    options: {
      deep: true, // pass the `props` deeply into all wrapping components
      disable: false, // disable this contextual environment completely
      cancelable: false, // allow this contextual environment to be opt-out optionally in toolbar
    },
  },
  /* ... */ // multiple contexts setups are supported
];

APIs

withContexts(contexts) : function

A decorating function for wrapping your stories under your predefined contexts. This means multiple contextual environments are supported. They are going to be loaded layer by layer and wrapped in a descending oder (top -> down -> story). The contexts is an array of objects that should have the following properties:


icon : string?

(default undefined)

An icon displayed in the Storybook toolbar to control contextual props. This addon allows you to define an icon for each contextual environment individually. Take a look at the currently supported icon lists from the official Storybook story. You must define an icon first if you want to take advantage of switching props dynamically in your Storybook toolbar.


title : string

(required)

A unique name of a contextual environment; if duplicate names are provided, the latter is going to be ignored.


components : (Component|string)[]

(required)

An array of components that is going to be injected to wrap stories. This means this addon allows multiple wrapping components to coexist. The wrapping sequence is from the left to right (parent -> children -> story). This nested wrapping behaviour can be useful in some cases; for instance, in the above example, we are wrapping stories under styled-components and material-ui theme providers. Also, you can use this addon to wrap any valid HTML tags.


params : object[] | undefined

(default: undefined)

An array of params contains a set of predefined props for components. This object has the following properties:

params.name : string

(required)

A unique name for representing the props.

params.props : object | null:

(required)

The props that are accepted by the wrapping component(s).

params.default : true?

(default: undefined)

Set to true if you want to use this param initially. Only the first one marked as default is identified.


options

A set of options offers more granular control over the defined contextual environment. These properties can be overridden at the story level:

options.deep : boolean?

(default: false)

Pass the props deeply into all wrapping components. Useful when you want them all to be passed with the same props.

options.disable : boolean?

(default: false)

Disable this contextual environment completely. Useful when you want to opt-out this context from a given story.

options.cancelable : boolean?

(default: false)

Allow this contextual environment to be opt-out optionally in toolbar. When set to true, an Off option will be shown at first in the toolbar menu in your Storybook.

???? Notes

  1. You can use this addon to inject any valid components, that is why icon and params can be optional.
  2. As mentioned, extra contextual environment setups can be added at the story level. Please make sure they are passed via the second argument as { contexts: [{ /* extra contexts */ }}.
  3. Additional params can be "appended" into an existing setup at the story level too (make sure it goes with the correct title); however, they are never be able to overridden the default setups. So it is important to have non-colliding names.
  4. The addon will persist the selected params (the addon state) between stories at run-time (similar to other addons). If the active params were gone after story switching, it falls back to the default then the first. As a rule of thumb, whenever collisions are possible, the first always wins.
  5. Query parameters are supported for pre-selecting contexts param, which comes in handy for visual regression testing. You can do this by appending &contexts=[name of contexts]=[name of param] in the URL under iframe mode. Use , to separate multiple contexts (e.g. &contexts=Theme=Forests,Language=Fr).

???? License

MIT

Current Tags

  • 5.3.21                                ...           latest (2 months ago)
  • 6.0.0-alpha.42                                ...           next (6 months ago)

284 Versions

  • 5.3.21                                ...           2 months ago
  • 5.3.20                                ...           2 months ago
  • 5.3.19                                ...           5 months ago
  • 6.0.0-alpha.42                                ...           6 months ago
  • 6.0.0-alpha.41                                ...           6 months ago
  • 6.0.0-alpha.40                                ...           6 months ago
  • 6.0.0-alpha.39                                ...           6 months ago
  • 6.0.0-alpha.37                                ...           6 months ago
  • 6.0.0-alpha.36                                ...           6 months ago
  • 6.0.0-alpha.35                                ...           6 months ago
  • 6.0.0-alpha.34                                ...           6 months ago
  • 6.0.0-alpha.33                                ...           6 months ago
  • 6.0.0-alpha.32                                ...           7 months ago
  • 6.0.0-alpha.31                                ...           7 months ago
  • 5.3.18                                ...           7 months ago
  • 6.0.0-alpha.30                                ...           7 months ago
  • 6.0.0-alpha.29                                ...           7 months ago
  • 6.0.0-alpha.28                                ...           7 months ago
  • 5.3.17                                ...           7 months ago
  • 5.3.15                                ...           7 months ago
  • 6.0.0-alpha.27                                ...           7 months ago
  • 6.0.0-alpha.26                                ...           8 months ago
  • 6.0.0-alpha.25                                ...           8 months ago
  • 6.0.0-alpha.24                                ...           8 months ago
  • 6.0.0-alpha.22                                ...           8 months ago
  • 6.0.0-alpha.21                                ...           8 months ago
  • 6.0.0-alpha.20                                ...           8 months ago
  • 5.3.14                                ...           8 months ago
  • 6.0.0-alpha.19                                ...           8 months ago
  • 6.0.0-alpha.18                                ...           8 months ago
  • 6.0.0-alpha.17                                ...           8 months ago
  • 6.0.0-alpha.15                                ...           8 months ago
  • 6.0.0-alpha.14                                ...           8 months ago
  • 6.0.0-alpha.13                                ...           8 months ago
  • 6.0.0-alpha.12                                ...           8 months ago
  • 6.0.0-alpha.11                                ...           9 months ago
  • 5.3.13                                ...           9 months ago
  • 6.0.0-alpha.10                                ...           9 months ago
  • 6.0.0-alpha.9                                ...           9 months ago
  • 6.0.0-alpha.8                                ...           9 months ago
  • 6.0.0-alpha.7                                ...           9 months ago
  • 6.0.0-alpha.6                                ...           9 months ago
  • 5.3.12                                ...           9 months ago
  • 5.3.11                                ...           9 months ago
  • 6.0.0-alpha.5                                ...           9 months ago
  • 6.0.0-alpha.4                                ...           9 months ago
  • 5.3.10                                ...           9 months ago
  • 6.0.0-alpha.3                                ...           9 months ago
  • 6.0.0-alpha.2                                ...           9 months ago
  • 5.3.9                                ...           9 months ago
  • 6.0.0-alpha.1                                ...           9 months ago
  • 5.3.8                                ...           9 months ago
  • 6.0.0-alpha.0                                ...           9 months ago
  • 5.3.7                                ...           9 months ago
  • 5.3.6                                ...           9 months ago
  • 5.3.5                                ...           9 months ago
  • 5.3.4                                ...           9 months ago
  • 5.3.3                                ...           9 months ago
  • 5.3.2                                ...           9 months ago
  • 5.3.1                                ...           10 months ago
  • 5.3.0                                ...           10 months ago
  • 5.3.0-rc.14                                ...           10 months ago
  • 5.3.0-rc.13                                ...           10 months ago
  • 5.3.0-rc.12                                ...           10 months ago
  • 5.3.0-rc.11                                ...           10 months ago
  • 5.3.0-rc.10                                ...           10 months ago
  • 5.3.0-rc.9                                ...           10 months ago
  • 5.3.0-rc.8                                ...           10 months ago
  • 5.3.0-rc.7                                ...           10 months ago
  • 5.3.0-rc.6                                ...           10 months ago
  • 5.3.0-rc.5                                ...           10 months ago
  • 5.3.0-rc.4                                ...           10 months ago
  • 5.3.0-rc.3                                ...           10 months ago
  • 5.3.0-rc.1                                ...           10 months ago
  • 5.3.0-rc.0                                ...           10 months ago
  • 5.3.0-beta.27                                ...           10 months ago
  • 5.3.0-beta.26                                ...           10 months ago
  • 5.3.0-beta.31                                ...           10 months ago
  • 5.3.0-beta.30                                ...           10 months ago
  • 5.3.0-beta.29                                ...           10 months ago
  • 5.3.0-beta.28                                ...           10 months ago
  • 5.3.0-beta.25                                ...           10 months ago
  • 5.3.0-beta.23                                ...           10 months ago
  • 5.3.0-beta.22                                ...           a year ago
  • 5.3.0-beta.21                                ...           a year ago
  • 5.3.0-beta.20                                ...           a year ago
  • 5.3.0-beta.19                                ...           a year ago
  • 5.3.0-beta.18                                ...           a year ago
  • 5.3.0-beta.17                                ...           a year ago
  • 5.3.0-beta.16                                ...           a year ago
  • 5.3.0-beta.15                                ...           a year ago
  • 5.2.8                                ...           a year ago
  • 5.3.0-beta.14                                ...           a year ago
  • 5.2.7                                ...           a year ago
  • 5.3.0-beta.13                                ...           a year ago
  • 5.3.0-beta.12                                ...           a year ago
  • 5.3.0-beta.11                                ...           a year ago
  • 5.3.0-beta.10                                ...           a year ago
  • 5.3.0-beta.9                                ...           a year ago
  • 5.3.0-beta.8                                ...           a year ago
  • 5.3.0-beta.6                                ...           a year ago
  • 5.3.0-beta.3                                ...           a year ago
  • 5.3.0-beta.2                                ...           a year ago
  • 5.3.0-beta.1                                ...           a year ago
  • 5.3.0-beta.0                                ...           a year ago
  • 5.3.0-alpha.47                                ...           a year ago
  • 5.3.0-alpha.46                                ...           a year ago
  • 5.3.0-alpha.45                                ...           a year ago
  • 5.3.0-alpha.44                                ...           a year ago
  • 5.3.0-alpha.43                                ...           a year ago
  • 5.2.6                                ...           a year ago
  • 5.3.0-alpha.42                                ...           a year ago
  • 5.3.0-alpha.41                                ...           a year ago
  • 5.3.0-alpha.40                                ...           a year ago
  • 5.3.0-alpha.39                                ...           a year ago
  • 5.3.0-alpha.38                                ...           a year ago
  • 5.3.0-alpha.37                                ...           a year ago
  • 5.3.0-alpha.36                                ...           a year ago
  • 5.3.0-alpha.35                                ...           a year ago
  • 5.3.0-alpha.34                                ...           a year ago
  • 5.3.0-alpha.33                                ...           a year ago
  • 5.3.0-alpha.32                                ...           a year ago
  • 5.3.0-alpha.31                                ...           a year ago
  • 5.3.0-alpha.30                                ...           a year ago
  • 5.3.0-alpha.29                                ...           a year ago
  • 5.3.0-alpha.28                                ...           a year ago
  • 5.3.0-alpha.27                                ...           a year ago
  • 5.3.0-alpha.26                                ...           a year ago
  • 5.3.0-alpha.25                                ...           a year ago
  • 5.3.0-alpha.24                                ...           a year ago
  • 5.2.5                                ...           a year ago
  • 5.3.0-alpha.23                                ...           a year ago
  • 5.3.0-alpha.22                                ...           a year ago
  • 5.3.0-alpha.21                                ...           a year ago
  • 5.3.0-alpha.20                                ...           a year ago
  • 5.3.0-alpha.19                                ...           a year ago
  • 5.2.4                                ...           a year ago
  • 5.3.0-alpha.18                                ...           a year ago
  • 5.3.0-alpha.17                                ...           a year ago
  • 5.3.0-alpha.15                                ...           a year ago
  • 5.3.0-alpha.13                                ...           a year ago
  • 5.3.0-alpha.12                                ...           a year ago
  • 5.2.3                                ...           a year ago
  • 5.2.2                                ...           a year ago
  • 5.3.0-alpha.11                                ...           a year ago
  • 5.3.0-alpha.10                                ...           a year ago
  • 5.3.0-alpha.9                                ...           a year ago
  • 5.3.0-alpha.8                                ...           a year ago
  • 5.3.0-alpha.7                                ...           a year ago
  • 5.3.0-alpha.6                                ...           a year ago
  • 5.3.0-alpha.5                                ...           a year ago
  • 5.3.0-alpha.4                                ...           a year ago
  • 5.3.0-alpha.3                                ...           a year ago
  • 5.3.0-alpha.2                                ...           a year ago
  • 5.3.0-alpha.1                                ...           a year ago
  • 5.2.1                                ...           a year ago
  • 5.3.0-alpha.0                                ...           a year ago
  • 5.2.0                                ...           a year ago
  • 5.2.0-rc.11                                ...           a year ago
  • 5.2.0-rc.10                                ...           a year ago
  • 5.2.0-rc.9                                ...           a year ago
  • 5.2.0-rc.8                                ...           a year ago
  • 5.2.0-rc.6                                ...           a year ago
  • 5.2.0-rc.5                                ...           a year ago
  • 5.2.0-rc.4                                ...           a year ago
  • 5.2.0-rc.2                                ...           a year ago
  • 5.2.0-rc.1                                ...           a year ago
  • 5.2.0-rc.0                                ...           a year ago
  • 5.2.0-beta.48                                ...           a year ago
  • 5.2.0-beta.47                                ...           a year ago
  • 5.2.0-beta.46                                ...           a year ago
  • 5.2.0-beta.45                                ...           a year ago
  • 5.2.0-beta.43                                ...           a year ago
  • 5.2.0-beta.42                                ...           a year ago
  • 5.2.0-beta.41                                ...           a year ago
  • 5.2.0-beta.40                                ...           a year ago
  • 5.2.0-beta.39                                ...           a year ago
  • 5.2.0-beta.38                                ...           a year ago
  • 5.2.0-beta.37                                ...           a year ago
  • 5.2.0-beta.36                                ...           a year ago
  • 5.2.0-beta.33                                ...           a year ago
  • 5.2.0-beta.32                                ...           a year ago
  • 5.2.0-beta.31                                ...           a year ago
  • 5.2.0-beta.30                                ...           a year ago
  • 5.1.11                                ...           a year ago
  • 5.2.0-beta.29                                ...           a year ago
  • 5.2.0-beta.28                                ...           a year ago
  • 5.2.0-beta.27                                ...           a year ago
  • 5.2.0-beta.26                                ...           a year ago
  • 5.2.0-beta.25                                ...           a year ago
  • 5.2.0-beta.24                                ...           a year ago
  • 5.2.0-beta.23                                ...           a year ago
  • 5.2.0-beta.22                                ...           a year ago
  • 5.2.0-beta.21                                ...           a year ago
  • 5.2.0-beta.20                                ...           a year ago
  • 5.2.0-beta.19                                ...           a year ago
  • 5.1.10                                ...           a year ago
  • 5.2.0-beta.18                                ...           a year ago
  • 5.2.0-beta.17                                ...           a year ago
  • 5.2.0-beta.16                                ...           a year ago
  • 5.2.0-beta.15                                ...           a year ago
  • 5.2.0-beta.14                                ...           a year ago
  • 5.2.0-beta.13                                ...           a year ago
  • 5.2.0-beta.12                                ...           a year ago
  • 5.2.0-beta.10                                ...           a year ago
  • 5.2.0-beta.9                                ...           a year ago
  • 5.2.0-beta.8                                ...           a year ago
  • 5.2.0-beta.7                                ...           a year ago
  • 5.2.0-beta.6                                ...           a year ago
  • 5.2.0-beta.5                                ...           a year ago
  • 5.2.0-beta.4                                ...           a year ago
  • 5.2.0-beta.3                                ...           a year ago
  • 5.2.0-beta.2                                ...           a year ago
  • 5.2.0-beta.1                                ...           a year ago
  • 5.2.0-beta.0                                ...           a year ago
  • 5.2.0-alpha.44                                ...           a year ago
  • 5.2.0-alpha.43                                ...           a year ago
  • 5.2.0-alpha.42                                ...           a year ago
  • 5.2.0-alpha.41                                ...           a year ago
  • 5.2.0-alpha.40                                ...           a year ago
  • 5.2.0-alpha.39                                ...           a year ago
  • 5.2.0-alpha.38                                ...           a year ago
  • 5.2.0-alpha.37                                ...           a year ago
  • 5.2.0-alpha.36                                ...           a year ago
  • 5.2.0-alpha.35                                ...           a year ago
  • 5.2.0-alpha.34                                ...           a year ago
  • 5.2.0-alpha.33                                ...           a year ago
  • 5.2.0-alpha.32                                ...           a year ago
  • 5.2.0-alpha.31                                ...           a year ago
  • 5.2.0-alpha.30                                ...           a year ago
  • 5.1.9                                ...           a year ago
  • 5.2.0-alpha.29                                ...           a year ago
  • 5.2.0-alpha.28                                ...           a year ago
  • 5.2.0-alpha.27                                ...           a year ago
  • 5.1.8                                ...           a year ago
  • 5.2.0-alpha.26                                ...           a year ago
  • 5.1.7                                ...           a year ago
  • 5.1.6                                ...           a year ago
  • 5.1.5                                ...           a year ago
  • 5.1.4                                ...           a year ago
  • 5.2.0-alpha.24                                ...           a year ago
  • 5.2.0-alpha.23                                ...           a year ago
  • 5.2.0-alpha.22                                ...           a year ago
  • 5.1.3                                ...           a year ago
  • 5.1.1                                ...           a year ago
  • 5.1.0-rc.5                                ...           a year ago
  • 5.1.0-rc.4                                ...           a year ago
  • 5.2.0-alpha.21                                ...           a year ago
  • 5.2.0-alpha.20                                ...           a year ago
  • 5.1.0-rc.3                                ...           a year ago
  • 5.2.0-alpha.19                                ...           a year ago
  • 5.1.0-rc.2                                ...           a year ago
  • 5.2.0-alpha.18                                ...           a year ago
  • 5.2.0-alpha.17                                ...           a year ago
  • 5.2.0-alpha.16                                ...           a year ago
  • 5.2.0-alpha.15                                ...           a year ago
  • 5.2.0-alpha.14                                ...           a year ago
  • 5.1.0-rc.1                                ...           a year ago
  • 5.2.0-alpha.13                                ...           a year ago
  • 5.2.0-alpha.12                                ...           a year ago
  • 5.2.0-alpha.11                                ...           a year ago
  • 5.1.0-rc.0                                ...           a year ago
  • 5.2.0-alpha.10                                ...           a year ago
  • 5.2.0-alpha.9                                ...           a year ago
  • 5.1.0-beta.1                                ...           a year ago
  • 5.2.0-alpha.8                                ...           a year ago
  • 5.2.0-alpha.7                                ...           a year ago
  • 5.2.0-alpha.6                                ...           a year ago
  • 5.2.0-alpha.5                                ...           a year ago
  • 5.2.0-alpha.4                                ...           a year ago
  • 5.1.0-beta.0                                ...           a year ago
  • 5.1.0-alpha.40                                ...           a year ago
  • 5.1.0-alpha.39                                ...           a year ago
  • 5.1.0-alpha.37                                ...           a year ago
  • 5.2.0-alpha.3                                ...           a year ago
  • 5.2.0-alpha.2                                ...           a year ago
  • 5.2.0-alpha.1                                ...           a year ago
  • 5.2.0-alpha.0                                ...           a year ago
  • 5.1.0-alpha.36                                ...           2 years ago
  • 5.1.0-alpha.35                                ...           2 years ago
  • 5.1.0-alpha.34                                ...           2 years ago
  • 5.1.0-alpha.33                                ...           2 years ago
  • 5.1.0-alpha.32                                ...           2 years ago
  • 5.1.0-alpha.31                                ...           2 years ago

Copyright 2014 - 2016 © taobao.org |