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

173 lines
5.3 KiB
TypeScript

import type { ComponentProps, MouseEvent, ReactNode } from 'react'
import { X } from 'lucide-react'
import { cn } from '../lib/cn'
import { focusRing } from '../lib/styles'
import {
BADGE_DOT,
BADGE_DOT_SIZES,
BADGE_SIZES,
BADGE_STYLES,
COUNT_BADGE_PING,
COUNT_BADGE_PLACEMENT,
COUNT_BADGE_SIZES,
COUNT_BADGE_STYLES,
colorForValue,
type BadgeColor,
type BadgeSize,
type BadgeVariant,
type ColorRange,
type CountBadgePlacement,
type CountBadgeSize,
} from './badge.tokens'
export type BadgeProps = Omit<ComponentProps<'span'>, 'color'> & {
/** Semantic colour. Overridden by a matching `colorRanges` entry. */
color?: BadgeColor
/** Visual weight. `soft` for most status labels, `solid` for emphasis. */
variant?: BadgeVariant
size?: BadgeSize
/** Shows a leading status dot. */
dot?: boolean
/** Icon before the label. Pass a decorative icon, e.g. `<Check />`. */
icon?: ReactNode
/** Renders a remove button after the label, e.g. for filter chips. */
onRemove?: (event: MouseEvent<HTMLButtonElement>) => void
/** Accessible name of the remove button. Defaults to "Remove {children}". */
removeLabel?: string
/** Colour the badge by value when `children` is a number, e.g. `[{ min: 10, max: Infinity, color: 'danger' }]`. */
colorRanges?: ColorRange[]
}
/** A short status label: a state, a category or a count. */
export function Badge({
color = 'neutral',
variant = 'soft',
size = 'md',
dot = false,
icon,
onRemove,
removeLabel,
colorRanges,
className,
children,
...props
}: BadgeProps) {
const resolved = colorForValue(children, colorRanges, color)
const text = typeof children === 'string' || typeof children === 'number' ? String(children) : undefined
return (
<span
className={cn(
'inline-flex shrink-0 items-center whitespace-nowrap rounded-full border font-medium leading-none tabular-nums',
'[&_svg]:shrink-0',
BADGE_STYLES[variant][resolved],
BADGE_SIZES[size],
onRemove && 'pr-0.5',
className,
)}
{...props}
>
{dot && (
<span
aria-hidden="true"
className={cn('shrink-0 rounded-full', BADGE_DOT_SIZES[size], variant === 'solid' ? 'bg-current' : BADGE_DOT[resolved])}
/>
)}
{icon}
{children}
{onRemove && (
<button
type="button"
aria-label={removeLabel ?? (text ? `Remove ${text}` : 'Remove')}
onClick={onRemove}
className={cn(
'inline-flex shrink-0 items-center justify-center rounded-full opacity-70 transition-[opacity,background-color]',
'hover:bg-current/15 hover:opacity-100',
size === 'sm' ? 'size-4' : size === 'md' ? 'size-5' : 'size-6',
focusRing,
)}
>
<X aria-hidden="true" strokeWidth={2.5} />
</button>
)}
</span>
)
}
export type CountBadgeProps = Omit<ComponentProps<'span'>, 'color'> & {
/** The element the count is pinned to: an icon, an avatar or a button. */
children: ReactNode
/** The number to show. */
count?: number
/** Counts above this show as "{max}+". */
max?: number
/** Show the badge when `count` is 0. */
showZero?: boolean
/** Show a small dot instead of the number. */
dot?: boolean
/** Which corner of the element the badge sits on. */
placement?: CountBadgePlacement
/** Draws attention with a pulsing ring (off under reduced motion). */
pulse?: boolean
color?: BadgeColor
size?: CountBadgeSize
/** Colour by value, e.g. warning from 5 and danger from 10. */
colorRanges?: ColorRange[]
/**
* Accessible name of the badge, e.g. "3 unread notifications". A function
* receives the count. Defaults to the visible text.
*/
label?: string | ((count: number) => string)
}
/** Pins a count or a dot to a corner of an element. */
export function CountBadge({
children,
count = 0,
max = 99,
showZero = false,
dot = false,
placement = 'top-end',
pulse = false,
color = 'danger',
size = 'md',
colorRanges,
label,
className,
...props
}: CountBadgeProps) {
const visible = dot || count > 0 || showZero
const display = count > max ? `${max}+` : String(count)
const name = typeof label === 'function' ? label(count) : (label ?? (dot ? undefined : display))
const resolved = colorForValue(count, colorRanges, color)
const sizes = COUNT_BADGE_SIZES[size]
return (
<span className={cn('relative inline-flex shrink-0 align-middle', className)} {...props}>
{children}
{visible && (
<span
role="status"
aria-label={name}
className={cn(
'pointer-events-none absolute z-10 inline-flex items-center justify-center rounded-full font-semibold leading-none tabular-nums ring-2 ring-surface',
COUNT_BADGE_PLACEMENT[placement],
COUNT_BADGE_STYLES[resolved],
dot ? sizes.dot : sizes.count,
)}
>
{pulse && (
<span
aria-hidden="true"
className={cn(
'absolute inset-0 -z-10 animate-ping rounded-full opacity-60 motion-reduce:hidden',
COUNT_BADGE_PING[resolved],
)}
/>
)}
{!dot && <span aria-hidden={name ? true : undefined}>{display}</span>}
</span>
)}
</span>
)
}