Skip to content

Dialog

A modal window for a short task or a confirmation.

import { Dialog } from '@/design-system';

Examples

Render it to open it, and stop rendering it to close it.

'use client';

import { useState } from 'react';
import { Button, Dialog, Field, Input } from '@/design-system';

export default function DialogBasic() {
  const [open, setOpen] = useState(false);
  const close = () => setOpen(false);

  return (
    <>
      <Button onClick={() => setOpen(true)}>Add customer</Button>
      {open && (
        <Dialog title="Add customer" onClose={close}>
          <Field label="Company name">
            <Input data-autofocus />
          </Field>
          <div className="mt-6 flex justify-end gap-3">
            <Button variant="secondary" onClick={close}>
              Cancel
            </Button>
            <Button onClick={close}>Add</Button>
          </div>
        </Dialog>
      )}
    </>
  );
}

How it works

  • It uses the native dialog element as a modal, so the rest of the page cannot be used while it is open.
  • Focus goes to the element marked data-autofocus. Without one, the browser chooses, usually the close button.
  • When it closes, focus returns to the element that had it before.
  • Clicking outside the dialog does not close it.

Props

Dialog takes only these props.

Dialog props
PropTypeDefaultDescription
titlestringrequiredThe heading, which also names the dialog.
onClose() => voidrequiredCalled by the close button and the Escape key. Stop rendering the dialog here.
childrenReactNoderequiredThe content, including any action buttons.

Tokens

Tokens
TokenDefaultSets
--dialog-title-size1.125remTitle text size
--dialog-radius--radius-controlCorner radius
--dialog-width28remWidest it gets
--dialog-padding1.5remPadding
--dialog-background--surfaceFill
--dialog-backdrophalf-transparent blackShade behind it