What is @storybook/csf?
The @storybook/csf (Component Story Format) package is a set of tools and utilities for defining component examples in Storybook. It provides a standardized format for writing stories and allows developers to take advantage of features like auto-generated documentation, controls for live editing of component props, and more.
What are @storybook/csf's main functionalities?
Defining stories
This feature allows developers to define stories for their components. A story is a single state of a component, and with CSF, stories are written as ES6 modules that export a default object with component metadata and named exports for each story.
import { Meta, Story } from '@storybook/csf';
export default {
title: 'Button',
component: Button,
} as Meta;
const Template: Story<ButtonProps> = (args) => <Button {...args} />;
export const Primary = Template.bind({});
Primary.args = {
primary: true,
label: 'Button',
};
Args
Args are the dynamic values that a story accepts. They are used to change the state of the component in the story. In the code sample, 'Secondary' is a story that uses the 'Template' but with different args to create a variation of the Button component.
export const Secondary = Template.bind({});
Secondary.args = {
label: 'Button',
};
Parameters
Parameters are Storybook's mechanism for providing static, named metadata about a story. In this example, the 'Primary' story is given a custom background parameter to change the background color in the Storybook UI.
Primary.parameters = {
backgrounds: {
values: [{ name: 'red', value: '#f00' }],
},
};
Other packages similar to @storybook/csf
react-docgen
React Docgen is a CLI and toolbox to help extract information from React components, and generate documentation from it. It's similar to @storybook/csf in that it helps document components, but it does not provide a story format or integrate directly with Storybook.
docz
Docz leverages MDX to allow developers to write documentation alongside their code. It is similar to @storybook/csf in that it provides a way to showcase components and their usage, but it uses a different approach, focusing on markdown documentation rather than a structured story format.
styleguidist
React Styleguidist is a component development environment with hot reloaded dev server and a living style guide that you can share with your team. It's similar to @storybook/csf in that it allows you to develop and document components, but it uses a different format and has its own style guide interface.