Storybook
Get each component's Storybook stories with it, in a React app set up with
nxds-cli. Each story tests the component's keyboard, focus and screen-reader
behaviour, so your Storybook shows when an edit breaks one.
Turn stories on
init --stories needs Storybook 10 or later on @storybook/react-vite or
@storybook/nextjs-vite. If your app has no Storybook, add it first with
npx storybook@latest init.
| Step | What init --stories does |
|---|---|
| Wiring file | Copies storybook.tsx into your nxds folder |
| Preview | Adds your nxds CSS entry and the nxds wiring to .storybook/preview |
| Stories | Adds your nxds folder to stories in .storybook/main, if no entry covers it |
| Tailwind | Adds @tailwindcss/vite to .storybook/main, if Storybook can't see the Tailwind your app builds with |
| Installed items | Copies the stories of every item your app has |
init also records "stories": true in nxds.json.
Add a component with its stories
add copies Dialog.stories.tsx beside dialog.tsx. It also installs the
components the stories use, such as Button and Input, and any package they
import. A package only a story imports is a dev dependency.
Each story draws its icons with your icon library. A story has no title, so Storybook names it from its path.
Run the stories as tests
Storybook's Vitest addon runs each story in a browser. A story with a play
function clicks, types and tabs through the component, then checks the result.
If your Storybook has no storybook Vitest project, add the addon with
npx storybook add @storybook/addon-vitest.
What the wiring adds
nxdsPreview in storybook.tsx returns four parts of a Storybook preview:
| Part | What it does |
|---|---|
decorators | Renders each story inside the nxds appearance provider |
globalTypes, initialGlobals | Adds a toolbar for mode, brand color, surface tone, UI scale, text size, density, corners, elevation and stroke |
parameters | Turns off axe's two color contrast rules. nxds checks contrast with APCA, which those rules disagree with |
Stories start from defaultAppearance in your appearance-state.ts.
If your app sets its own colors, init passes
{ deriveColors: false } to nxdsPreview, so stories show your colors. The
toolbar then has no brand color or surface tone menu.
Wire the preview by hand
If init --stories can't extend your preview, it lists this step. Add the
four parts to the preview your Storybook exports:
Put your own decorators before ...nxds.decorators, so the provider wraps
them. Keep the default export an object, because Storybook reads the preview
file without running it.
If your app sets its own colors, call
nxdsPreview(defaultAppearance, { deriveColors: false }).
Edit and update stories
Stories are your files. update replaces a story you haven't edited, and
merges the release's changes into one you have. diff lists the stories that
differ from the latest release.
When you change a component, change its story to match. A story that fails names the behaviour the change broke.
Turn stories off
init --no-stories leaves the story files where they are, and prints how many
it left. add, update and diff stop copying and tracking them. Delete the
ones you don't need, because update no longer changes a story when its
component changes. Delete storybook.tsx only after you remove its import from
.storybook/preview.
init records "stories": false in nxds.json, so add stops suggesting
stories.
Check your setup
-
npx nxds-cli@latest doctorpasses itsstoriescheck. - Storybook lists the stories in your nxds folder.
- The Storybook toolbar switches a story between light and dark.
Next steps
- Install: every
nxds-clicommand and option. - Components: each component, with its
addcommand. - Appearance: the settings the toolbar changes.