Every story and the landing page now fit from a 320px phone to a wide desktop without scrolling sideways. - Inputs in flex rows (Input, FeaturedInput, NumberInput, Combobox) shrink instead of holding their native width - Tab rows and ToggleGroup scroll within themselves on narrow screens; vertical tabs stack above their panel on phones - Positioned popups cap at Base UI's --available-width - Landing page: tablet header no longer crowds; code sample fits at 1024px - Storybook a11y panel skips page-level rules that can't apply to a story - check-stories.mjs gains --width and fails on horizontal overflow; all 423 stories pass at 320, 375, 768 and desktop widths in both modes
7.2 KiB
Component conventions
Every Tansy UI component follows these rules, so the kit reads as one API. The
reference implementations are src/components/Button.tsx, Input.tsx and
Field.tsx, with their stories and tests. When in doubt, copy them.
Foundation
- Headless base: interactive parts are built on Base UI
(
@base-ui/react). Its docs ship in the package atnode_modules/@base-ui/react/docs/react/components/*.md, with Tailwind examples and full API tables. Use Base UI for anything with focus management, keyboard navigation, popups or ARIA state: Dialog, Popover, Menu, Select, Combobox, Tooltip, Tabs, Accordion, Checkbox, Radio, Switch, Slider, Toast, Field and more. Never hand-roll focus traps, dismissal or roving focus. - Styling: Tailwind v4 utilities only. Shared class strings live in
src/lib/styles.ts(focusRing,controlHeight,controlBox,popupSurface,popupMotion,listItem,labelText,descriptionText,errorText). Use them instead of repeating utilities. - Class merging: always
cn(...)fromsrc/lib/cn.ts, with the consumer'sclassNamelast so it wins. - Refs: React 19 passes
refas a normal prop. Type props fromComponentProps<'element'>and spread...propsonto the root element (or the main control for form fields). NoforwardRef, and never copy refs in an effect. - Ids: from
useId()or Base UI. Never derive anidfromname. - Files:
Name.tsxexports only components (Vite fast refresh). Style maps, types and constants go inname.tokens.ts. Stories inName.stories.tsx, tests inName.test.tsx.
Tokens and theming
Use only these colour utilities, never Tailwind's built-in hues (slate, gray,
red, blue…) or hex codes:
| Purpose | Utilities |
|---|---|
| Neutral surfaces | bg-background, bg-surface, bg-surface-muted, bg-surface-sunken |
| Borders | border-border, border-border-strong |
| Text | text-fg, text-fg-muted, text-fg-subtle |
| Brand | primary-50 … primary-900 (re-themed by the palette) |
| Status | success-*, warning-*, danger-*, info-* (50 … 900, fixed across palettes) |
| Overlay | bg-overlay; popup shadow shadow-popup |
Dark mode is the dark class on <html>. Surface, border and text tokens
switch automatically. Brand and status colours need dark: variants where
contrast demands it. Patterns that work:
- soft fill:
bg-{c}-50 text-{c}-700 dark:bg-{c}-500/15 dark:text-{c}-300 - outline:
border-{c}-300 text-{c}-700 dark:border-{c}-500/50 dark:text-{c}-300 - text on light surfaces:
-700, and-800for warning. White text on fills: primary500, danger600, success and info700. Warning fills taketext-warning-900.
Every story must pass axe colour contrast in both light and dark mode.
Props
| Concept | Prop | Values |
|---|---|---|
| Semantic colour | color |
'primary' | 'neutral' | 'success' | 'warning' | 'danger' | 'info' (type Color in lib/styles.ts) |
| Visual style | variant |
Per component, e.g. Button solid | soft | outline | ghost | link; Alert and Badge soft | solid | outline |
| Size | size |
'sm' | 'md' | 'lg', default md. Control heights come from controlHeight: 32, 40 and 48 px |
| Icons | startIcon, endIcon, icon |
ReactNode. Callers pass <Mail />. Never icon names as strings, never icon fonts |
| Field text | label, description, error |
ReactNode. error marks the control invalid and replaces description |
| Required | required |
Shows the mark and sets required on the control |
| Value | value, defaultValue, onValueChange(value) |
Every component works both controlled and uncontrolled |
| Boolean state | checked, defaultChecked, onCheckedChange(checked) |
Checkbox, Switch |
| Open state | open, defaultOpen, onOpenChange(open) |
Dialog, Popover, Menu, Tooltip, Drawer, Alert (dismissible) |
| Disabled | disabled |
Native disabled or Base UI's, with data-disabled styling |
Text inputs also keep the native onChange. Document every prop with a JSDoc
comment; Storybook's docs table reads them.
Accessibility
- A visible focus ring on every interactive element (
focusRing, orfocusWithinRingon wrappers around an input). - Form controls sit in
Field(or Base UIField.Root), so the label, description and error are linked (aria-labelledby,aria-describedby,aria-invalid). - Icon-only controls require
aria-label. Decorative icons getaria-hidden. - Popups: Escape closes, focus returns to the trigger, and the keyboard works as the WAI-ARIA pattern describes. Base UI does this when used as documented.
- Respect
motion-reduce:for animations.
Responsive
Every component must work from a 320px phone up without making the page scroll sideways:
- Inputs inside flex rows use
w-0 min-w-0 flex-1so they can shrink. - Rows of items (tabs, segmented controls) scroll within themselves on narrow screens rather than widening the page.
- Positioned popups cap at
max-w-(--available-width); never give them amin-widthlarger than that. - Story layouts wrap (
flex-wrap) instead of assuming a wide canvas.
Check with node scripts/check-stories.mjs --width 320 (and 375 and 768).
Stories
title:Group/Name. Groups:Actions,Forms,Overlays,Navigation,Feedback,Display,Layout.tags: ['autodocs'], a component description inparameters.docs.description.component, andargTypesfor every public prop with a matching control (inline-radiofor short enums,selectfor long ones,text,boolean,number;control: falsefor nodes and functions).- Callbacks use
fn()fromstorybook/test, so they appear in the Actions panel. - Stories:
Playground(args-driven), plus one per variant, colour, size and state (disabled, error, required, loading, read-only, empty),ControlledandUncontrolledwhere values exist, and realistic compositions. Popups should have a story that renders them open (defaultOpen), so they can be reviewed without clicking. - Realistic, generic sample content (Jane Doe, Acme Corporation, [email protected]). No real people or companies.
Tests
Every component has Name.test.tsx (Vitest, Testing Library, user-event):
- renders with the right role and accessible name,
- label, description and error wiring for form controls,
- controlled and uncontrolled behaviour,
- keyboard interaction for the component's pattern (Escape, arrows, Enter/Space),
await expectNoA11yViolations(container)fromsrc/test/a11y.ts.
Run npx vitest run src/components/Name.test.tsx.
Checking in a real browser
npm run build-storybook
node scripts/check-stories.mjs --filter forms-input --mode light
node scripts/check-stories.mjs --filter forms-input --mode dark
python3 scripts/contact-sheet.py /tmp/tansy-story-check/light --prefix forms-input
Before a release, also run it with --palette <name> for each of the seven
palettes: a colour that passes in Classic Blue can fail in Amber.
check-stories.mjs reports runtime errors, failed requests and axe violations,
including contrast, and saves a screenshot of every story. Look at the contact
sheet, not just the report.