Dialog
A modal window over the page, with a title, a description and actions.
Installation
pnpm dlx @human-kit/design add dialognpx @human-kit/design add dialogyarn dlx @human-kit/design add dialogbunx @human-kit/design add dialogThe command copies the files to src/lib/components/ui/dialog. It also
installs the packages that the files import.
Usage
<script lang="ts">
import { buttonVariants } from '$lib/components/ui/button';
import * as Dialog from '$lib/components/ui/dialog';
</script>
<Dialog.Root>
<Dialog.Trigger class={buttonVariants({ variant: 'outline' })}>Archive</Dialog.Trigger>
<Dialog.Content>
<Dialog.Header>
<Dialog.Title>Archive the cycle?</Dialog.Title>
<Dialog.Description>Its records stay in the history.</Dialog.Description>
</Dialog.Header>
<Dialog.Footer>
<Dialog.Close class={buttonVariants({ variant: 'outline' })}>Cancel</Dialog.Close>
<Dialog.Close class={buttonVariants({})}>Archive</Dialog.Close>
</Dialog.Footer>
</Dialog.Content>
</Dialog.Root> Examples
Confirm a destructive action
A title that asks, a line that says what is lost, and two buttons. The one that destroys says what it destroys and takes intent: 'danger'.
A form
Put the fields in Dialog.Body: it alone scrolls, so the title and the footer with its save button stay on screen. Bind open to close the dialog once the save is done.
Guidelines
- Use a dialog for a short task that must be done or dropped before going on: a confirmation, a small form. For a record to read beside the page, use a drawer.
- Keep it to one task. A dialog that needs tabs or a second dialog wants a page of its own.
- Use
smfor a confirmation,mdorlgfor a form, andxlonly for content that needs the width, such as a table. - End with the action in the footer, the main one last, named with a verb: "Archive", not "OK". On a phone the footer stacks with the main action on top.
- Do not open a dialog the person did not ask for. It takes the whole screen and stops what they were doing.
Accessibility
- The content is a
role="dialog"witharia-modal, named byDialog.Titleand described byDialog.Description. Always give it a title. - While open the focus stays inside it, the page behind is hidden from a screen reader and does not scroll.
- The focus goes to the first control when it opens, and back to the trigger when it closes. The close button comes last in the code for that reason, though it sits in the corner.
- Escape and a press outside close the topmost dialog only. A popover open inside it closes first.
- The close button is named by
closeLabel, "Close" unless you give another word.
API reference
Dialog.Root
| Prop | Type | Default |
|---|---|---|
open | boolean Whether the dialog shows. Bindable. | — |
defaultOpen | boolean Whether it starts open, when no | false |
onOpenChange | (open: boolean) => void Runs when it opens or closes. | — |
controlledOpen | boolean Stops it writing back to | false |
triggerRef | HTMLElement | null The element the focus goes back to on close. | — |
children | Snippet<[{ open, close, toggle, isOpen }]> The parts, with the functions that open and close the dialog. | — |
It draws nothing of its own.
Dialog.Trigger
| Prop | Type | Default |
|---|---|---|
element | HTMLButtonElement | null The rendered button. Bindable. | — |
Plus every attribute of a native <button> but type. It has no look: give it the classes of a button, buttonVariants({ variant: 'outline' }).
Dialog.Content
| Prop | Type | Default |
|---|---|---|
size | 'sm' | 'md' | 'lg' | 'xl' How wide the panel gets. Under that width it fills the screen less 1rem a side. | 'sm' |
showCloseButton | boolean Shows a close button in the top corner. | true |
closeLabel | string The name of the close button, read aloud. | 'Close' |
shouldCloseOnInteractOutside | boolean Closes the dialog on a press outside it. Turn it off for a form that would lose its work. | true |
shouldCloseOnEscape | boolean Closes the dialog with Escape. | true |
Plus every attribute of a <div>. It brings its own portal and the curtain over the page.
Dialog.Header
| Prop | Type | Default |
|---|---|---|
class | string Classes merged onto the header. | — |
Plus every attribute of a <div>. It holds the title and the description, and keeps room for the close button.
Dialog.Body
| Prop | Type | Default |
|---|---|---|
class | string Classes merged onto the body. | — |
Plus every attribute of a <div>. The only part that scrolls.
Dialog.Footer
| Prop | Type | Default |
|---|---|---|
class | string Classes merged onto the footer. | — |
Plus every attribute of a <div>. An empty footer is not drawn.
Dialog.Title
| Prop | Type | Default |
|---|---|---|
class | string Classes merged onto the title. | — |
Plus every attribute of the <h2> it renders. It names the dialog.
Dialog.Description
| Prop | Type | Default |
|---|---|---|
class | string Classes merged onto the text. | — |
Plus every attribute of the <p> it renders. It describes the dialog.
Dialog.Close
| Prop | Type | Default |
|---|---|---|
element | HTMLButtonElement | null The rendered button. Bindable. | — |
Plus every attribute of a native <button> but type. It closes the dialog it is in. It has no look: give it the classes of a button.