Manage UI overlays (modals, drawers, popovers) and synchronize them with the browser navigation history. Requires a stable ID to ensure states are correctly restored across page unmounts and remounts.
Do not use useOverlay for simple modals or dialogs that you only intend to show once and never reopen via the browser's forward button. Because open() pushes a history entry, a user who closes the dialog with the browser's Back button can immediately reopen it by pressing Forward.
This hook is designed for history-aware overlays — ones where you intentionally want the overlay to be restorable through browser navigation (e.g. multi-step flows, deep-linked modals). For simple one-time dialogs, use plain useState instead.
The id parameter is required. It must be a stable string that uniquely identifies the overlay instance on the page.
const { isOpen, open, close } = useOverlay('page-name:my-modal');
Open the modals below and try navigating using your browser's back button. Even if you navigate to another page and come back, the stable IDs ensure the overlay state is perfectly restored.
Try opening the modals and then use your browser's Back and Forward buttons.
import { useOverlay } from 'kitzo'; import { Dialog, DialogContent } from './components/ui/dialog'; function MyComponent() { // Use page-scoped IDs for maximum reliability const modal = useOverlay('settings:profile-modal'); return ( <> <button onClick={modal.open}>Open Modal</button> <Dialog open={modal.isOpen} onOpenChange={(open) => !open && modal.close()}> <DialogContent> <h2>I am a history-aware modal!</h2> <button onClick={modal.close}>Close</button> </DialogContent> </Dialog> </> ); }
| Parameter | Type | Description |
|---|---|---|
id | string | A stable unique identifier for the overlay. Required for history synchronization. |
| Return Value | Type | Description |
|---|---|---|
isOpen | boolean | Whether the overlay is currently active and visible. |
open | () => void | Opens the overlay by pushing a new entry to the browser history. |
close | () => void | Closes the overlay by triggering a browser back navigation. |
useId) if they might change when the component tree shifts.'login:otp-modal') to avoid collisions with other overlays in the history stack.