56 lines
1.8 KiB
TypeScript
56 lines
1.8 KiB
TypeScript
import { cva } from 'class-variance-authority';
|
|
import { cn } from '~/lib/utils';
|
|
|
|
const optionVariants = cva('flex cursor-pointer items-center gap-2 rounded-option font-medium transition-colors', {
|
|
variants: {
|
|
size: {
|
|
sm: 'h-7 px-3 text-xs',
|
|
md: 'h-[30px] px-3.5 text-sm',
|
|
},
|
|
selected: {
|
|
true: 'bg-surface-pressed text-text-primary',
|
|
false: 'text-text-subtle hover:text-text-primary',
|
|
},
|
|
},
|
|
});
|
|
|
|
export interface SegmentedOption<Value extends string> {
|
|
value: Value;
|
|
label: string;
|
|
}
|
|
|
|
interface SegmentedControlProps<Value extends string> {
|
|
value: Value;
|
|
options: readonly SegmentedOption<Value>[];
|
|
onChange: (value: Value) => void;
|
|
label?: string;
|
|
size?: 'sm' | 'md';
|
|
surface?: 'raised' | 'sunken';
|
|
}
|
|
|
|
export function SegmentedControl<Value extends string>({ value, options, onChange, label, size = 'md', surface = 'raised' }: SegmentedControlProps<Value>) {
|
|
return (
|
|
<div
|
|
role='radiogroup'
|
|
aria-label={label}
|
|
className={cn(
|
|
'flex w-fit items-center self-start rounded-group p-[3px] ring-1 ring-inset',
|
|
surface === 'raised' ? 'bg-surface-raised ring-border-subtle' : 'bg-background ring-border-control',
|
|
)}
|
|
>
|
|
{options.map((option) => (
|
|
<button
|
|
key={option.value}
|
|
type='button'
|
|
role='radio'
|
|
aria-checked={value === option.value}
|
|
onClick={() => onChange(option.value)}
|
|
className={optionVariants({ size, selected: value === option.value })}
|
|
>
|
|
{option.label}
|
|
</button>
|
|
))}
|
|
</div>
|
|
);
|
|
}
|