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, '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. ``. */ icon?: ReactNode /** Renders a remove button after the label, e.g. for filter chips. */ onRemove?: (event: MouseEvent) => 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 ( {dot && ( ) } export type CountBadgeProps = Omit, '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 ( {children} {visible && ( {pulse && ( )} ) }