ConfirmDialog
Asks before doing something that matters. It names the thing, says what will happen, and its button says exactly what it does. For the most destructive actions the person types the name of the thing first.
import { ConfirmDialog } from '@sypnia/ui/react'
Something that cannot be undone
The red button says what it deletes. Focus starts on «Cancel».
Code
import { useState } from 'react';
import { Button, ConfirmDialog, toast, Toaster } from '@sypnia/ui/react';
// The focus starts on «Cancel»: pressing Enter by reflex never deletes anything.
export default function Simple() {
const [open, setOpen] = useState(false);
return (
<>
<Button variant="danger-quiet" icon="trash" onClick={() => setOpen(true)}>Delete row</Button>
<ConfirmDialog open={open} onClose={() => setOpen(false)} title="Delete the order 1042?" action="Delete order"
onConfirm={() => { setOpen(false); toast('Order 1042 deleted'); }}>
The order and its 3 items are removed from the table. This cannot be undone.
</ConfirmDialog>
<Toaster />
</>
);
} Something that matters but can be undone
The button carries the light, not the red, and shows that it is working.
Code
import { useState } from 'react';
import { Button, ConfirmDialog, toast, Toaster } from '@sypnia/ui/react';
// For what matters but can be undone: the button carries the light instead of the red.
export default function Primary() {
const [open, setOpen] = useState(false);
const [busy, setBusy] = useState(false);
const publish = () => { setBusy(true); setTimeout(() => { setBusy(false); setOpen(false); toast('Access rules published'); }, 1600); };
return (
<>
<Button variant="primary" onClick={() => setOpen(true)}>Publish rules</Button>
<ConfirmDialog tone="primary" open={open} busy={busy} onClose={() => setOpen(false)} onConfirm={publish}
title="Publish the new access rules?" action={busy ? 'Publishing…' : 'Publish rules'}>
They apply to every request from this moment. You can go back to the previous rules from the history.
</ConfirmDialog>
<Toaster />
</>
);
} Typing the name
Type storefront: only then does «Delete project» wake up.
Code
import { useState } from 'react';
import { Button, ConfirmDialog, toast, Toaster } from '@sypnia/ui/react';
// For the most destructive actions: the button wakes up only when the name is typed exactly.
export default function TypedWord() {
const [open, setOpen] = useState(false);
const [busy, setBusy] = useState(false);
const remove = () => { setBusy(true); setTimeout(() => { setBusy(false); setOpen(false); toast('The project storefront was deleted'); }, 1600); };
return (
<>
<Button variant="danger" icon="trash" onClick={() => setOpen(true)}>Delete project</Button>
<ConfirmDialog open={open} busy={busy} onClose={() => setOpen(false)} onConfirm={remove} confirmWord="storefront"
title="Delete the project Storefront?" action={busy ? 'Deleting…' : 'Delete project'}>
Its 5 tables, 5,552 rows, files and API keys are removed at once, and apps that use them stop working. This cannot be undone.
</ConfirmDialog>
<Toaster />
</>
);
} When to use it
Use it when
- Before anything that cannot be undone: delete, revoke, remove a member.
- Before something that can be undone but reaches many people: publish, send. Use
tone="primary". - Deleting something large (a project, a database): add
confirmWordwith its name.
Do not use it when
- The action is easy to undo: do it, and offer «Undo» in a toast.
- The question would be «Are you sure?» with «Yes» and «No». Name the thing in the title and the action on the button.
- It collects information: that is a Dialog with a form.
For everyone
- It is an alert dialog: screen readers read the title and the explanation as soon as it opens.
- Focus starts on «Cancel», so Enter pressed by reflex never confirms. With a typed word, focus starts in the field and Enter confirms only once the word matches.
- While
busy, the action cannot fire twice and the dialog cannot be dismissed. - Escape cancels. A press outside does nothing: a decision is made with a button, never by accident. Focus returns to the button that opened it.
Props
| ConfirmDialog | Type | Default |
|---|---|---|
actionrequiredThe confirm button says exactly what it does: «Delete table», never «OK». | string | |
onCloserequired | () => void | |
onConfirmrequired | () => void | |
titlerequiredThe question, naming the thing: «Delete the table orders?» | ReactNode | |
busy | boolean | |
childrenWhat will happen, in plain words, and whether it can be undone. | ReactNode | |
confirmWordFor the most destructive actions: the person must type this word (usually the name of the thing) to enable the button. | string | |
open | boolean | true |
tonedanger: it cannot be undone. primary: it can, but it matters. | "primary" | "danger" | danger |