Document components with stories
Document a component in isolation
A Storybook story describes one meaningful state of a component, such as a ready badge or a waiting badge. Unlike a screenshot, a story renders actual component code when the configured Storybook builder runs. Component Story Format (CSF) organizes stories as JavaScript or TypeScript module exports. Args supply inputs; controls let reviewers explore them. A decorator wraps a story with shared context, styling or layout. Keep fixtures deterministic and avoid loading actual customer data. This chapter uses an original repair desk status badge and Storybook 9-style React/Vite CSF syntax as a proposed baseline. The framework and its packages must be pinned and reviewed together before executing the examples.
import React from 'react';export type Status = 'ready' | 'waiting';export function Badge({status}: {status: Status}) { return <span className="bm-badge" data-status={status}>{status === 'ready' ? 'Ready' : 'Waiting'}</span>;}The visible words communicate status independently of color. This component is a static label; it does not automatically announce changing status to assistive technology. A parent responsible for an asynchronous operation may need a persistent status region. The packaged variant in the next lesson adds explicit runtime rejection of unsupported values.
Use args and decorators for predictable states
import React from 'react';import type {Meta, StoryObj} from '@storybook/react-vite';import {Badge} from './Badge';import './package/badge.css';const meta = { component: Badge, title: 'Repair desk/Badge', tags: ['autodocs'], args: {status: 'ready'}, argTypes: {status: {control: 'select', options: ['ready', 'waiting']}}, decorators: [(Story) => <div style={{padding: '1rem'}}><Story /></div>],} satisfies Meta<typeof Badge>;export default meta;type Story = StoryObj<typeof meta>;export const Ready: Story = {};export const Waiting: Story = {args: {status: 'waiting'}};Ready inherits the default args and Waiting overrides one value. The decorator gives both previews the same padding. With a compatible docs addon, the autodocs tag can generate a documentation page; it does not itself install the addon. In an approved Storybook React/Vite workspace configure the story glob and framework, then place these files under its included source directory. Use one matching version of Storybook packages and check the builder configuration. No package install is performed in this chapter. For visual regression, compare screenshots captured with a fixed viewport, font and browser; review legitimate changes instead of blindly updating baselines.
Exercise observable behavior with a play function
import React, {useState} from 'react';import type {Meta, StoryObj} from '@storybook/react-vite';import {expect} from 'storybook/test';import {Badge} from './Badge';import './package/badge.css';function StatusToggle() { const [ready, setReady] = useState(false); return <section><Badge status={ready ? 'ready' : 'waiting'} /> <button type="button" onClick={() => setReady(v => !v)}>Change status</button></section>;}const meta = {component: StatusToggle, title: 'Repair desk/Status toggle'} satisfies Meta<typeof StatusToggle>;export default meta;type Story = StoryObj<typeof meta>;export const ChangesStatus: Story = {play: async ({canvas, userEvent}) => { await expect(canvas.getByText('Waiting')).toBeInTheDocument(); await userEvent.click(canvas.getByRole('button', {name: 'Change status'})); await expect(canvas.getByText('Ready')).toBeInTheDocument();}};The interaction should begin with Waiting, activate the semantic button and observe Ready. The play function runs after rendering in a compatible Storybook environment; it does not run merely because TypeScript transforms this file. Older Storybook versions use different test imports and canvas access. Run the story with a matching test integration and check keyboard interaction independently. A passing story test does not prove the entire application works or satisfies accessibility requirements. Practice: add a story that clicks twice and checks the initial state returns.