8058
Ir
irvc / files
irvc / web / src / ui / Toggle.tsx
1.2 KB
1import { cn } from "cn"; 2 3// Toggle is a labelled on/off row, used for every boolean preference. 4export function Toggle({ 5 label, 6 hint, 7 checked, 8 disabled, 9 onChange, 10}: { 11 label: string; 12 hint?: string; 13 checked: boolean; 14 disabled?: boolean; 15 onChange: (value: boolean) => void; 16}) { 17 return ( 18 <button 19 type="button" 20 role="switch" 21 aria-checked={checked} 22 disabled={disabled} 23 onClick={() => onChange(!checked)} 24 className="flex w-full items-center justify-between gap-3 rounded-md px-2 py-2 text-left text-sm transition-colors enabled:hover:bg-raised disabled:cursor-not-allowed disabled:opacity-40" 25 > 26 <span className="min-w-0"> 27 <span className="block truncate">{label}</span> 28 {hint && <span className="block text-xs text-faint">{hint}</span>} 29 </span> 30 <span 31 className={cn( 32 "flex h-5 w-9 shrink-0 items-center rounded-full border p-0.5 transition-colors", 33 checked ? "border-accent bg-accent/25" : "border-line bg-surface", 34 )} 35 > 36 <span 37 className={cn( 38 "size-3.5 rounded-full transition-transform", 39 checked ? "translate-x-4 bg-accent" : "bg-faint", 40 )} 41 /> 42 </span> 43 </button> 44 ); 45}