import { type Accessor, createContext, useContext } from "solid-js"; import type { FieldRootState } from "../internals/field-constants"; /** * Shape of the state surface exposed by `` to its compound parts * (currently just ``). Mirrors Base UI React's * `SwitchRootState` — it extends the shared `FieldRootState` (valid / touched * / dirty / filled / focused / disabled) with the switch-specific flags. */ export interface SwitchRootState extends FieldRootState { /** Whether the switch is currently active. */ checked: boolean; /** Whether the component should ignore user interaction. */ disabled: boolean; /** Whether the user should be unable to activate or deactivate the switch. */ readOnly: boolean; /** Whether the user must activate the switch before submitting a form. */ required: boolean; } /** * The value handed down by `` to its descendants. * * Solid 2 adaptations vs. React: * * - Reactive reads are exposed as `Accessor` so descendants never perform a * top-level reactive read on the context object itself (RFC 01). * - `state` is also an accessor so `` can drive its own * `data-*` projection through `renderElement` without re-deriving. */ export interface SwitchRootContextValue { /** Current checked state. */ checked: Accessor; /** Whether the switch ignores user interaction. */ disabled: Accessor; /** Whether the switch is read-only (focusable but not activatable). */ readOnly: Accessor; /** Whether the switch is required for form submission. */ required: Accessor; /** Composite state snapshot — drives compound-part `data-*` projection. */ state: Accessor; } /** * Switch root context. `createContext(null)` + `== null` sentinel * — Solid 2's `useContext` throws `ContextNotFoundError` when * the default is `undefined`, which would shadow the intended Base UI error * message below. */ export const SwitchRootContext = createContext( null, ); /** * Read the `` context. Throws when the caller forgot to wrap * the subtree in ``. */ export function useSwitchRootContext(): SwitchRootContextValue { const context = useContext(SwitchRootContext); if (context == null) { throw new Error( "Base UI: SwitchRootContext is missing. Switch parts must be placed within .", ); } return context; }