Overview
useDisclosure is a hook for managing the visibility state of UI elements like modals, drawers, dropdowns, and other disclosure widgets. It provides a complete API for opening, closing, and toggling visibility, with optional scroll locking when the element is open.
Import
Signature
Parameters
Configuration options for the disclosure behavior.
Whether to lock/unlock page scrolling when the disclosure opens/closes. Useful for modals and drawers.
The initial open state. Can be a boolean or a function that returns a boolean.
Return Value
Returns an object with the following properties:The current open/closed state of the disclosure.
Function to open the disclosure. If
hasScrollControl is enabled, also locks page scrolling.Function to close the disclosure. If
hasScrollControl is enabled, also unlocks page scrolling.Function to toggle the disclosure state:
- When called with no arguments: toggles between open and closed
- When called with a boolean: sets the state to that boolean value
- Manages scroll locking if
hasScrollControlis enabled
Usage
Basic Modal
Modal with Scroll Locking
Dropdown with Toggle
Initially Open State
Notes
- All callback functions (
onOpen,onClose,onToggle) are memoized for stable references - The returned API object is memoized and only updates when
isOpenchanges - When
hasScrollControlis enabled, the hook uses thelockScrollutility from@zayne-labs/toolkit-core - Built on top of the
useTogglehook for state management