UNPKG

6.84 kBMarkdownView Raw
1# `@storybook/client-api` -- APIs that control the rendering of stories in the preview iframe.
2
3## Story store
4
5The story store contains the list of stories loaded in a Storybook.
6
7Each story is loaded via the `.add()` API and contains the follow attributes, which are available on the `context` (which is passed to the story's render function and decorators):
8
9- `kind` - the grouping of the story, typically corresponds to a single component. Also known as the `title` in CSF.
10- `name` - the name of the specific story.
11- `id` - an unique, URL sanitized identifier for the story, created from the `kind` and `name`.
12- `parameters` - static data about the story, see below.
13- `args` - dynamic inputs to the story, see below.
14- `hooks` - listeners that will rerun when the story changes or is unmounted, see `@storybook/addons`.
15- `viewMode` - property that tells if the story is being rendered in Canvas or Docs tab. Values are `story` for canvas and `docs` for docs.
16
17## Parameters
18
19The story parameters is a static, serializable object of data that provides details about the story. Those details can be used by addons or Storybook itself to render UI or provide defaults about the story rendering.
20
21Parameters _cannot change_ and are synchronized to the manager once when the story is loaded (note over the lifetime of a development Storybook a story can be loaded several times due to hot module reload, so the parameters technically can change for that reason).
22
23Usually addons will read from a single key of `parameters` namespaced by the name of that addon. For instance the configuration of the `backgrounds` addon is driven by the `parameters.backgrounds` namespace.
24
25Parameters are inheritable -- you can set global parameters via `addParameters` (exported by `client_api` and each framework), at the component level by the `parameters` key of the component (default) export in CSF (or in `.storiesOf`), and on a single story via the `parameters` key on the story data, or the third argument to `.add()`.
26
27Some notable parameters:
28
29- `parameters.fileName` - the file that the story was defined in, when available
30- `parameters.argsTypes` - type information about args (see below)
31
32### Parameter enhancement
33
34Ideally all parameters should be set _at build time_ of the Storybook, either directly by the user, or via the use of a webpack loader. (For an example of this, see `addon-storysource`, which writes to the `parameters.storysource` parameter with a set of static information about the story file).
35
36However, in some cases it is necessary to set parameters at _load time_ when the Storybook first loads. This should be avoided if at all possible as it is cost that must be paid each time a Storybook loads, rather than just once when the Storybook is built.
37
38To add a parameter enhancer, call `store.addArgTypesEnhancer(enhancer)` _before_ any stories are loaded (in addon registration or in `preview.js`). As each story is loaded, the enhancer will be called with the full story `context` -- the return value should be an object that will be patched into the Story's `argTypes`.
39
40There is a default enhancer that ensures that each `arg` in a story has a baseline `argType`. This value can be improved by subsequent enhancers, e.g. those provided by `@storybook/addon-docs`.
41
42## Args
43
44Args are "inputs" to stories.
45
46You can think of them equivalently to React props, Angular inputs and outputs, etc.
47
48Changing the args cause the story to be re-rendered with the new set of args.
49
50### Using args in a story
51
52By default (starting in 6.0) the args will be passed to the story as first argument and the context as second:
53
54```js
55const YourStory = ({ x, y } /*, context*/) => /* render your story using `x` and `y` */
56```
57
58If you set the `parameters.options.passArgsFirst` option on a story to false, args are passed to a story in the context, preserving the pre-6.0 story API; like parameters, they are available as `context.args`.
59
60```js
61const YourStory = ({ args: { x, y }}) => /* render your story using `x` and `y` */
62```
63
64### Arg types and values
65
66Arg types are used by the docs addon to populate the props table and are documented there. They are controlled by `parameters.argTypes` and can (sometimes) be automatically inferred from type information about the story or the component rendered by the story.
67
68A story can set initial values of its args with the `parameters.args` parameter. If you set an initial value for an arg that doesn't have a type a simple type will be inferred from the value.
69
70The initial value for an arg named "X" will be either `parameters.args.X` (if set) or `parameters.argTypes.X.defaultValue`. If an arg doesn't have a default value or an initial value, it will start unset, although it can still be set later via user interaction.
71
72For instance, for this story:
73
74```js
75export MyStory = ....
76MyStory.argTypes = {
77 primary: { defaultValue: true, /* other things */ },
78 size: { /* other things */ },
79 color: { /* other things */ },
80};
81MyStory.args = {
82 size: 'large',
83 extra: 'prop',
84};
85```
86
87Then `context.args` will default to `{ primary: true, size: 'large', extra: 'prop' }`.
88
89### Using args in an addon
90
91Args values are automatically synchronized (via the `changeStoryArgs` and `storyArgsChanged` events) between the preview and manager; APIs exist in `lib/api` to read and set args in the manager.
92
93Args need to be serializable -- so currently cannot include callbacks (this may change in a future version).
94
95Note that arg values are passed directly to a story -- you should only store the actual value that the story needs to render in the arg. If you need more complex information supporting that, use parameters or addon state.
96
97Both `@storybook/client-api` (preview) and `@storybook/api` (manager) export a `useArgs()` hook that you can use to access args in decorators or addon panels. The API is as follows:
98
99```js
100import { useArgs } from '@storybook/client-api'; // or '@storybook/api'
101
102// `args` is the args of the currently rendered story
103// `updateArgs` will update its args. You can pass a subset of the args; other args will not be changed.
104const [args, updateArgs] = useArgs();
105```
106
107## Global Args
108
109Global args are args that are "global" across all stories. They are used for things like themes and internationalization (i18n) in stories, where you want Storybook to "remember" your setting as you browse between stories.
110
111### Initial values of global args
112
113To set initial values of global args, set the `parameters.globals` parameters. Addons can use parameter enhancers (see above) to do this.
114
115### Using global args in an addon
116
117Similar to args, global args are synchronized to the manager and can be accessed via the `useGlobals` hook.
118
119```js
120import { useGlobals } from '@storybook/client-api'; // or '@storybook/api'
121
122const [globals, updateGlobals] = useGlobals();
123```