Understand Component Story Format (CSF)

Michael Chan
InstructorMichael Chan
Share this video with your friends

Social Share Links

Send Tweet
Published a year ago
Updated 2 months ago

Easily capture discrete UI states using Component Story Format (CSF). Discover this straightforward approach to writing stories that are portable, composable, and easily testable. Revolutionizing your UI development workflow.

[00:00] Now that we've covered the bulk of Storybooks UI features and how they intersect with code, let's learn how to define stories of our own. We do this using a file format called component story format or c s f for short. Let's be bold and delete all of our existing stories and start from [00:19] scratch. And while we're in here, let's change this directory from stories to components to be a little bit more real world. Create a new file titled button.stories.js. Import the button that we want to write stories for. Close our sidebar [00:39] for more space. Import button from we use a relative path here. Button.jsx. Next, we need to export a default with our component meta. The simplest version where we've imported a component is simply to define that component field using [00:59] our component button. And this is technically it. We have a valid storybook component even though there's nothing showing up in the sidebar over here. We just need to add stories. In CSF, we define stories by exporting named exports. So I'll create a story called My Story and we want to use the object [01:19] syntax here. When I save this we'll see it appear in the sidebar. We're getting this automatic category here based on the containing folder components. And when we click into our component, we see our first story. It is looking a little bit meager though. There's a button [01:39] here, but there's no text. Remember, Storybook uses args to provide UI controls in Storybook. So on this Storybook object, we wanna add an args property with the label button. Notice that we're not using a templating syntax. We're just defining the args that make up this unique story. [01:59] Storybook automatically applies these args to this button component using a behind the scenes template, giving us a number of benefits inside of Storybook while remaining as terse as possible.

egghead
egghead

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today