/* ───────────────────────────────────────────────────────────────────────── PixelSwitch — toggle switch with disabled + surface. ───────────────────────────────────────────────────────────────────────── */ import React, { forwardRef } from 'react'; import { Tone, Surface, cn, toneMap, focusRing, surfaceClasses, useEffectiveSurface, } from '../common'; import { useControllableState } from '../hooks/useControllableState'; /** Public prop bag for {@link PixelSwitch}. */ export interface PixelSwitchProps { /** Label rendered next to the switch. */ label: string; /** Controlled checked state. */ checked?: boolean; /** Uncontrolled initial checked state. */ defaultChecked?: boolean; /** Fires with the next checked value when clicked. */ onChange?: (next: boolean) => void; /** Disables interaction + grays out the control. */ disabled?: boolean; /** Visual tone for the "on" state. Default: `'green'`. */ tone?: Tone; /** Surface variant. Inherits from `PxlKitSurfaceProvider` when omitted. */ surface?: Surface; /** Form-serialization name. Hidden mirror sends `'on'` / `''`. */ name?: string; /** HTML form value when checked. Defaults to `'on'`. */ value?: string; /** Marks the field as required for native form validation. */ required?: boolean; /** DOM `id` forwarded to the trigger. */ id?: string; } export const PixelSwitch = forwardRef(function PixelSwitch( { label, checked, defaultChecked, onChange, disabled = false, tone = 'green', surface: surfaceProp, name, value = 'on', required, id, }, ref, ) { const surface = useEffectiveSurface(surfaceProp); const s = surfaceClasses(surface); const [internalChecked, setInternalChecked] = useControllableState({ value: checked, defaultValue: defaultChecked ?? false, onChange, }); const isChecked = internalChecked ?? false; return ( <> {name && isChecked && } ); });