Files
tansy-ui/src/components/SelectParts.tsx
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

276 lines
8.1 KiB
TypeScript

/**
* Internal building blocks shared by Select, FeaturedSelect and Combobox:
* the option rows, group headings, loading and empty rows, the clear button
* and the popup footer action. Not part of the public API.
*/
import { Combobox as BaseCombobox } from '@base-ui/react/combobox'
import { Select as BaseSelect } from '@base-ui/react/select'
import { Check, ChevronDown, LoaderCircle, X } from 'lucide-react'
import { Fragment, useId, type ReactNode } from 'react'
import { cn } from '../lib/cn'
import { focusRing, listItem } from '../lib/styles'
import {
groupOptions,
listClass,
type ComboboxGroup,
type RenderOption,
type SelectActionButton,
type SelectOption,
type SelectSize,
} from './select.tokens'
const itemClass = cn(listItem, 'scroll-my-1 py-2 data-selected:font-medium')
const groupLabelClass = 'px-2.5 pt-2 pb-1 text-xs font-semibold text-fg-muted select-none'
const separatorClass = 'mx-1 my-1 h-px bg-border'
const indicatorClass = 'ml-auto flex shrink-0 text-primary-600 dark:text-primary-400 [&_svg]:size-4'
export function Chevron() {
return <ChevronDown aria-hidden="true" />
}
export function Spinner({ className }: { className?: string }) {
return <LoaderCircle aria-hidden="true" className={cn('animate-spin motion-reduce:animate-none', className)} />
}
/** Right offset of the clear button, so it sits in the spacer left of the chevron. */
const CLEAR_RIGHT: Record<SelectSize, string> = { sm: 'right-8', md: 'right-9', lg: 'right-11' }
const CLEAR_BOX: Record<SelectSize, string> = {
sm: 'size-5 [&_svg]:size-3.5',
md: 'size-6 [&_svg]:size-4',
lg: 'size-7 [&_svg]:size-4.5',
}
/** A real, labelled button that clears the value. Positioned over the trigger's spacer. */
export function ClearButton({
size,
label,
onClear,
className,
}: {
size: SelectSize
label: string
onClear: () => void
className?: string
}) {
return (
<button
type="button"
aria-label={label}
onClick={onClear}
className={cn(
'absolute top-1/2 z-10 flex -translate-y-1/2 items-center justify-center rounded-md text-fg-muted transition-colors hover:bg-surface-sunken hover:text-fg',
focusRing,
'focus-visible:outline-offset-0',
CLEAR_RIGHT[size],
CLEAR_BOX[size],
className,
)}
>
<X aria-hidden="true" />
</button>
)
}
/** Label, description and custom rendering of one option. */
function OptionBody({
option,
selected,
renderOption,
descriptionId,
}: {
option: SelectOption
selected: boolean
renderOption?: RenderOption
descriptionId: string
}) {
if (renderOption) return <span className="min-w-0 flex-1">{renderOption(option, { selected })}</span>
return (
<span className="flex min-w-0 flex-1 flex-col">
<span className="truncate">{option.label}</span>
{option.description && (
<span id={descriptionId} className="truncate text-xs font-normal leading-5 text-fg-muted">
{option.description}
</span>
)}
</span>
)
}
function SelectItem({
option,
selected,
renderOption,
}: {
option: SelectOption
selected: boolean
renderOption?: RenderOption
}) {
const descriptionId = useId()
const described = option.description && !renderOption
return (
<BaseSelect.Item
value={option.value}
label={option.label}
disabled={option.disabled}
aria-label={renderOption ? undefined : option.label}
aria-describedby={described ? descriptionId : undefined}
className={itemClass}
>
<OptionBody option={option} selected={selected} renderOption={renderOption} descriptionId={descriptionId} />
<BaseSelect.ItemIndicator className={indicatorClass}>
<Check aria-hidden="true" />
</BaseSelect.ItemIndicator>
</BaseSelect.Item>
)
}
/** Items for a Base UI Select list, grouped by `group` when any option has one. */
export function SelectOptions({
options,
isSelected,
renderOption,
}: {
options: SelectOption[]
isSelected: (value: string) => boolean
renderOption?: RenderOption
}) {
return groupOptions(options).map((group, index) => {
const items = group.items.map((option) => (
<SelectItem
key={option.value}
option={option}
selected={isSelected(option.value)}
renderOption={renderOption}
/>
))
if (group.label === null) return <Fragment key="__ungrouped">{items}</Fragment>
return (
<Fragment key={group.label}>
{index > 0 && <BaseSelect.Separator className={separatorClass} />}
<BaseSelect.Group>
<BaseSelect.GroupLabel className={groupLabelClass}>{group.label}</BaseSelect.GroupLabel>
{items}
</BaseSelect.Group>
</Fragment>
)
})
}
export function ComboboxItem({
option,
selected,
renderOption,
index,
}: {
option: SelectOption
selected: boolean
renderOption?: RenderOption
index?: number
}) {
const descriptionId = useId()
const described = option.description && !renderOption
return (
<BaseCombobox.Item
value={option}
index={index}
disabled={option.disabled}
aria-label={renderOption ? undefined : option.label}
aria-describedby={described ? descriptionId : undefined}
className={itemClass}
>
<OptionBody option={option} selected={selected} renderOption={renderOption} descriptionId={descriptionId} />
<BaseCombobox.ItemIndicator className={indicatorClass}>
<Check aria-hidden="true" />
</BaseCombobox.ItemIndicator>
</BaseCombobox.Item>
)
}
/** The list inside a Base UI Combobox popup, grouped or flat. */
export function ComboboxOptions({
grouped,
isSelected,
renderOption,
}: {
grouped: boolean
isSelected: (value: string) => boolean
renderOption?: RenderOption
}) {
if (!grouped) {
return (
<BaseCombobox.List className={cn(listClass, 'data-empty:hidden')}>
{(option: SelectOption, index: number) => (
<ComboboxItem
key={option.value}
option={option}
index={index}
selected={isSelected(option.value)}
renderOption={renderOption}
/>
)}
</BaseCombobox.List>
)
}
return (
<BaseCombobox.List className={cn(listClass, 'data-empty:hidden')}>
{(group: ComboboxGroup, index: number) => (
<Fragment key={group.value || '__ungrouped'}>
{index > 0 && <BaseCombobox.Separator className={separatorClass} />}
<BaseCombobox.Group items={group.items}>
{group.value && (
<BaseCombobox.GroupLabel className={groupLabelClass}>{group.value}</BaseCombobox.GroupLabel>
)}
<BaseCombobox.Collection>
{(option: SelectOption) => (
<ComboboxItem
key={option.value}
option={option}
selected={isSelected(option.value)}
renderOption={renderOption}
/>
)}
</BaseCombobox.Collection>
</BaseCombobox.Group>
</Fragment>
)}
</BaseCombobox.List>
)
}
/** A muted row for loading, empty and status messages. */
export function StatusRow({ loading, children }: { loading?: boolean; children: ReactNode }) {
return (
<div className="flex items-center gap-2 px-3 py-2.5 text-sm text-fg-muted [&_svg]:size-4">
{loading && <Spinner />}
{children}
</div>
)
}
/** Footer button in a popup, such as "Add department". */
export function ActionFooter({ action, onDone }: { action: SelectActionButton; onDone: () => void }) {
return (
<div className="shrink-0 border-t border-border p-1">
<button
type="button"
onClick={() => {
onDone()
action.onClick()
}}
className={cn(
listItem,
focusRing,
'w-full py-2 font-medium text-primary-700 hover:bg-surface-sunken focus-visible:outline-offset-0 dark:text-primary-300 [&_svg]:size-4',
)}
>
{action.icon != null && (
<span aria-hidden="true" className="flex">
{action.icon}
</span>
)}
{action.label}
</button>
</div>
)
}