|
| 1 | +--- |
| 2 | +globs: *.stories.tsx,*.docs.mdx |
| 3 | +alwaysApply: false |
| 4 | +--- |
| 5 | +## Imports |
| 6 | + |
| 7 | +### Stories Files (.stories.tsx) |
| 8 | +- Import types: `import type { Meta, StoryObj } from '@storybook/react-vite';` |
| 9 | +- Import `StoryFn` for custom template functions |
| 10 | +- For interactive tests: `import { within, userEvent } from '@testing-library/react';` or `import { userEvent, within } from 'storybook/test';` |
| 11 | + |
| 12 | +### Documentation Files (.docs.mdx) |
| 13 | +- `import { Meta, Canvas, Story, Controls } from '@storybook/addon-docs/blocks';` |
| 14 | + - `Meta` - Define meta information with `<Meta of={StoriesImport} />` |
| 15 | + - `Canvas` - Display story with code panel |
| 16 | + - `Story` - Reference specific story with `<Story of={StoriesImport.StoryName} />` |
| 17 | + - `Controls` - Display interactive controls table with `<Controls of={StoriesImport.StoryName} />` |
| 18 | + - `ArgTypes` - Display argument types documentation |
| 19 | + - `Source` - Show code examples |
| 20 | +- Import stories: `import * as ComponentStories from './Component.stories';` |
| 21 | + |
| 22 | +## Meta Configuration |
| 23 | + |
| 24 | +Use `satisfies Meta<typeof Component>` or `as Meta<typeof Component>`: |
| 25 | + |
| 26 | +```tsx |
| 27 | +const meta = { |
| 28 | + title: 'Category/ComponentName', |
| 29 | + component: ComponentName, |
| 30 | + subcomponents: { Item: Component.Item }, // For compound components |
| 31 | + args: { /* common default args */ }, |
| 32 | + parameters: { controls: { exclude: baseProps } }, // Exclude base design system props |
| 33 | + argTypes: { /* ... */ } |
| 34 | +} satisfies Meta<typeof Component>; |
| 35 | + |
| 36 | +export default meta; |
| 37 | +``` |
| 38 | + |
| 39 | +## ArgTypes Structure |
| 40 | + |
| 41 | +Group by categories with comments: |
| 42 | +- `/* Content */` - children, labels, placeholders, icons |
| 43 | +- `/* Selection */` - selectedKey, defaultSelectedKey |
| 44 | +- `/* Behavior */` - filter, trigger modes, loading states |
| 45 | +- `/* Presentation */` - type, theme, size, direction |
| 46 | +- `/* State */` - isDisabled, isRequired, isReadOnly, validationState, autoFocus |
| 47 | +- `/* Events */` - onPress, onChange, onSelectionChange, onBlur, onFocus |
| 48 | + |
| 49 | +### ArgType Format |
| 50 | + |
| 51 | +```tsx |
| 52 | +propName: { |
| 53 | + control: { type: 'radio' | 'boolean' | 'text' | 'number' | null }, |
| 54 | + options: ['option1', 'option2'], // For radio/select |
| 55 | + description: 'Clear description', |
| 56 | + table: { |
| 57 | + defaultValue: { summary: 'value' }, |
| 58 | + type: { summary: 'string' } |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +- Use `control: { type: null }` to disable controls (for functions, complex types) |
| 64 | +- Use `action: 'event-name'` for event handlers |
| 65 | +- Use `action: (e) => ({ type: 'event', data })` for custom action logging |
| 66 | + |
| 67 | +## Stories |
| 68 | + |
| 69 | +### Named Exports (Preferred) |
| 70 | +```tsx |
| 71 | +export const StoryName = (args) => <Component {...args} />; |
| 72 | +``` |
| 73 | + |
| 74 | +### Story Objects with CSF3 |
| 75 | +```tsx |
| 76 | +export const StoryName: StoryObj<typeof Component> = { |
| 77 | + render: (args) => <Component {...args} />, |
| 78 | + args: { /* story-specific args */ }, |
| 79 | + play: async ({ canvasElement }) => { |
| 80 | + // Interactive test |
| 81 | + } |
| 82 | +}; |
| 83 | +``` |
| 84 | + |
| 85 | +### Templates (Legacy Pattern) |
| 86 | +```tsx |
| 87 | +const Template: StoryFn<ComponentProps> = (args) => <Component {...args} />; |
| 88 | + |
| 89 | +export const Story = Template.bind({}); |
| 90 | +Story.args = { /* ... */ }; |
| 91 | +``` |
| 92 | + |
| 93 | +## Testing with Play Functions |
| 94 | + |
| 95 | +```tsx |
| 96 | +export const Interactive: StoryObj = { |
| 97 | + render: () => { /* ... */ }, |
| 98 | + play: async ({ canvasElement }) => { |
| 99 | + const canvas = within(canvasElement); |
| 100 | + const element = canvas.getByRole('button'); |
| 101 | + await userEvent.click(element); |
| 102 | + } |
| 103 | +}; |
| 104 | +``` |
| 105 | + |
| 106 | +## MDX Documentation Structure |
| 107 | + |
| 108 | +```mdx |
| 109 | +import { Meta, Canvas, Story, Controls } from '@storybook/addon-docs/blocks'; |
| 110 | +import * as ComponentStories from './Component.stories'; |
| 111 | + |
| 112 | +<Meta of={ComponentStories} /> |
| 113 | + |
| 114 | +# ComponentName |
| 115 | + |
| 116 | +Component description |
| 117 | + |
| 118 | +## When to Use |
| 119 | +- Use case 1 |
| 120 | +- Use case 2 |
| 121 | + |
| 122 | +## Component |
| 123 | + |
| 124 | +<Story of={ComponentStories.Default} /> |
| 125 | + |
| 126 | +--- |
| 127 | + |
| 128 | +### Properties |
| 129 | + |
| 130 | +<Controls of={ComponentStories.Default} /> |
| 131 | + |
| 132 | +### Base Properties |
| 133 | + |
| 134 | +Reference base properties support here. |
| 135 | + |
| 136 | +## Examples |
| 137 | + |
| 138 | +### Example Section |
| 139 | + |
| 140 | +<Story of={ComponentStories.ExampleStory} /> |
| 141 | +``` |
| 142 | + |
| 143 | +## Common Patterns |
| 144 | + |
| 145 | +- Use `baseProps` exclusion for design system props |
| 146 | +- Define default `width` in meta `args` for form components |
| 147 | +- Create size/state matrix stories to show all variants |
| 148 | +- Use `Space` component for layout in template functions |
| 149 | +- Export type: `type Story = StoryObj<typeof meta>;` |
0 commit comments