Files
tansy-ui/src/components/switch.tokens.ts
T
thekhalidsha f01de83733 Rebuild Tansy UI on Base UI with one API and dark mode (v0.3.0)
Every component is rebuilt on @base-ui/react with a single prop API,
token-only colours and dark mode, and 28 new components are added.

Components
- New: Field, FeaturedTextArea, NumberInput, Combobox, CheckboxGroup,
  Slider, ToggleGroup/Toggle, IconButton, Dialog/Modal, AlertDialog,
  Drawer/Sheet, Popover, InfoTip, DropdownMenu, Tabs, Breadcrumb,
  Pagination, Toast (Toaster, useToast), Progress, Meter, Skeleton,
  CountBadge, Avatar/AvatarGroup, Kbd, Separator, Table
- Rewritten: Button, Input, FeaturedInput, TextArea, Select,
  FeaturedSelect (real ARIA combobox), NativeSelect, Checkbox, Radio,
  Switch, Tooltip, Accordion, Alert, Spinner, Badge, Card
- Removed TextField (use Input) and sonner (use Toast)

Foundations
- Semantic tokens (surfaces, text, borders, status ramps) in
  src/styles/tokens.css, with class-based dark mode
- Visible focus rings, linked labels/descriptions/errors, alert roles by
  severity, keyboard support from Base UI
- Fixed the Classic Blue ramp; Emerald, Amber and Teal 500 steps now
  carry white text at AA
- Library build (per-component ES modules, types, tokens.css, exports)

Quality
- Vitest, Testing Library and axe tests for every component (289 tests)
- scripts/check-stories.mjs renders every story in Chrome and fails on
  errors or axe violations, including contrast; all 423 stories pass in
  light and dark mode and in all seven palettes
- Storybook: controls for every prop, open states for every popup,
  light/dark toolbar, Introduction guides, docs/CONVENTIONS.md

BREAKING CHANGE: component props changed across the kit; see CHANGELOG.md.
2026-09-28 21:12:11 +05:30

49 lines
2.1 KiB
TypeScript

/**
* Switch style tokens.
*
* Kept apart from Switch.tsx so that file only exports components (Vite fast
* refresh). Every entry is a complete, literal class string.
*
* Geometry: the track has 2px padding (`p-0.5`), so
* travel = track width - thumb width - 4px.
* sm: 36 - 16 - 4 = 16px (translate-x-4)
* md: 44 - 20 - 4 = 20px (translate-x-5)
* lg: 52 - 24 - 4 = 24px (translate-x-6)
*/
import type { Color, Size } from '../lib/styles'
export type SwitchSize = Size
export type SwitchColor = Color
export type SwitchLabelPosition = 'end' | 'start'
export const SWITCH_SIZES: Record<SwitchSize, { track: string; thumb: string; icon: string }> = {
sm: { track: 'h-5 w-9', thumb: 'size-4 data-checked:translate-x-4', icon: '[&_svg]:size-2.5' },
md: { track: 'h-6 w-11', thumb: 'size-5 data-checked:translate-x-5', icon: '[&_svg]:size-3' },
lg: { track: 'h-7 w-[3.25rem]', thumb: 'size-6 data-checked:translate-x-6', icon: '[&_svg]:size-3.5' },
}
/** Pushes the label down so its first line is centred on the track. */
export const SWITCH_LABEL_OFFSET: Record<SwitchSize, string> = {
sm: 'pt-0',
md: 'pt-0.5',
lg: 'pt-0.5',
}
export const SWITCH_LABEL_SIZES: Record<SwitchSize, string> = {
sm: 'text-sm leading-5',
md: 'text-sm leading-5',
lg: 'text-base leading-6',
}
/** Track fill when on, and the colour of an icon inside the thumb when on. */
export const SWITCH_COLORS: Record<SwitchColor, { track: string; icon: string; thumb?: string }> = {
primary: { track: 'data-checked:bg-primary-500', icon: 'data-checked:text-primary-600' },
// In dark mode `fg` is light, so the thumb turns dark to stay visible.
neutral: { track: 'data-checked:bg-fg', icon: 'data-checked:text-fg', thumb: 'dark:data-checked:bg-surface' },
success: { track: 'data-checked:bg-success-600', icon: 'data-checked:text-success-700' },
warning: { track: 'data-checked:bg-warning-400', icon: 'data-checked:text-warning-800' },
danger: { track: 'data-checked:bg-danger-600', icon: 'data-checked:text-danger-700' },
info: { track: 'data-checked:bg-info-600', icon: 'data-checked:text-info-700' },
}