useDisclosure
useDisclosure is a hook that provides all the tools you need to create a disclosure widget. Disclosure widgets are used to show or hide content. This hook provides the state and functions to control the visibility of the content.
useDisclosure returns an object with isOpen, onOpen, onClose, and onOpenChange along with an isControlled flag. It supports both controlled mode (pass isOpen via props) and uncontrolled mode (uses internal state with an optional defaultOpen). Callbacks for onOpen, onClose, and onChange can be provided in the props to run side effects when the state transitions.
When to Use
- Managing modal, dialog, or drawer open/close state with lifecycle callbacks
- Building collapsible panels, accordions, or expandable sections
- Any disclosure pattern where you need both controlled and uncontrolled support with open/close event hooks
Notes
- Controlled and uncontrolled: Pass
isOpenin props for controlled mode. Omit it (or leave itundefined) for uncontrolled mode with optionaldefaultOpen. - Callback support: Unlike simpler boolean hooks,
useDisclosureacceptsonOpen,onClose, andonChangecallbacks, making it well-suited for side effects like analytics or focus management. - See also
useBooleanfor a simpler boolean state hook without callbacks, anduseTogglefor a minimal tuple-based toggle.
Usage
Live Editor
function Demo() { const { isOpen, onOpen, onClose } = useDisclosure(); return ( <> <button onClick={onOpen}>Open</button> <button onClick={onClose}>Close</button> <p>{isOpen ? "Open" : "Close"}</p> </> ); };
Result
API
UseDisclosureProps
| Property | Description | Type | DefaultValue |
|---|---|---|---|
| isOpen | Whether the disclosure is open, if passed, it will be controlled | boolean | - |
| defaultOpen | default open state | boolean | - |
| onClose | Callback when disclosure is closed | () => void | - |
| onOpen | Callback when disclosure is opened | () => void | - |
| onChange | Callback when disclosure is changed | (isOpen: boolean | undefined) => void | - |
useDisclosure
Returns
{ isOpen: boolean; onOpen: () => void; onClose: () => void; onOpenChange: () => void; isControlled: boolean; }
Arguments
| Argument | Description | Type | DefaultValue |
|---|---|---|---|
| props | - | UseDisclosureProps | undefined | - |