The Coline design system: accessible React components for Coline Apps, themed live by the host workspace, and for Coline Sites in light and dark. Tailwind v4 stylesheet included. MIT.
| 5 | import { cn } from "../lib/utils"; |
| 6 | import { transitions } from "../lib/motion"; |
| 7 |
| 8 | export interface SegmentedOption<T extends string> { |
| 9 | value: T; |
| 10 | label: React.ReactNode; |
| 11 | icon?: React.ReactNode; |
| 12 | } |
| 13 |
| 14 | /** |
| 15 | * A row of mutually exclusive choices with a sliding selection. Arrow keys |
| 16 | * move the selection (roving focus), like a native radio group. |
| 17 | */ |
| 18 | export function SegmentedControl<T extends string>({ |
| 19 | value, |
| 20 | onValueChange, |
| 21 | options, |
| 22 | size = "md", |
| 23 | label, |
| 24 | className, |
| 25 | }: { |
| 26 | value: T; |
| 27 | onValueChange: (value: T) => void; |
| 28 | options: Array<SegmentedOption<T>>; |
| 29 | size?: "sm" | "md"; |
| 30 | label: string; |
| 31 | className?: string; |
| 32 | }) { |
| 33 | const id = React.useId(); |
| 34 | const refs = React.useRef<Array<HTMLButtonElement | null>>([]); |
| 35 | const selectedIndex = Math.max(0, options.findIndex((option) => option.value === value)); |
| 36 | const move = (delta: number) => { |
| 37 | const next = (selectedIndex + delta + options.length) % options.length; |
| 38 | const option = options[next]; |
| 39 | if (!option) return; |
| 40 | onValueChange(option.value); |
| 41 | refs.current[next]?.focus(); |
| 42 | }; |
| 43 | return ( |
| 44 | <div |
| 45 | role="radiogroup" |
| 46 | aria-label={label} |
| 47 | className={cn("inline-flex rounded-xl bg-muted p-0.5", className)} |
| 48 | onKeyDown={(event) => { |
| 49 | if (event.key === "ArrowRight" || event.key === "ArrowDown") { |
| 50 | event.preventDefault(); |
| 51 | move(1); |
| 52 | } else if (event.key === "ArrowLeft" || event.key === "ArrowUp") { |
| 53 | event.preventDefault(); |
| 54 | move(-1); |
| 55 | } |
| 56 | }} |
| 57 | > |
| 58 | {options.map((option, index) => { |
| 59 | const selected = option.value === value; |
| 60 | return ( |
| 61 | <button |
| 62 | key={option.value} |
| 63 | ref={(element) => { |
| 64 | refs.current[index] = element; |
| 65 | }} |
| 66 | type="button" |
| 67 | role="radio" |
| 68 | aria-checked={selected} |
| 69 | tabIndex={selected ? 0 : -1} |
| 70 | onClick={() => onValueChange(option.value)} |
| 71 | className={cn( |
| 72 | "relative inline-flex items-center gap-1.5 rounded-[10px] font-medium outline-none transition-colors duration-150 focus-visible:ring-2 focus-visible:ring-ring/50", |
| 73 | size === "sm" ? "h-7 px-2.5 text-xs" : "h-8 px-3 text-sm", |
| 74 | selected ? "text-foreground" : "text-muted-foreground hover:text-foreground", |
| 75 | )} |
| 76 | > |
| 77 | {selected ? ( |
| 78 | <motion.span |
| 79 | layoutId={`${id}-segment`} |
| 80 | transition={transitions.indicator} |
| 81 | className="absolute inset-0 rounded-[10px] bg-background shadow-[0_1px_2px_rgba(0,0,0,0.06),0_0_0_1px_color-mix(in_oklch,var(--border)_70%,transparent)]" |
| 82 | /> |
| 83 | ) : null} |
| 84 | {option.icon ? <span className="relative flex [&_svg]:size-3.5">{option.icon}</span> : null} |
| 85 | <span className="relative">{option.label}</span> |
| 86 | </button> |
| 87 | ); |
| 88 | })} |
| 89 | </div> |
| 90 | ); |
| 91 | } |
| 92 |