import { type Accessor, createContext, useContext } from "solid-js"; /** * Context value published by ``. * * `` exposes: * * - `disabled` — reactive bit that descendant control parts merge with * their own `disabled` prop (matches React Base UI). * - `legendId` / `setLegendId` — the `` id registration * pair, so the root can wire `aria-labelledby`. These mirror the * `useRegisteredLabelId` pattern used across other tier-1 roots * (progress / meter) and are `undefined` on the NOOP context value. * * Mirrors Base UI React's * `packages/react/src/fieldset/root/FieldsetRootContext.ts`. */ export interface FieldsetContextValue { disabled: Accessor; /** Marker — `true` when this context was provided by a real ``. */ readonly present: boolean; /** * Current registered legend id (if any). Only meaningful when `present` * is `true`; the NOOP context leaves this unset. */ legendId?: Accessor; /** * Register the legend id so the root can wire `aria-labelledby`. Called * by `` on mount (with the id) and on cleanup (with * `undefined`). Safe to invoke from within owned scope — the underlying * signal in the root opts into `ownedWrite` (pure internal plumbing). * Only provided when `present` is `true`. */ setLegendId?: (id: string | undefined) => void; } export const NOOP_FIELDSET_CONTEXT: FieldsetContextValue = { disabled: () => false, present: false, }; /** * Context carrying `` state. Provided via * `` (Solid 2's context-as-provider pattern). */ export const FieldsetContext = createContext( NOOP_FIELDSET_CONTEXT, ); /** * Read the current `` context. Returns {@link NOOP_FIELDSET_CONTEXT} * when no fieldset is present — callers can branch on `.present` if they * need to know whether a real fieldset wraps them. */ export function useFieldsetContext(): FieldsetContextValue { return useContext(FieldsetContext); }