Skip to content
Porcelana

Dialog

Modal dialog that becomes a bottom sheet on phones.

Example

Example.tsx
import { Button, Dialog, DialogContent, DialogDescription, DialogFooter, DialogTitle, DialogTrigger, Field, Input } from "@porcelana/ui";

export default function Example() {
  return (
    <Dialog>
      <DialogTrigger asChild>
        <Button>Edit alarm</Button>
      </DialogTrigger>
      <DialogContent>
        <DialogTitle>Edit alarm</DialogTitle>
        <DialogDescription>Changes apply to every device.</DialogDescription>
        <Field label="Label">
          <Input defaultValue="Morning run" />
        </Field>
        <DialogFooter>
          <Button>Save</Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
}

Import

Import
import { DialogContent, Dialog } from "@porcelana/ui";
import "@porcelana/ui/styles.css"; // once, at your app root

Props

Generated from the TypeScript types. Standard HTML attributes are passed through and not listed.

DialogContent

Centered modal on desktop; bottom sheet under 640px. Focus is trapped and restored.

PropTypeDefaultDescription
aria-labelstring–Defines a string value that labels the current element. @see aria-labelledby.
asChildboolean–Render the single child element instead, merging props and behaviour onto it.
centeredboolean–Keep the centered modal on phones instead of the bottom sheet.
closeLabelstringCloseAccessible label for the close button.
deferPointerDownOutsideboolean–When `true`, a `'pointerdown'` event outside of the layered element will wait for the interaction's click event before dispatching, allowing third-party code to stop propagation of later events and cancel dismissal.
forceMounttrue–Used to force mounting when more control is needed. Useful when controlling animation with React animation libraries.
idstring–Element id. Generated automatically when omitted.
onCloseAutoFocus((event: Event) => void)–Event handler called when auto-focusing on close. Can be prevented.
onEscapeKeyDown((event: KeyboardEvent) => void)–Event handler called when the escape key is down. Can be prevented.
onFocusOutside((event: FocusOutsideEvent) => void)–Event handler called when the focus moves outside of the `DismissableLayer`. Can be prevented.
onInteractOutside((event: PointerDownOutsideEvent | FocusOutsideEvent) => void)–Event handler called when an interaction happens outside the `DismissableLayer`. Specifically, when a `pointerdown` event happens outside or focus moves outside of it. Can be prevented.
onOpenAutoFocus((event: Event) => void)–Event handler called when auto-focusing on open. Can be prevented.
onPointerDownOutside((event: PointerDownOutsideEvent) => void)–Event handler called when the a `pointerdown` event happens outside of the `DismissableLayer`. Can be prevented.
showClosebooleantrueShow the × close button. Defaults to true.

Dialog

PropTypeDefaultDescription
defaultOpenboolean–Initial open state when uncontrolled.
modalboolean–Block interaction with the rest of the page while open.
onOpenChange((open: boolean) => void)–Called when the open state changes.
openboolean–Controlled open state. Use with onOpenChange.

Accessibility

  • Focus is trapped inside and returns to the trigger on close; Escape closes.
  • DialogTitle names it and DialogDescription describes it.
  • The × button has a translatable label (closeLabel).

Mobile

  • Under 640px it slides up as a bottom sheet with a grab handle, and footer buttons stack full-width.
  • Pass centered to keep the modal layout.