docs/writing-stories/stories-for-multiple-components.mdx
It's useful to write stories that render two or more components at once if those components are designed to work together. For example, ButtonGroup, List, and Page components.
When the components you're documenting have a parent-child relationship, you can use the subcomponents property to document them together. This is especially useful when the child component is not meant to be used on its own, but only as part of the parent component.
Here's an example with List and ListItem components:
Note that by adding a subcomponents property to the meta (or default export), we get an extra panel on the ArgTypes and Controls tables, listing the props of ListItem:
Subcomponents are only intended for documentation purposes and have some limitations:
Let's talk about some techniques you can use to mitigate the above, which are especially useful in more complicated situations.
We can also reduce repetition in our stories by reusing story definitions. Here, we can reuse the ListItem stories' args in the story for List:
By rendering the Unchecked story with its args, we are able to reuse the input data from the ListItem stories in the List.
One way we improve that situation is by pulling the rendered subcomponent out into a children arg:
Now that children is an arg, we can potentially reuse it in another story.
However, there are some caveats when using this approach that you should be aware of.
The children arg, just like all args, needs to be JSON serializable. To avoid errors with your Storybook, you should:
Another option that is more “data”-based is to create a special “story-generating” template component:
<CodeSnippets path="list-story-template.md" />This approach is a little more complex to setup, but it means you can more easily reuse the args to each story in a composite component. It also means you can alter the args to the component with the Controls panel.