'use client' import { Label, Switch as GuiSwitch, Text, VisuallyHidden } from '@hanzo/gui'; import * as React from 'react'; const NOOP = () => { /* noop */ }; // ─── Size mappings ──────────────────────────────────────────────────── // Paint and box size only. Thumb travel is NOT a hardcoded translate any more: // gui measures the track and the knob and moves the knob by the difference, so // the switch stays correct at any size and on native. const SIZE = { sm: { width: 28, height: 16, thumb: 12, label: 12 }, md: { width: 36, height: 20, thumb: 16, label: 14 }, lg: { width: 44, height: 24, thumb: 20, label: 16 }, } as const; const TRACK_ON = 'var(--color-text-primary)' as never; const TRACK_OFF = 'var(--color-switch-bg)' as never; const OUTLINE = 'var(--color-gray-500)' as never; const WHITE = 'var(--color-white)' as never; // ─── Props ──────────────────────────────────────────────────────────── export interface SwitchProps extends Omit, 'onChange' | 'dir'> { inputProps?: React.InputHTMLAttributes; labelProps?: React.ComponentPropsWithoutRef<'span'>; rootRef?: React.Ref; trackLabel?: { on: React.ReactNode; off: React.ReactNode }; thumbLabel?: { on: React.ReactNode; off: React.ReactNode }; checked?: boolean; defaultChecked?: boolean; onCheckedChange?: (checked: boolean) => void; onChange?: React.FormEventHandler; disabled?: boolean; size?: 'sm' | 'md' | 'lg'; direction?: 'ltr' | 'rtl'; } const SwitchBase = React.forwardRef( function Switch(props, ref) { const { inputProps, children, rootRef, labelProps, trackLabel, thumbLabel, checked: checkedProp, defaultChecked, onCheckedChange, onChange, disabled = false, size = 'md', direction, ...rest } = props; const [ internalChecked, setInternalChecked ] = React.useState(defaultChecked ?? false); const isControlled = checkedProp !== undefined; const checkedState = isControlled ? checkedProp : internalChecked; const handleCheckedChange = React.useCallback( (nextChecked: boolean) => { if (!isControlled) { setInternalChecked(nextChecked); } // The boolean itself, which is what the prop's type says and what every // caller here reads. A `{ checked }` payload was the old shape and the // harness was still destructuring one, so its switch read `undefined`. onCheckedChange?.(nextChecked); }, [ isControlled, onCheckedChange ], ); const s = SIZE[size]; const isRtl = direction === 'rtl'; return ( ); }, ); export const Switch = SwitchBase;