Skip to the content
human-kit design
31 — Overlays

Dialog

A modal window over the page, with a title, a description and actions.

Installation

pnpm dlx @human-kit/design add dialog
npx @human-kit/design add dialog
yarn dlx @human-kit/design add dialog
bunx @human-kit/design add dialog

The 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 sm for a confirmation, md or lg for a form, and xl only 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" with aria-modal, named by Dialog.Title and described by Dialog.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

PropTypeDefault
openboolean

Whether the dialog shows. Bindable.

—
defaultOpenboolean

Whether it starts open, when no open is bound.

false
onOpenChange(open: boolean) => void

Runs when it opens or closes.

—
controlledOpenboolean

Stops it writing back to open: it only reports through onOpenChange, so your code can refuse a change.

false
triggerRefHTMLElement | null

The element the focus goes back to on close. Dialog.Trigger sets it; set it yourself for a dialog opened from code. Bindable.

—
childrenSnippet<[{ open, close, toggle, isOpen }]>

The parts, with the functions that open and close the dialog.

—

It draws nothing of its own.

Dialog.Trigger

PropTypeDefault
elementHTMLButtonElement | 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

PropTypeDefault
size'sm' | 'md' | 'lg' | 'xl'

How wide the panel gets. Under that width it fills the screen less 1rem a side.

'sm'
showCloseButtonboolean

Shows a close button in the top corner.

true
closeLabelstring

The name of the close button, read aloud.

'Close'
shouldCloseOnInteractOutsideboolean

Closes the dialog on a press outside it. Turn it off for a form that would lose its work.

true
shouldCloseOnEscapeboolean

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

PropTypeDefault
classstring

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

PropTypeDefault
classstring

Classes merged onto the body.

—

Plus every attribute of a <div>. The only part that scrolls.

PropTypeDefault
classstring

Classes merged onto the footer.

—

Plus every attribute of a <div>. An empty footer is not drawn.

Dialog.Title

PropTypeDefault
classstring

Classes merged onto the title.

—

Plus every attribute of the <h2> it renders. It names the dialog.

Dialog.Description

PropTypeDefault
classstring

Classes merged onto the text.

—

Plus every attribute of the <p> it renders. It describes the dialog.

Dialog.Close

PropTypeDefault
elementHTMLButtonElement | 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.