Recipes
Confirmations
Ask for confirmation only when an action cannot be undone. For everything else, act immediately and offer Undo — it is faster and safer.
Reversible: Undo instead of “Are you sure?”
Archive with Undo
The action happens at once; the toast with Undo stays until dismissed.
- Globex — Invoice INV-2043 is overdue
- Initech — New purchase order PO-881
- Hooli — Question about VAT
import { Button, EmptyState, ToastProvider, useToast } from '@reactloom/ui';
import { useState } from 'react';
const initial = [
{ id: 'm1', from: 'Globex', subject: 'Invoice INV-2043 is overdue' },
{ id: 'm2', from: 'Initech', subject: 'New purchase order PO-881' },
{ id: 'm3', from: 'Hooli', subject: 'Question about VAT' },
];
/** Reversible: act immediately and offer Undo, instead of asking “Are you sure?”. */
function Inbox() {
const toast = useToast();
const [messages, setMessages] = useState(initial);
const archive = (id: string) => {
const index = messages.findIndex((m) => m.id === id);
const message = messages[index];
if (!message) return;
setMessages((list) => list.filter((m) => m.id !== id));
toast.show({
id: `archived-${id}`,
title: `Archived “${message.subject}”`,
action: {
label: 'Undo',
onAction: () => setMessages((list) => [...list.slice(0, index), message, ...list.slice(index)]),
},
});
};
if (messages.length === 0) {
return <EmptyState size="sm" headingLevel={4} live="polite" tone="success" title="Inbox zero" actions={<Button size="sm" variant="secondary" onClick={() => setMessages(initial)}>Reset demo</Button>} />;
}
return (
<ul aria-label="Inbox" style={{ listStyle: 'none', margin: 0, padding: 0, width: '100%', maxWidth: 480 }}>
{messages.map((m) => (
<li key={m.id} style={{ display: 'flex', alignItems: 'center', gap: 12, padding: '8px 0', borderBottom: '1px solid var(--pl-color-border-default)' }}>
<span style={{ flex: 1 }}>
<strong>{m.from}</strong> — {m.subject}
</span>
<Button size="sm" variant="secondary" aria-label={`Archive “${m.subject}”`} onClick={() => archive(m.id)}>
Archive
</Button>
</li>
))}
</ul>
);
}
export default function Example() {
return (
<ToastProvider>
<Inbox />
</ToastProvider>
);
}Irreversible: confirm in an alert dialog
Delete a workspace
Type the name to confirm. A mismatch explains itself instead of disabling the button.
import { Alert, Button, Dialog, Input, Stack, Text } from '@reactloom/ui';
import { useState } from 'react';
const WORKSPACE = 'acme-finance';
/** Irreversible and high-impact: confirm in an alert dialog and ask for the name. */
export default function Example() {
const [open, setOpen] = useState(false);
const [typed, setTyped] = useState('');
const [error, setError] = useState<string | undefined>();
const [deleted, setDeleted] = useState(false);
if (deleted) {
return (
<Alert tone="success" live="polite" title={`Workspace “${WORKSPACE}” was deleted`} actions={<Button size="sm" variant="secondary" onClick={() => setDeleted(false)}>Reset demo</Button>} />
);
}
return (
<Dialog.Root
open={open}
onOpenChange={(next) => {
setOpen(next);
if (!next) {
setTyped('');
setError(undefined);
}
}}
>
<Dialog.Trigger asChild>
<Button variant="destructive">Delete workspace</Button>
</Dialog.Trigger>
<Dialog.Content role="alertdialog" size="sm">
<Dialog.Title>Delete workspace “{WORKSPACE}”?</Dialog.Title>
<Dialog.Description>
This permanently deletes 1,284 invoices, 312 customers and all files. It cannot be undone.
</Dialog.Description>
<form
id="delete-form"
noValidate
onSubmit={(e) => {
e.preventDefault();
// The button stays enabled; a mismatch explains itself instead of a silent disabled state.
if (typed !== WORKSPACE) {
setError(`Type ${WORKSPACE} exactly to confirm.`);
e.currentTarget.querySelector('input')?.focus();
return;
}
setOpen(false);
setDeleted(true);
}}
>
<Stack gap="2">
<Text size="sm">
To confirm, type <strong>{WORKSPACE}</strong>.
</Text>
<Input label="Workspace name" hideLabel autoComplete="off" spellCheck={false} value={typed} onChange={(e) => setTyped(e.target.value)} error={error} />
</Stack>
</form>
<Dialog.Footer>
<Dialog.Close asChild>
<Button variant="secondary">Cancel</Button>
</Dialog.Close>
<Button variant="destructive" type="submit" form="delete-form">
Delete workspace
</Button>
</Dialog.Footer>
</Dialog.Content>
</Dialog.Root>
);
}Details that matter
- Confirmation dialogs that appear for every action train people to click through them. Reserve them for loss that cannot be recovered.
- Use
role="alertdialog": it is announced as urgent and does not close on a backdrop click. - The title asks the question with the object’s name; the description states the consequence in numbers people recognise.
- The destructive button repeats the action (“Delete workspace”), never just “OK”. Cancel is focused first.
- Toasts with an action stay until dismissed, so there is time to use Undo with a keyboard or screen reader.