ConfirmDialog
Pregunta antes de hacer algo importante. Nombra la cosa, dice qué va a pasar, y su botón dice exactamente lo que hace. Para las acciones más destructivas la persona primero escribe el nombre de la cosa.
import { ConfirmDialog } from '@sypnia/ui/react'
Algo que no se puede deshacer
El botón rojo dice qué borra. El foco empieza en «Cancelar».
Código
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 />
</>
);
} Algo importante que sí se puede deshacer
El botón lleva la luz, no el rojo, y muestra que está trabajando.
Código
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 />
</>
);
} Escribir el nombre
Escribe storefront: solo entonces se despierta «Delete project».
Código
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 />
</>
);
} Cuándo usarlo
Úsalo cuando
- Antes de cualquier cosa que no se puede deshacer: borrar, revocar, quitar a un miembro.
- Antes de algo que se puede deshacer pero llega a mucha gente: publicar, enviar. Usa
tone="primary". - Al borrar algo grande (un proyecto, una base de datos): agrega
confirmWordcon su nombre.
No lo uses cuando
- La acción es fácil de deshacer: hazla y ofrece «Deshacer» en un toast.
- La pregunta sería «¿Estás seguro?» con «Sí» y «No». Nombra la cosa en el título y la acción en el botón.
- Recoge información: eso es un Dialog con un formulario.
Para todos
- Es un diálogo de alerta: los lectores de pantalla leen el título y la explicación apenas se abre.
- El foco empieza en «Cancelar», así un Enter por reflejo nunca confirma. Con una palabra para escribir, el foco empieza en el campo y Enter confirma solo cuando la palabra coincide.
- Mientras está
busy, la acción no se puede disparar dos veces y el diálogo no se puede cerrar. - Escape cancela. Un toque afuera no hace nada: una decisión se toma con un botón, nunca por accidente. El foco vuelve al botón que lo abrió.
Props
| ConfirmDialog | Tipo | Por defecto |
|---|---|---|
actionobligatorioThe confirm button says exactly what it does: «Delete table», never «OK». | string | |
onCloseobligatorio | () => void | |
onConfirmobligatorio | () => void | |
titleobligatorioThe 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 |