1---2name: storybook3description: Build component stories with proper args, controls, decorators, and testing patterns.4---56## CSF Format (Component Story Format)78- Default export is component meta—title, component, args, decorators9- Named exports are stories—each export becomes a story in sidebar10- `satisfies Meta<typeof Component>` for TypeScript type checking11- CSF3 uses object syntax, not functions—`export const Primary = { args: {...} }`1213## Args vs ArgTypes1415- `args` are actual prop values passed to component—`args: { label: 'Click me' }`16- `argTypes` configure controls UI—`argTypes: { size: { control: 'select', options: ['sm', 'lg'] } }`17- Default args in meta apply to all stories—override in individual stories18- `argTypes: { onClick: { action: 'clicked' } }` logs events in Actions panel1920## Controls2122- Auto-inferred from TypeScript props—boolean becomes toggle, string becomes text input23- Override control type: `argTypes: { color: { control: 'color' } }`24- Disable control: `argTypes: { children: { control: false } }`25- Options for select: `control: { type: 'select' }, options: ['a', 'b', 'c']`2627## Decorators2829- Wrap stories with context—providers, layout wrappers, theme30- Component-level in meta: `decorators: [(Story) => <Provider><Story /></Provider>]`31- Global in `.storybook/preview.js`: applies to all stories32- Order matters—later decorators wrap earlier ones3334## Play Functions3536- Interactive testing within story: `play: async ({ canvasElement }) => {...}`37- Use `@storybook/testing-library` for queries—`within(canvasElement).getByRole()`38- `await userEvent.click(button)` for interactions39- `expect(element).toBeVisible()` for assertions—tests run in browser4041## Actions4243- `argTypes: { onClick: { action: 'clicked' } }` auto-logs to Actions panel44- Or import: `import { action } from '@storybook/addon-actions'`45- Use `fn()` from `@storybook/test` in Storybook 8+ for spying in play functions46- Actions help verify event handlers without manual console.log4748## Story Organization4950- Title path creates hierarchy: `title: 'Components/Forms/Button'`51- Stories appear in order of export—put Primary first52- `tags: ['autodocs']` generates docs page automatically53- `parameters: { docs: { description: { story: 'text' } } }` adds story description5455## Common Patterns5657- **Default state:** `export const Default = {}`58- **With all props:** `export const WithIcon = { args: { icon: <Icon /> } }`59- **Edge cases:** Empty, Loading, Error, Disabled states as separate stories60- **Responsive:** Use viewport addon parameters per story6162## Render Functions6364- Custom render: `render: (args) => <Wrapper><Component {...args} /></Wrapper>`65- Access context in render: `render: (args, { globals }) => ...`66- Useful when story needs different JSX structure than default67- Prefer decorators for wrapping, render for restructuring6869## Configuration7071- `.storybook/main.js`: addons, framework, stories glob patterns72- `.storybook/preview.js`: global decorators, parameters, argTypes73- Stories glob: `stories: ['../src/**/*.stories.@(js|jsx|ts|tsx)']`74- Static assets: `staticDirs: ['../public']` for images/fonts7576## Common Mistakes7778- Forgetting to install addon AND add to main.js addons array79- Using `storiesOf` API—deprecated, use CSF exports80- Missing component in meta—controls won't auto-generate81- Decorators returning `Story` without calling it: `(Story) => <Story />` not `(Story) => Story`