Skip to main content
196

Search Lumen

Find components, APIs, guides, and recipes.

GitHub
Web docs
Overlays and menus

useDialog

Manages modal and alert dialog lifecycle — open, close, focus trapping, backdrop click dismiss, and Escape key handling. Spreads props onto a <dialog> element.

Live component preview · Astro

Explore Dialog

Try the component this hook connects to. The preview uses the Astro adapter; the React example below uses useDialog.

Edit profile

React usage

Import from @santi020k/lumen-react and spread the controller props onto the matching components.

tsxuseDialog example
import { Button, Dialog, Card } from '@santi020k/lumen-react'
import { useDialog } from '@santi020k/lumen-react'

export function ConfirmDialog() {
  const dialog = useDialog()

  return (
    <>
      <Button {...dialog.triggerProps}>Open dialog</Button>
      <Dialog {...dialog.dialogProps}>
        <Card>
          <h2>Confirm action</h2>
          <p>Are you sure you want to continue?</p>
          <Button {...dialog.closeProps}>Close</Button>
        </Card>
      </Dialog>
    </>
  )
}
import { Button, Dialog, Card } from '@santi020k/lumen-react'
import { useDialog } from '@santi020k/lumen-react'

export function ConfirmDialog() {
  const dialog = useDialog()

  return (
    <>
      <Button {...dialog.triggerProps}>Open dialog</Button>
      <Dialog {...dialog.dialogProps}>
        <Card>
          <h2>Confirm action</h2>
          <p>Are you sure you want to continue?</p>
          <Button {...dialog.closeProps}>Close</Button>
        </Card>
      </Dialog>
    </>
  )
}

Load @santi020k/lumen-react/styles.css once in your application. With React Server Components, put 'use client' at the top of the module that calls the hook. Follow the React setup guide for the framework boundary.

Options

PropertyTypeDefaultDescription
alertbooleanfalseRender as an alert dialog that blocks backdrop-click dismiss.
defaultOpenbooleanfalseInitial open state for uncontrolled usage.
openboolean-Controlled open state. When provided the hook does not manage open internally.
onOpenChange(open: boolean) => void-Callback fired when the open state changes.
idstringautoExplicit id for the dialog element.

Controller return values

PropertyTypeDescription
dialogPropsLumenProps<"dialog">Spread onto the <dialog> element. Includes aria-modal, data attributes, click and keydown handlers.
triggerPropsLumenProps<"button">Spread onto the trigger button. Binds the click handler that opens the dialog.
closePropsLumenProps<"button">Spread onto any close button inside the dialog.
dialogRefRefObject<HTMLDialogElement>Ref to the dialog DOM element.
triggerRefRefObject<HTMLElement>Ref to the trigger DOM element.
openbooleanCurrent open state.
close() => voidImperatively close the dialog.
setOpenDispatch<SetStateAction<boolean>>State setter for controlled usage.
Web docsFull catalog