Skip to content

Dialog

A window that opens over the page for a decision or a short task that needs the person now. The page behind waits, dimmed, until it closes.

import { Dialog } from '@sypnia/ui/react'

On this page
  1. A short task
  2. Sizes
  3. Does not lose what you wrote
  4. Long content
  5. Loading
  6. When to use it
  7. For everyone
  8. Props

A short task

Rename the project: Enter saves, Escape leaves it as it was.

The project is called Storefront.
Code
import { useState } from 'react';
import { Actions, Button, Dialog, Field, Input } from '@sypnia/ui/react';

// Press the button. Escape, the cross or a press outside close it, and the focus returns to the button.
export default function Basic() {
  const [open, setOpen] = useState(false);
  const [name, setName] = useState('Storefront');
  const [draft, setDraft] = useState(name);
  const save = () => { setName(draft.trim() || name); setOpen(false); };
  return (
    <div className="sy-row">
      <Button icon="edit" onClick={() => { setDraft(name); setOpen(true); }}>Rename project</Button>
      <span className="sy-text--sm sy-text--soft">The project is called {name}.</span>
      <Dialog open={open} onClose={() => setOpen(false)} title="Rename the project"
        footer={<Actions><Button onClick={() => setOpen(false)}>Cancel</Button><Button variant="primary" onClick={save}>Rename</Button></Actions>}>
        <Field label="Name" hint="Members see the new name at once.">
          <Input autoFocus value={draft} onChange={(e) => setDraft(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') save(); }} />
        </Field>
      </Dialog>
    </div>
  );
}

Sizes

Four widths. On a phone they are all the same: the full width.

Code
import { useState } from 'react';
import { Actions, Button, Dialog } from '@sypnia/ui/react';

const sizes = { sm: 'A short question', md: 'A short form', lg: 'A form with two columns', xl: 'A preview or a comparison' } as const;
export default function Sizes() {
  const [size, setSize] = useState<keyof typeof sizes | null>(null);
  return (
    <div className="sy-row sy-row--pairs">
      {(Object.keys(sizes) as (keyof typeof sizes)[]).map((s) => <Button key={s} onClick={() => setSize(s)}>Open {s}</Button>)}
      <Dialog open={size !== null} size={size ?? 'md'} onClose={() => setSize(null)} title={size ? sizes[size] : ''}
        footer={<Actions><Button variant="primary" onClick={() => setSize(null)}>Done</Button></Actions>}>
        <p>This dialog has the size <code>{size}</code>. On a phone every size takes the full width and sits at the bottom, near the thumb.</p>
      </Dialog>
    </div>
  );
}

Does not lose what you wrote

Change the message, then press outside or Escape: it stays open and says how to close. Without changes, a press outside closes it.

Code
import { useState } from 'react';
import { Actions, Button, Dialog, Field, Textarea } from '@sypnia/ui/react';

// Pass `dirty` while there are unsaved changes: a press outside or Escape keeps the text and says how to close.
// Without changes, a press outside closes it like any dialog.
const start = 'Hello Laura, your order left our warehouse this morning.';
export default function KeepsWork() {
  const [open, setOpen] = useState(false);
  const [text, setText] = useState(start);
  const close = () => { setOpen(false); setText(start); };
  return (
    <>
      <Button icon="mail" onClick={() => setOpen(true)}>Write to the customer</Button>
      <Dialog open={open} dirty={text !== start} onClose={close} title="Message about order 1042"
        footer={<Actions><Button onClick={close}>Cancel</Button><Button variant="primary" onClick={close}>Send message</Button></Actions>}>
        <Field label="Message" hint="The customer receives it by email.">
          <Textarea autoFocus value={text} onChange={(e) => setText(e.target.value)} />
        </Field>
      </Dialog>
    </>
  );
}

Long content

The list scrolls; the title and the buttons stay in view.

Code
import { useState } from 'react';
import { Actions, Button, Checkbox, Dialog } from '@sypnia/ui/react';

const tables = ['customers', 'invoices', 'invoice_lines', 'order_items', 'orders', 'payments', 'products', 'product_images', 'refunds', 'reviews', 'shipments', 'stock_levels', 'suppliers', 'warehouses'];
// When the content is long, the body scrolls and the title and the buttons stay in view.
export default function LongContent() {
  const [open, setOpen] = useState(false);
  return (
    <>
      <Button icon="download" onClick={() => setOpen(true)}>Export tables</Button>
      <Dialog open={open} onClose={() => setOpen(false)} title="Which tables do you want to export?"
        footer={<Actions><Button onClick={() => setOpen(false)}>Cancel</Button><Button variant="primary" onClick={() => setOpen(false)}>Export as CSV</Button></Actions>}>
        {tables.map((t) => <Checkbox key={t} label={<span className="sy-machine">{t}</span>} defaultChecked={t.startsWith('order')} />)}
      </Dialog>
    </>
  );
}

Loading

Open it: the title and the button are there at once; the body waits as lines of skeleton.

Code
import { useState } from 'react';
import { Actions, Button, Dialog } from '@sypnia/ui/react';

// The dialog opens at once with its title and buttons; its body is a skeleton until the details arrive.
export default function Loading() {
  const [open, setOpen] = useState(false);
  const [loading, setLoading] = useState(true);
  const show = () => { setOpen(true); setLoading(true); setTimeout(() => setLoading(false), 1500); };
  return (
    <>
      <Button onClick={show}>Show the table's details</Button>
      <Dialog open={open} onClose={() => setOpen(false)} title="Table orders" loading={loading} lines={3}
        footer={<Actions><Button variant="primary" onClick={() => setOpen(false)}>Done</Button></Actions>}>
        <p>48,210 rows, 12 columns. Two policies decide who reads it; nobody outside the team can write to it.</p>
      </Dialog>
    </>
  );
}

When to use it

Use it when

  • A short task that belongs to the page behind: rename, invite, choose what to export.
  • The person must answer before continuing.
  • Closing by accident would lose work: set dismissable={false}, so only the buttons close it.

Do not use it when

  • It only asks «are you sure?»: ConfirmDialog does that with the right focus and wording.
  • The task is long or has many fields: a Drawer, or a page of its own.
  • It reports a result («Saved»): a toast. It warns without asking anything: an Alert.
  • It would open by itself, without the person pressing anything.

For everyone

  • It is drawn in the top layer of the window, never inside the page, and the page behind cannot be scrolled or reached with Tab.
  • Focus moves inside when it opens (to the first field, or the one marked autoFocus) and returns to the button that opened it when it closes.
  • A press on the backdrop closes it, and so do Escape and the cross. A drag that starts inside (selecting text) and ends outside does not. With dirty (unsaved changes) a press outside or Escape keeps the work and says how to close; dismissable={false} turns the backdrop off. The cross is named «Close».
  • It is announced as a dialog with its title. On phones it sits at the bottom edge, full width.

Props

DialogTypeDefault
onCloserequired () => void
titlerequired ReactNode
dirty

The form inside has changes that are not saved. A press outside or Escape then does not close it and does not lose them: the panel nudges and says to save or cancel. Only the buttons close it.

boolean
dismissable

Whether a press on the backdrop (outside the panel) closes it. On by default; Escape always closes it too. Set false for a form that must be finished or cancelled with its buttons.

boolean
footer

Buttons at the bottom, inside <Actions>.

ReactNode
lines

How many lines of skeleton the body shows while loading.

number
loading

What it shows is on its way: the title and the buttons stay, the body is lines of skeleton.

boolean
open

Whether it is open. It can also simply be mounted and unmounted: it is open while mounted.

boolean
size "sm" | "md" | "lg" | "xl" md
  1. A short task
  2. Sizes
  3. Does not lose what you wrote
  4. Long content
  5. Loading
  6. When to use it
  7. For everyone
  8. Props

    Search pages, sections and terms