--- name: storybook-component-documentation user-invocable: false description: Use when creating or improving component documentation in Storybook. Helps generate comprehensive docs using MDX, autodocs, and JSDoc comments. allowed-tools: - Read - Write - Edit - Bash - Grep - Glob --- # Storybook - Component Documentation Create comprehensive, auto-generated component documentation using Storybook's autodocs feature, MDX pages, and JSDoc comments. ## Key Concepts ### Autodocs Automatically generate documentation pages from stories: ```typescript const meta = { title: 'Components/Button', component: Button, tags: ['autodocs'], // Enable auto-documentation parameters: { docs: { description: { component: 'A versatile button component with multiple variants and sizes.', }, }, }, } satisfies Meta; ``` ### MDX Documentation Create custom documentation pages with full control: ```mdx import { Meta, Canvas, Story, Controls } from '@storybook/blocks'; import * as ButtonStories from './Button.stories'; # Button Component A versatile button component for user interactions. ## Usage ## Props ``` ### JSDoc Comments Document component props for auto-extraction: ```typescript interface ButtonProps { /** * The button label text */ label: string; /** * Is this the principal call to action? * @default false */ primary?: boolean; /** * Button size variant * @default 'medium' */ size?: 'small' | 'medium' | 'large'; } ``` ## Best Practices ### 1. Enable Autodocs Add the `autodocs` tag to generate documentation automatically: ```typescript const meta = { component: Button, tags: ['autodocs'], parameters: { docs: { description: { component: 'Button component with primary and secondary variants.', }, }, }, } satisfies Meta; ``` ### 2. Document Component Descriptions Provide clear, concise component descriptions: ```typescript const meta = { component: Tooltip, tags: ['autodocs'], parameters: { docs: { description: { component: ` Tooltip displays helpful information when users hover over an element. Supports multiple placements and can be triggered by hover, click, or focus. ## Features - Multiple placement options - Customizable delay - Accessible (ARIA compliant) - Keyboard navigation support `.trim(), }, }, }, } satisfies Meta; ``` ### 3. Document Story Variations Add descriptions to individual stories: ```typescript export const WithIcon: Story = { args: { label: 'Download', icon: 'download', }, parameters: { docs: { description: { story: 'Buttons can include icons to enhance visual communication.', }, }, }, }; export const Loading: Story = { args: { loading: true, label: 'Processing...', }, parameters: { docs: { description: { story: 'Loading state disables interaction and shows a spinner.', }, }, }, }; ``` ### 4. Use JSDoc for Type Documentation Document props with JSDoc for rich documentation: ```typescript interface CardProps { /** * Card title displayed at the top */ title: string; /** * Optional subtitle below the title */ subtitle?: string; /** * Card variant affecting visual style * @default 'default' */ variant?: 'default' | 'outlined' | 'elevated'; /** * Card content */ children: React.ReactNode; /** * Called when card is clicked * @param event - The click event */ onClick?: (event: React.MouseEvent) => void; /** * Elevation level (shadow depth) * @minimum 0 * @maximum 5 * @default 1 */ elevation?: number; } ``` ### 5. Create Usage Examples Show realistic usage examples in MDX: ```mdx import { Meta, Canvas, Story } from '@storybook/blocks'; import * as FormStories from './Form.stories'; # Form Component ## Basic Usage ## With Validation ```tsx import { Form } from './Form'; function MyForm() { return (
console.log(data)} validationSchema={schema} >
); } ``` ``` ## Common Patterns ### Component Overview Page ```mdx import { Meta, Canvas, Controls } from '@storybook/blocks'; import * as ButtonStories from './Button.stories'; # Button Buttons trigger actions and events throughout the application. ## Variants ### Primary Use primary buttons for the main call-to-action. ### Secondary Use secondary buttons for less important actions. ## Props ## Accessibility - Keyboard accessible (Enter/Space to activate) - Screen reader friendly - Focus visible indicator - Proper ARIA labels ## Best Practices - Use clear, action-oriented labels - Provide sufficient click target size (min 44×44px) - Don't use more than one primary button per section - Include loading states for async actions ``` ### API Documentation ```typescript const meta = { component: DataGrid, tags: ['autodocs'], parameters: { docs: { description: { component: 'Advanced data grid with sorting, filtering, and pagination.', }, }, }, argTypes: { data: { description: 'Array of data objects to display', table: { type: { summary: 'Array>' }, }, }, columns: { description: 'Column configuration', table: { type: { summary: 'ColumnDef[]' }, }, }, onRowClick: { description: 'Callback fired when a row is clicked', table: { type: { summary: '(row: any, index: number) => void' }, }, }, }, } satisfies Meta; ``` ### Migration Guides ```mdx import { Meta } from '@storybook/blocks'; # Migration Guide: v2 → v3 ## Breaking Changes ### Button Component **Before (v2):** ```tsx ``` **After (v3):** ```tsx ``` The `type` prop has been renamed to `variant` for consistency. ### Input Component **Before (v2):** ```tsx ``` **After (v3):** ```tsx ``` Error handling now uses an object to support additional metadata. ## New Features ### Icon Support Buttons now support icons: ```tsx ``` ``` ### Design Tokens Document design tokens and theming: ```mdx import { Meta, ColorPalette, ColorItem, Typeset } from '@storybook/blocks'; # Color Palette ## Primary Colors ## Typography ``` ## Advanced Patterns ### Inline Stories in MDX ```mdx import { Meta, Story } from '@storybook/blocks'; import { Button } from './Button'; # Button Examples ## Inline Story {() => { const [count, setCount] = React.useState(0); return ( ); }} ``` ### Code Snippets ```mdx import { Source } from '@storybook/blocks'; # Installation Install the component library: Then import components: ); } `} /> ``` ## Anti-Patterns ### ❌ Don't Skip Component Descriptions ```typescript // Bad const meta = { component: Button, tags: ['autodocs'], } satisfies Meta; ``` ```typescript // Good const meta = { component: Button, tags: ['autodocs'], parameters: { docs: { description: { component: 'Primary UI component for user actions.', }, }, }, } satisfies Meta; ``` ### ❌ Don't Use Generic JSDoc Comments ```typescript // Bad interface ButtonProps { /** The label */ label: string; /** The size */ size?: string; } ``` ```typescript // Good interface ButtonProps { /** Text displayed on the button */ label: string; /** * Visual size of the button * @default 'medium' */ size?: 'small' | 'medium' | 'large'; } ``` ### ❌ Don't Forget Story Descriptions ```typescript // Bad export const Disabled: Story = { args: { disabled: true }, }; ``` ```typescript // Good export const Disabled: Story = { args: { disabled: true }, parameters: { docs: { description: { story: 'Disabled state prevents user interaction and dims the button visually.', }, }, }, }; ``` ## Related Skills - **storybook-story-writing**: Creating well-structured stories - **storybook-args-controls**: Configuring interactive controls for props - **storybook-configuration**: Setting up Storybook with proper documentation addons