Adding Storybook Stories - Quick Guide โ
CSF 3.0 Format (Recommended) โ
Use this format for stories with interactive controls:
jsx
import React from 'react'
import '../tailwind.css'
import '../styles.css'
import YourComponent from './YourComponent'
export default {
title: 'Components/YourComponent',
component: YourComponent,
tags: ['autodocs'],
argTypes: {
// Define controls for each prop
propName: {
control: 'text', // or 'select', 'boolean', 'number', etc.
description: 'Description of this prop'
},
variant: {
control: 'select',
options: ['option1', 'option2'],
description: 'Choose a variant'
},
size: {
control: { type: 'range', min: 12, max: 64, step: 4 },
description: 'Size control'
}
}
}
// Each story as an object with args
export const Default = {
args: {
propName: 'Default value',
variant: 'option1'
}
}
export const Variant2 = {
args: {
propName: 'Another value',
variant: 'option2'
}
}Control Types โ
text- Text inputboolean- Checkboxnumber- Number inputselect- Dropdown with optionsradio- Radio buttonsrange- Slider with min/maxcolor- Color pickerdate- Date pickerobject- JSON editorarray- Array editor
Example: Button Component โ
jsx
export default {
title: 'Components/Button',
component: Button,
tags: ['autodocs'],
argTypes: {
children: {
control: 'text',
description: 'Button text'
},
variant: {
control: 'select',
options: ['primary', 'secondary', 'ghost'],
description: 'Visual style variant'
},
disabled: {
control: 'boolean',
description: 'Disabled state'
},
onClick: {
action: 'clicked', // Shows in Actions panel
description: 'Click handler'
}
}
}
export const Primary = {
args: {
variant: 'primary',
children: 'Click me'
}
}Legacy CSF 2.0 Format (Function Stories) โ
This format works but won't have controls:
jsx
export const Example = () => <Component prop="value" />Convert to CSF 3.0 for interactive controls!
"Open in Editor" Feature โ
The "Open in Editor" feature in Storybook requires:
- Running Storybook in VSCode (not just browser)
- Using the VSCode Simple Browser or Preview
- Proper VSCode integration
To use:
- Start Storybook:
npm run storybook - In VSCode, press
Cmd+Shift+P(Mac) orCtrl+Shift+P(Windows/Linux) - Type "Simple Browser: Show" and enter
http://localhost:6006 - Now "Open in Editor" should work within VSCode
Common Issues โ
No Controls Panel โ
- Make sure your story uses CSF 3.0 format (objects with
args) - Check that
argTypesare defined - Verify component is specified in
export default
"Open in Editor" Not Working โ
- Use VSCode Simple Browser instead of external browser
- Ensure VSCode has file permissions
- Check that story files are in workspace
See Also โ
- Button.stories.jsx - Example with controls
- Icon.stories.jsx - Multiple stories with controls
- Storybook CSF Documentation