Skip to main content
GitHub

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

npx nxds-cli@latest init --stories

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.

StepWhat init --stories does
Wiring fileCopies storybook.tsx into your nxds folder
PreviewAdds your nxds CSS entry and the nxds wiring to .storybook/preview
StoriesAdds your nxds folder to stories in .storybook/main, if no entry covers it
TailwindAdds @tailwindcss/vite to .storybook/main, if Storybook can't see the Tailwind your app builds with
Installed itemsCopies the stories of every item your app has

init also records "stories": true in nxds.json.

Add a component with its stories

npx nxds-cli@latest add dialog

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

npx vitest run --project=storybook

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:

PartWhat it does
decoratorsRenders each story inside the nxds appearance provider
globalTypes, initialGlobalsAdds a toolbar for mode, brand color, surface tone, UI scale, text size, density, corners, elevation and stroke
parametersTurns 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:

// .storybook/preview.tsx
import type { Preview } from '@storybook/react-vite';

import '../src/nxds.css';
import { defaultAppearance } from '../src/appearance-state';
import { nxdsPreview } from '../src/components/nxds/storybook';

const nxds = nxdsPreview(defaultAppearance);

const preview: Preview = {
  decorators: [...nxds.decorators],
  parameters: {
    a11y: { options: nxds.parameters.a11y.options },
  },
  globalTypes: { ...nxds.globalTypes },
  initialGlobals: { ...nxds.initialGlobals },
};

export default preview;

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

npx nxds-cli@latest init --no-stories

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 doctor passes its stories check.
  • Storybook lists the stories in your nxds folder.
  • The Storybook toolbar switches a story between light and dark.

Next steps

  • Install: every nxds-cli command and option.
  • Components: each component, with its add command.
  • Appearance: the settings the toolbar changes.