title | sidebar | ||||
---|---|---|---|---|---|
How to write stories |
|
A story captures the rendered state of a UI component. It's an object with annotations that describe the component's behavior and appearance given a set of arguments.
Storybook uses the generic term arguments (args for short) when talking about React’s props
, Vue’s props
, Angular’s @Input
, and other similar concepts.
A component’s stories are defined in a story file that lives alongside the component file. The story file is for development-only, and it won't be included in your production bundle. In your filesytem, it looks something like this:
components/
├─ Button/
│ ├─ Button.js | ts | jsx | tsx | vue | svelte
│ ├─ Button.stories.js | ts | jsx | tsx
We define stories according to the Component Story Format (CSF), an ES6 module-based standard that is easy to write and portable between tools.
The key ingredients are the default export that describes the component, and named exports that describe the stories.
The default export metadata controls how Storybook lists your stories and provides information used by addons. For example, here’s the default export for a story file Button.stories.js|ts
:
{/* prettier-ignore-start */}
{/* prettier-ignore-end */}
Starting with Storybook version 7.0, story titles are analyzed statically as part of the build process. The default export must contain a title
property that can be read statically or a component
property from which an automatic title can be computed. Using the id
property to customize your story URL must also be statically readable.
Use the named exports of a CSF file to define your component’s stories. We recommend you use UpperCamelCase for your story exports. Here’s how to render Button
in the “primary” state and export a story called Primary
.
{/* prettier-ignore-start */}
{/* prettier-ignore-end */}
#### Working with React HooksReact Hooks are convenient helper methods to create components using a more streamlined approach. You can use them while creating your component's stories if you need them, although you should treat them as an advanced use case. We recommend args as much as possible when writing your own stories. As an example, here’s a story that uses React Hooks to change the button's state:
{/* prettier-ignore-start */}
{/* prettier-ignore-end */}
#### Working with Solid SignalsSolid Signals are convenient helper methods to create components using a more streamlined approach. You can use them while creating your component's stories if you need them, although you should treat them as an advanced use case. We recommend args as much as possible when writing your own stories. As an example, here’s a story that uses Solid Signals to change the button's state:
{/* prettier-ignore-start */}
{/* prettier-ignore-end */}
You can rename any particular story you need. For instance, to give it a more accurate name. Here's how you can change the name of the Primary
story:
{/* prettier-ignore-start */}
{/* prettier-ignore-end */}
Your story will now be shown in the sidebar with the given text.
{/* Maintaining a prior heading */}
A story is an object that describes how to render a component. You can have multiple stories per component, and those stories can build upon one another. For example, we can add Secondary and Tertiary stories based on our Primary story from above.
{/* prettier-ignore-start */}
{/* prettier-ignore-end */}
What’s more, you can import args
to reuse when writing stories for other components, and it's helpful when you’re building composite components. For example, if we make a ButtonGroup
story, we might remix two stories from its child component Button
.
{/* prettier-ignore-start */}
{/* prettier-ignore-end */}
When Button’s signature changes, you only need to change Button’s stories to reflect the new schema, and ButtonGroup’s stories will automatically be updated. This pattern allows you to reuse your data definitions across the component hierarchy, making your stories more maintainable.
That’s not all! Each of the args from the story function are live editable using Storybook’s Controls panel. It means your team can dynamically change components in Storybook to stress test and find edge cases.