Upgrade to Pro
Unlimited projects and priority support.
Your trial ends in 3 days. Upgrade now to keep everything you've built.
A hands-on tutorial · ~2 hours
Start with one button, see it in Storybook, give it variants, then grow it into a small, accessible set of form components driven by design tokens, and compose them into a Card. The way real design-system teams do it.
STEP 01
You need Node 20 or newer. Check with node -v. We'll scaffold a React + TypeScript project with Vite, then add Storybook on top.
Now add Storybook. The installer detects React and Vite on its own. Accept the recommended options when it asks.
When it finishes, Storybook opens at http://localhost:6006. You can start it again any time with:
The installer adds some example stories in src/stories/. Delete that whole folder. We're writing our own.
Last, install classnames. Its cx helper combines CSS class names, and every component uses it.
Here's where we're headed. Each component gets its own folder with three files: the component, its styles, and its stories. Styles are never shared between components. If a bigger component needs a button, it renders our Button instead of borrowing its CSS.
This guide uses Storybook 9+ imports: @storybook/react-vite and storybook/test. If package.json shows Storybook 8, use @storybook/react and @storybook/test instead. Everything else is the same.
STEP 02
Before any components, we define the design decisions they'll share: colors, spacing, radii, type sizes. These are design tokens, stored as CSS custom properties.
There are two layers. A palette of raw values (--blue-500), and semantic tokens that describe a job (--color-action-primary). Components only ever use the semantic ones.
Storybook renders components in its own preview frame, so it needs to load the tokens too. Open .storybook/preview.ts and add the import at the top:
If you rebrand from blue to purple, you change one line: --color-action-primary. Every button, focus ring and radio dot follows. Dark mode is the same idea: redefine the semantic tokens, and you never touch a component.
STEP 04
A story is one rendered state of a component. You'll write a stories file next to every component. It becomes a live playground, documentation, and later, a test.
Run npm run storybook and open Components → Button. Things to try:
children. The button updates live.You're building the button in isolation: no app, no routing, no data. Every state you care about (disabled, long label, error) is one click away instead of buried three screens deep in an app.
STEP 05
Real buttons come in flavors. We'll add two props: variant (what it means) and size (how big it is). Here's the target:
Update the component. TypeScript union types make the allowed values explicit, and the editor autocompletes them.
In the CSS, take the color and padding out of .button and add a class per variant and size. The base class keeps everything they share.
Now give each variant a story, plus one story that shows all the sizes side by side. argTypes turns variant and size into radio buttons in the Controls panel.
In the Primary story, switch variant to danger in Controls. Then open Sizes and switch the variant there. All three buttons change, because they share args.
fullWidth boolean prop that makes the button width: 100%.loading prop: show a small spinner, set aria-busy, and disable clicks while it's true.STEP 06
Form fields are where accessibility gets real. Our Input always has a label (it's required in the types), and it wires up hint and error text so screen readers announce them.
We'll never share your email.
Enter a valid email address.
aria-describedby is a space-separated list of ids, the same shape as className. So cx builds it too, including only the ids for messages that actually render.
htmlFor links the label to the input, so clicking the label focuses it. aria-describedby makes a screen reader read the hint or error after the label. aria-invalid announces "invalid" and gives us a hook for the red border in CSS.
required story and show an asterisk after the label when props.required is true.size prop (sm / md) that matches the Button sizes, so they line up in a toolbar.STEP 07
Same pattern as Input, with its own stylesheet, plus one new feature: an optional character counter.
Shown on your public profile.
0 / 200Yes, on purpose. Each component owns its styles, so changing how a TextArea looks can never break an Input. What they do share is the tokens: both read --color-border-strong and --color-focus-ring, so they stay visually consistent without being coupled.
autoGrow prop that resizes the textarea to fit its content as the user types. Hint: set height from scrollHeight in the change handler.STEP 08
Inputs and textareas are easy to make accessible by hand. Radio groups aren't: arrow keys should move between options, only one option should be in the tab order, and screen readers need the right roles.
Instead of writing all that ourselves, we use Radix Primitives. They're unstyled, accessible building blocks. Radix handles behavior; we handle looks, with our tokens.
Radix exposes state through data attributes like data-state="checked" and data-disabled. We style those directly; no extra classes or JavaScript needed.
In Storybook, click into the canvas and press Tab until a radio is focused, then use the arrow keys. Selection moves between options and skips the disabled one. That's Radix doing the work.
STEP 09
Same approach. The Radix Checkbox also supports a third state, 'indeterminate', used for "select all" when only some items are checked.
Switch with @radix-ui/react-switch. It follows exactly the same pattern.useState inside a render function.STEP 10
Everything so far has been a basic component: one job, one element. A Card is an advanced component. Instead of inventing new buttons or new styles, it's assembled from pieces that already exist.
Composition shows up two ways here:
CardHeader, CardTitle, CardContent, CardFooter…) that the app arranges. The Card doesn't decide which buttons go in the footer. Whoever uses it does.Unlimited projects and priority support.
Your trial ends in 3 days. Upgrade now to keep everything you've built.
Switch themes from your profile settings.
All the parts live in one file, since they only make sense together. Each part is a thin wrapper around a semantic HTML element, using the same ComponentProps + cx pattern as everything else.
The Card passes className={styles.dismiss} to our Button. That class only sets a margin to tuck the button into the corner. It never touches colors or padding, so the Button stays in charge of looking like a Button. This is exactly why Button merges className with cx instead of replacing it.
The stories are where composition really shows. Each one builds a different real-world card out of the same parts, plus the Button and Input you already made.
primaryAction prop?
You could give Card props like primaryActionLabel and onPrimaryAction. It works until someone needs two buttons, a link, a disabled state or a form. Then the prop list grows forever. Accepting children and letting the app compose real Buttons covers every one of those cases with zero new props.
CardMedia part that renders an image flush to the card's top edge (hint: negative margins equal to the card's padding).StatCard that composes Card parts internally and takes just label, value and trend props. That's an even more advanced component built from this one.STEP 11
Apps that use your library should import from a single entry point, not dig through your folders. This file is your library's public API.
The a11y addon runs automated accessibility checks against every story and reports problems in a panel.
Restart Storybook and open the Accessibility tab on each story. Try deleting a label prop from an Input story and watch it flag the problem.
Five typed, accessible basic components and one advanced Card composed from them. Each owns its own styles, all share one token system, and every one has documented stories. That's the core of how production design systems work.
Where to go next:
[data-theme='dark'], then add a theme switcher to Storybook's toolbar.play functions to stories to simulate clicks and typing, then run them with Storybook's test addon.vite-plugin-dts, and npm publish.npm run build-storybook outputs a static site you can host on Vercel and link from your resume.