chan.dev/ lessons/ storybook

Understand Component Story Format (CSF)

[ensure that addon panel is disabled. we want to showcase it in the next lesson.]

Now that we’ve covered the bulk of Storybook’s UI (and where it intersets with code), Let’s talk about defining stories of our own.

This is done with Component Story Format.

Let’s be bold and delete all of the sample stories. And while we’re at it change the directory to components (as it would likely appear in a real project).

src/components/Button.stories.js
import {Button} from './Button.jsx'
export default {
component: Button,
}

To add our first story,

src/components/Button.stories.js
import {Button} from './Button.jsx'
export default {
component: Button,
}
export const MyStory = {}

It’s a silly Story tho. Let’s flesh this meager button out with some args.

Remember that Storybook uses args to provide UI Controls in the Addons Panel. Let’s define the first label:

src/components/Button.stories.js
import {Button} from './Button.jsx'
export default {
component: Button,
}
export const MyStory = {
args: {
label: 'My Button',
},
}

Notice that we aren’t using a templating language to render our stories. CSF object syntax automatically spreads args onto components for use. This creates an incredibly terse syntax where only the difference between stories are showcased.