createDisclosureState
Track open state, content height, and data-attribute getters for collapsible panels.
createDisclosureState derives the state of an expandable panel from an open accessor: presence through the exit animation, measured content height, data attributes, and hidden and inert flags. Collapsible and Accordion use it. It does not own the open state; pair it with createControllableValue or your own signal.
Import#
Signature#
Usage Example#
Register elements from the component that renders them, not inside the ref callback, so the
registration is released when the element unmounts.
src/MyCollapsible.tsx
Features#
- Attaches a
ResizeObserverto the measured element and reports itsscrollHeightthroughcontentHeight(), remeasuring when the element resizes and whenopenchanges. - Exposes
data-expanded,data-closed, anddata-disabledas getters, so each attribute tracks its own condition when you spreaddataAttrs. - Keeps the content shell mounted through its exit animation (
shouldMount(),exiting()) unlesstransitionisfalse, and always mounts it withforceMount. - Returns focus to the trigger (set with
setTriggerElement) when the panel closes while focus is inside the content shell (registered withregisterContentElement). - With
onBeforeMatch, kept-mounted closed content useshidden="until-found"so browser find-in-page can reveal it.