Skip to content

Instantly share code, notes, and snippets.

@shilman
Last active May 22, 2022 06:49
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shilman/69c1dd41a466bae137cc88bd2c6ef487 to your computer and use it in GitHub Desktop.
Save shilman/69c1dd41a466bae137cc88bd2c6ef487 to your computer and use it in GitHub Desktop.
Storybook Controls Walkthrough

Storybook Controls w/ CRA & TypeScript

This is a quick-and-dirty walkthrough to set up a fresh project with Storybook Controls. It's also an introduction to Storybook Args, which is a major update to Storybook's Component Story Format (CSF): a more portable and ergonomic way to write stories.

This walkthrough gives you:

  • Auto-generated controls in the addons panel
  • Auto-generated controls in your Docs
  • Auto-generated actions for event logging
  • An introduction to the future of CSF

The purpose of this walkthrough is to get people started with Args/Controls. There are countless permutations and variables which can influence the setup, and we plan to support all common use cases by the time the feature is released. Args is currently in RC and it's pretty stable. We need testing and feedback to get it into final release state!

This document is based on the popular Storybook Docs w/ CRA & TypeScript walkthrough. If you want a full tutorial on addon-docs, please see Design Systems for Developers, which goes into much more depth but is based on JS and was written before Args existed.

What you'll make

This is a simple Storybook that has automatically-generated actions for component event handlers and interactive controls in the component's prop table:

Step 1: Initialize CRA w/ TS

yarn create react-app cra-controls --template typescript

Step 2: Initialize Storybook w/ TS

cd cra-controls
npx sb@next init --story-format=csf-ts

Step 3: Verify Installation

yarn storybook

Then edit the button label in ./src/stories/1-Button.stories.tsx and verify that the story updates in Storybook.

Verify that the addons panel is working at the bottom of the screen:

  • That clicking the button logs an action in the Actions tab.
  • That there's a Controls tab in the addons panel, and that editing the button label updates story.

Also verify that there's a Docs tab, and that it shows something reasonable-ish. We'll make it better in the next step.

Step 4: Test MDX

Finally, test that MDX is working. You already registered *.stories.mdx in Step 3, so just create ./src/stories/Test.stories.mdx:

import { Meta, Story, Preview, Props } from "@storybook/addon-docs/blocks";
import { Button } from "./Button";

<Meta title="Test" component={Button} />

# Testing Button

Here's some _markdown_!

export const Template = (args) => <Button {...args} />;

<Preview>
  <Story name="button" args={{ children: "hiya" }}>
    {Template.bind({})}
  </Story>
</Preview>

# Props

<Props story="button" />

You should see a new entry in your Storybook's navigation, a story, and the documentation you wrote in the Docs tab. The Props block still supports the previous 5.x configuration, e.g. <Props of={Button} />. The difference is that the story version generates controls for the given story.

Step 9: More info & Feedback

This document gives a basic setup to get you started. For more information on:

If you run into any problems or have any suggestions, please file an issue in the Storybook Github Repo. We're doing our best to provide full support for JS/TS and all the frameworks that Storybook supports. Thank you for your help improving Storybook Args!

@jpmarra
Copy link

jpmarra commented Jun 23, 2020

This is rad! When going through step 8, I ran into this issue storybookjs/storybook#9996 and can't figure out how to get past it 😔

@sethburtonhall
Copy link

@jpmarra Same.

@ButuzGOL
Copy link

all works fine

@akhilesh85
Copy link

I did try the steps for Angular. After the installation, I encountered some bugs and documentation gaps for angular. I created an Issue with all the listed bugs and approaches that I took. storybookjs/storybook#11452

@chrislopresto
Copy link

I love that that this gist jumps from step 4 straight to step 9 now. 😅

@shilman
Copy link
Author

shilman commented Jul 10, 2020

😅

@nad182
Copy link

nad182 commented Jul 11, 2020

Followed this walkthrough, but this

  • That clicking the button logs an action in the Actions tab.

is not happening for me.

@akhilesh85
Copy link

Hi @shilman

In the addon controls docummentation there is a mention of options for enum in the control annotation section.

As per the documentation, the examples showcased for options were only the Array Datatype. What all other dataTypes does options support? If I want a particular option in the options collection to be selected by default, can we configure in its definition?

Please let me know. Thanks.

Regards,
Akhilesh

@zhaoyao91
Copy link

how to use sb6 withought cra?

@SajPrad
Copy link

SajPrad commented Sep 15, 2020

Is there a way to use a function as a control? Or args?

@shahnshahmalik
Copy link

I'm trying to follow the above instructions but getting an error:
Unexpected value 'undefined' declared by the module 'DynamicModule'
, I created an issue in order to get help.
storybookjs/storybook#13833

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment