/*! Strand UI | MIT License | dillingerstaffing.com */ import type { JSX } from "preact"; import { forwardRef } from "preact/compat"; import { useState } from "preact/hooks"; import { cx } from "../../internal/index.js"; export interface SwitchProps extends Omit, "checked" | "onChange" | "label" | "type" | "role"> { /** Controlled state; leave unset to let the switch own it. */ checked?: boolean; /** Initial state of an uncontrolled switch. */ defaultChecked?: boolean; onChange?: (checked: boolean) => void; disabled?: boolean; label?: string; /** `compact` drops the row to 30px on a fine pointer only (DL 14.7). */ density?: "comfortable" | "compact"; className?: string; } /** * Binary toggle; aria-checked carries the state and the sheet reads it. * * @example * */ export const Switch = forwardRef( ({ checked, defaultChecked = false, onChange, disabled = false, label, density = "comfortable", className = "", ...rest }, ref) => { const [ownChecked, setOwnChecked] = useState(defaultChecked); const isOn = checked ?? ownChecked; const toggle = () => { if (checked === undefined) setOwnChecked(!isOn); onChange?.(!isOn); }; return ( ); }, ); Switch.displayName = "Switch";