Dialog
A modal dialog for a question or a short form, as a compound component on Base UI's dialog. For one question with a typed answer, use PromptDialog.
import { Dialog } from '@adecore/ui';import { Button, Dialog } from '@adecore/ui';
import { useState } from 'react';
export default function DialogDemo() {
const [open, setOpen] = useState(false);
return (
<Dialog.Root open={open} onOpenChange={setOpen}>
<Dialog.Trigger render={<Button variant="danger-outline" />}>Remove branch</Dialog.Trigger>
<Dialog.Popup size="sm">
<Dialog.Title>Remove the branch?</Dialog.Title>
<Dialog.Description className="mt-1">It has two commits nothing else has.</Dialog.Description>
<Dialog.Text size="xs" className="mt-2">
A removed branch can be restored from the reflog for 90 days.
</Dialog.Text>
<Dialog.Footer>
<Dialog.Close render={<Button />}>Cancel</Dialog.Close>
<Button variant="danger" onClick={() => setOpen(false)}>
Remove
</Button>
</Dialog.Footer>
</Dialog.Popup>
</Dialog.Root>
);
}Parts
| Part | What it is |
|---|---|
Dialog.Root | Holds the open state: open and onOpenChange, or defaultOpen. |
Dialog.Trigger | The button that opens it. |
Dialog.Popup | The portal, the backdrop and the popup in one part. |
Dialog.Title | The heading and accessible name. size="lg" for a header rather than a question. |
Dialog.Description | The sentence under the title, read as the dialog's description. No margin. |
Dialog.Text | Another line in the description's style, such as a warning. |
Dialog.Footer | The buttons, main action last (on the right). |
Dialog.Close | A button that closes the dialog. Pass a Button through render. |
Size
size="sm" is 420 pixels wide with 20 pixels of padding, for question dialogs. Without a size, className sets the width and there is no padding.
A dialog over a dialog
A dialog opened over another stacks over it and dims it. nested forces that layering for a dialog that always opens over another. Menus and selects in either dialog float above both.
import { Button, Dialog } from '@adecore/ui';
export default function DialogNested() {
return (
<Dialog.Root>
<Dialog.Trigger render={<Button variant="secondary" />}>Edit profile</Dialog.Trigger>
<Dialog.Popup className="w-[480px] p-5">
<Dialog.Title size="lg">Profile</Dialog.Title>
<Dialog.Description className="mt-1">A dialog opened from inside another one stacks over it and dims it.</Dialog.Description>
<Dialog.Footer>
<Dialog.Root>
<Dialog.Trigger render={<Button variant="danger-outline" />}>Delete account</Dialog.Trigger>
<Dialog.Popup size="sm">
<Dialog.Title>Delete the account?</Dialog.Title>
<Dialog.Description className="mt-1">Everything in it goes with it.</Dialog.Description>
<Dialog.Footer>
<Dialog.Close render={<Button />}>Keep it</Dialog.Close>
<Dialog.Close render={<Button variant="danger" />}>Delete</Dialog.Close>
</Dialog.Footer>
</Dialog.Popup>
</Dialog.Root>
<Dialog.Close render={<Button variant="primary" />}>Done</Dialog.Close>
</Dialog.Footer>
</Dialog.Popup>
</Dialog.Root>
);
}Popup props
Dialog.Popup takes Base UI's popup props, plus:
| Prop | Type | Default | |
|---|---|---|---|
size | 'sm' | ||
nested | boolean | false | Always over another dialog. |
backdrop | boolean | true | The dim behind the dialog. |
backdropClassName | string | Such as lightbox-backdrop, a heavier dim for a picture. | |
keepMounted | boolean | Keeps the popup mounted while closed. |
Dialog.Title takes size 'base' (the default) or 'lg'. Dialog.Description and Dialog.Text take size 'sm' (the default) or 'xs'. Dialog.Text and Dialog.Footer take render.
Keyboard and focus
Focus moves into the dialog and Tab stays inside. Escape and a backdrop click call onOpenChange(false). Closing returns focus to the trigger.
Types
DialogPopupProps, DialogTitleProps, DialogDescriptionProps, DialogTextProps and DialogFooterProps are exported types.