useDisclosurestate
useDisclosure
NewSSR SAFEManages boolean open/close state for modals, dialogs, drawers, and collapsibles with memoized action handlers.
$ pnpm dlx shadcn@latest add @byphongphan/use-disclosureRequires
@byphongphan in components.jsonuse-disclosure.tsx
Loading preview...
Installation
$ pnpm dlx shadcn@latest add @byphongphan/use-disclosureRequires
@byphongphan in components.jsonUsage
Controlled Dialog Modal
useDisclosure integrates seamlessly with UI dialogs and modals by binding isOpen to open and handlers.set directly to onOpenChange:
Lifecycle Callbacks
You can track opening and closing actions, initialize resources, or report telemetry using optional onOpen and onClose callbacks:
Props & Parameters
| Param | Type | Default | Description |
|---|---|---|---|
initialState | boolean | false | Initial boolean open/close state. |
callbacks | UseDisclosureCallbacks | — | Optional object containing onOpen and onClose callback events. |
UseDisclosureCallbacks
| Property | Type | Description |
|---|---|---|
onOpen | () => void | Optional callback triggered when state becomes true. |
onClose | () => void | Optional callback triggered when state becomes false. |
Returns
useDisclosure returns a tuple [isOpen, handlers]:
| Return | Type | Description |
|---|---|---|
isOpen | boolean | Current boolean open status. |
handlers | UseDisclosureHandlers | Stable memoized object containing action helper functions. |
UseDisclosureHandlers
| Handler | Type | Description |
|---|---|---|
open | () => void | Sets boolean state to true and triggers onOpen. |
close | () => void | Sets boolean state to false and triggers onClose. |
toggle | () => void | Toggles state and executes the corresponding callback. |
set | (value: boolean) => void | Sets the boolean value directly (ideal for onOpenChange). |