1---
2name: storybook
3description: Build component stories with proper args, controls, decorators, and testing patterns.
4metadata:
5 source: https://skillmd.com/skills/modbender/storybook
6 clawdbot:
7 emoji: 📖
8 requires:
9 bins:
10 - npx
11 os:
12 - linux
13 - darwin
14 - win32
15---
16
17## CSF Format (Component Story Format)
18
19- Default export is component meta—title, component, args, decorators
20- Named exports are stories—each export becomes a story in sidebar
21- `satisfies Meta<typeof Component>` for TypeScript type checking
22- CSF3 uses object syntax, not functions—`export const Primary = { args: {...} }`
23
24## Args vs ArgTypes
25
26- `args` are actual prop values passed to component—`args: { label: 'Click me' }`
27- `argTypes` configure controls UI—`argTypes: { size: { control: 'select', options: ['sm', 'lg'] } }`
28- Default args in meta apply to all stories—override in individual stories
29- `argTypes: { onClick: { action: 'clicked' } }` logs events in Actions panel
30
31## Controls
32
33- Auto-inferred from TypeScript props—boolean becomes toggle, string becomes text input
34- Override control type: `argTypes: { color: { control: 'color' } }`
35- Disable control: `argTypes: { children: { control: false } }`
36- Options for select: `control: { type: 'select' }, options: ['a', 'b', 'c']`
37
38## Decorators
39
40- Wrap stories with context—providers, layout wrappers, theme
41- Component-level in meta: `decorators: [(Story) => <Provider><Story /></Provider>]`
42- Global in `.storybook/preview.js`: applies to all stories
43- Order matters—later decorators wrap earlier ones
44
45## Play Functions
46
47- Interactive testing within story: `play: async ({ canvasElement }) => {...}`
48- Use `@storybook/testing-library` for queries—`within(canvasElement).getByRole()`
49- `await userEvent.click(button)` for interactions
50- `expect(element).toBeVisible()` for assertions—tests run in browser
51
52## Actions
53
54- `argTypes: { onClick: { action: 'clicked' } }` auto-logs to Actions panel
55- Or import: `import { action } from '@storybook/addon-actions'`
56- Use `fn()` from `@storybook/test` in Storybook 8+ for spying in play functions
57- Actions help verify event handlers without manual console.log
58
59## Story Organization
60
61- Title path creates hierarchy: `title: 'Components/Forms/Button'`
62- Stories appear in order of export—put Primary first
63- `tags: ['autodocs']` generates docs page automatically
64- `parameters: { docs: { description: { story: 'text' } } }` adds story description
65
66## Common Patterns
67
68- **Default state:** `export const Default = {}`
69- **With all props:** `export const WithIcon = { args: { icon: <Icon /> } }`
70- **Edge cases:** Empty, Loading, Error, Disabled states as separate stories
71- **Responsive:** Use viewport addon parameters per story
72
73## Render Functions
74
75- Custom render: `render: (args) => <Wrapper><Component {...args} /></Wrapper>`
76- Access context in render: `render: (args, { globals }) => ...`
77- Useful when story needs different JSX structure than default
78- Prefer decorators for wrapping, render for restructuring
79
80## Configuration
81
82- `.storybook/main.js`: addons, framework, stories glob patterns
83- `.storybook/preview.js`: global decorators, parameters, argTypes
84- Stories glob: `stories: ['../src/**/*.stories.@(js|jsx|ts|tsx)']`
85- Static assets: `staticDirs: ['../public']` for images/fonts
86
87## Common Mistakes
88
89- Forgetting to install addon AND add to main.js addons array
90- Using `storiesOf` API—deprecated, use CSF exports
91- Missing component in meta—controls won't auto-generate
92- Decorators returning `Story` without calling it: `(Story) => <Story />` not `(Story) => Story`