useDisclosurestate

useDisclosure

NewSSR SAFE

Manages boolean open/close state for modals, dialogs, drawers, and collapsibles with memoized action handlers.

$ pnpm dlx shadcn@latest add @byphongphan/use-disclosure
Requires @byphongphan in components.json
state
Loading preview...

Installation

$ pnpm dlx shadcn@latest add @byphongphan/use-disclosure
Requires @byphongphan in components.json

Usage

Controlled Dialog Modal

useDisclosure integrates seamlessly with UI dialogs and modals by binding isOpen to open and handlers.set directly to onOpenChange:

import { useDisclosure } from "@/shared/hooks"import {  Dialog,  DialogContent,  DialogDescription,  DialogFooter,  DialogHeader,  DialogTitle,} from "@/registry/ui/dialog"import { Button } from "@/registry/ui/button"
export function DeploymentModal() { const [isOpen, handlers] = useDisclosure(false)
return ( <> <Button onClick={handlers.open}>Open Modal</Button>
<Dialog open={isOpen} onOpenChange={handlers.set}> <DialogContent> <DialogHeader> <DialogTitle>Confirm Deployment</DialogTitle> <DialogDescription> Are you sure you want to deploy to production? </DialogDescription> </DialogHeader>
<DialogFooter> <Button variant="outline" onClick={handlers.close}> Cancel </Button> <Button onClick={handlers.close}> Confirm </Button> </DialogFooter> </DialogContent> </Dialog> </> )}

Lifecycle Callbacks

You can track opening and closing actions, initialize resources, or report telemetry using optional onOpen and onClose callbacks:

import { useDisclosure } from "@/shared/hooks"
export function TrackedDrawer() { const [isOpen, { open, close, toggle }] = useDisclosure(false, { onOpen: () => { console.log("Analytics: Drawer opened") }, onClose: () => { console.log("Analytics: Drawer closed") }, })
return ( <div> <button onClick={toggle}>Toggle Drawer</button> {isOpen && <aside>Drawer Content</aside>} </div> )}

Props & Parameters

ParamTypeDefaultDescription
initialStatebooleanfalseInitial boolean open/close state.
callbacksUseDisclosureCallbacks—Optional object containing onOpen and onClose callback events.

UseDisclosureCallbacks

PropertyTypeDescription
onOpen() => voidOptional callback triggered when state becomes true.
onClose() => voidOptional callback triggered when state becomes false.

Returns

useDisclosure returns a tuple [isOpen, handlers]:

ReturnTypeDescription
isOpenbooleanCurrent boolean open status.
handlersUseDisclosureHandlersStable memoized object containing action helper functions.

UseDisclosureHandlers

HandlerTypeDescription
open() => voidSets boolean state to true and triggers onOpen.
close() => voidSets boolean state to false and triggers onClose.
toggle() => voidToggles state and executes the corresponding callback.
set(value: boolean) => voidSets the boolean value directly (ideal for onOpenChange).

Command Palette

Quick search and commands navigation