Skip to content

ConfirmFlows

How much to ask depends on what can be lost. Reversible: do it and offer Undo. Small: a short question by the button. It matters: a dialog that says what happens. It destroys data: a dialog that says what is lost and asks to type the name.

import { ConfirmDialog, Popconfirm, toast } from '@sypnia/ui/react'

On this page
  1. On a desktop
  2. On a phone
  3. When to use it
  4. For everyone
  5. Props

On a desktop

The four levels, lightest first. Each button runs its real flow.

Ways to confirm

Archive the project

Reversible: it happens at once and the toast offers Undo.

Remove a saved filter

Small: a short question next to the button.

Remove a member

Matters, and can be reversed by inviting again: a dialog that lists what happens.

Delete a database

Destroys data: the dialog says what is lost and asks to type the name.

Code
import { useState } from 'react';
import {
  Button, ConfirmDialog, FormRow, KeyValue, Panel, Popconfirm, Stack, Toaster, toast, useSypnia,
} from '@sypnia/ui/react';

// How much to ask depends on what can be lost. Reversible: do it and offer Undo. Small: a short question next to the
// button. Matters but can be reversed: a dialog that says what happens. Destroys data: a dialog that says what is
// lost and asks to type the name. Every button says what it does: never «OK», never «Yes».
const en = {
  panel: 'Ways to confirm', archive: 'Archive the project', archiveDesc: 'Reversible: it happens at once and the toast offers Undo.', archiveBtn: 'Archive',
  archived: 'Project archived', undo: 'Undo', restored: 'Project restored',
  filter: 'Remove a saved filter', filterDesc: 'Small: a short question next to the button.', filterBtn: 'Remove filter', filterQ: 'Remove the filter «Failed payments»?', filterBody: 'Only the saved filter goes; no data changes.', remove: 'Remove', removed: 'Filter removed',
  member: 'Remove a member', memberDesc: 'Matters, and can be reversed by inviting again: a dialog that lists what happens.', memberBtn: 'Remove Luis',
  memberQ: 'Remove Luis Ortega from Northwind?', memberBody: 'Luis loses access at once. What he made stays:', memberAction: 'Remove Luis', memberDone: 'Luis was removed. Invite him again to give access back.',
  keys: 'API keys he created', keysV: '2 keep working until you delete them', projects: 'Projects he owns', projectsV: 'pass to you', sessions: 'Open sessions', sessionsV: 'end now',
  drop: 'Delete a database', dropDesc: 'Destroys data: the dialog says what is lost and asks to type the name.', dropBtn: 'Delete database',
  dropQ: 'Delete the database northwind-prod?', dropBody: 'Its 14 tables, 1.2 million rows and every backup are deleted. Apps using it stop working. This cannot be undone.', dropAction: 'Delete database', dropped: 'northwind-prod was deleted',
};
const es: typeof en = {
  panel: 'Formas de confirmar', archive: 'Archivar el proyecto', archiveDesc: 'Reversible: pasa de una vez y el aviso ofrece Deshacer.', archiveBtn: 'Archivar',
  archived: 'Proyecto archivado', undo: 'Deshacer', restored: 'Proyecto recuperado',
  filter: 'Quitar un filtro guardado', filterDesc: 'Pequeño: una pregunta corta junto al botón.', filterBtn: 'Quitar filtro', filterQ: '¿Quitar el filtro «Pagos fallidos»?', filterBody: 'Solo se va el filtro guardado; ningún dato cambia.', remove: 'Quitar', removed: 'Filtro quitado',
  member: 'Sacar a un miembro', memberDesc: 'Importa, y se revierte invitando de nuevo: un diálogo que dice qué pasa.', memberBtn: 'Sacar a Luis',
  memberQ: '¿Sacar a Luis Ortega de Northwind?', memberBody: 'Luis pierde el acceso de inmediato. Lo que hizo se queda:', memberAction: 'Sacar a Luis', memberDone: 'Luis salió. Invítalo de nuevo para devolverle el acceso.',
  keys: 'Llaves de API que creó', keysV: '2 siguen funcionando hasta que las borres', projects: 'Proyectos suyos', projectsV: 'pasan a ti', sessions: 'Sesiones abiertas', sessionsV: 'se cierran ya',
  drop: 'Borrar una base de datos', dropDesc: 'Destruye datos: el diálogo dice qué se pierde y pide escribir el nombre.', dropBtn: 'Borrar base de datos',
  dropQ: '¿Borrar la base de datos northwind-prod?', dropBody: 'Se borran sus 14 tablas, 1,2 millones de filas y todas sus copias. Las apps que la usan dejan de funcionar. No se puede deshacer.', dropAction: 'Borrar base de datos', dropped: 'Se borró northwind-prod',
};
const wait = (ms: number) => new Promise((r) => setTimeout(r, ms));

export default function ConfirmFlows() {
  const { locale } = useSypnia();
  const c = locale.startsWith('es') ? es : en;
  const [open, setOpen] = useState<'member' | 'drop' | null>(null);
  const [busy, setBusy] = useState(false);
  const confirm = async (done: string) => { setBusy(true); await wait(1000); setBusy(false); setOpen(null); toast(done, { tone: 'ok' }); };
  return (
    <>
      <Panel title={c.panel}>
        <Stack gap={0}>
          <FormRow label={c.archive} description={c.archiveDesc}>
            <Button icon="archive" onClick={() => toast(c.archived, { action: { label: c.undo, onClick: () => toast(c.restored, { tone: 'ok' }) } })}>{c.archiveBtn}</Button>
          </FormRow>
          <FormRow label={c.filter} description={c.filterDesc}>
            <Popconfirm trigger={<Button icon="filter">{c.filterBtn}</Button>} title={c.filterQ} action={c.remove} onConfirm={() => toast(c.removed, { tone: 'ok' })}>{c.filterBody}</Popconfirm>
          </FormRow>
          <FormRow label={c.member} description={c.memberDesc}>
            <Button icon="user-minus" onClick={() => setOpen('member')}>{c.memberBtn}</Button>
          </FormRow>
          <FormRow label={c.drop} description={c.dropDesc} danger>
            <Button variant="danger" icon="trash" onClick={() => setOpen('drop')}>{c.dropBtn}</Button>
          </FormRow>
        </Stack>
      </Panel>
      <ConfirmDialog open={open === 'member'} tone="primary" title={c.memberQ} action={c.memberAction} busy={busy} onClose={() => setOpen(null)} onConfirm={() => confirm(c.memberDone)}>
        <Stack gap={3}><p>{c.memberBody}</p><KeyValue items={[[c.keys, c.keysV], [c.projects, c.projectsV], [c.sessions, c.sessionsV]]} /></Stack>
      </ConfirmDialog>
      <ConfirmDialog open={open === 'drop'} title={c.dropQ} action={c.dropAction} confirmWord="northwind-prod" busy={busy} onClose={() => setOpen(null)} onConfirm={() => confirm(c.dropped)}>
        <p>{c.dropBody}</p>
      </ConfirmDialog>
      <Toaster />
    </>
  );
}

On a phone

On a phone the buttons go under their text and the dialogs fill the width.

When to use it

Use it when

  • Choosing the lightest confirmation that is still safe: most actions need none, only Undo.
  • Saying the consequences in the dialog, with numbers when there are any: «14 tables, 1.2 million rows».

Do not use it when

  • A dialog for something Undo can fix: people learn to press «Yes» without reading.
  • Buttons that say «OK» or «Yes»: the button says what it does, «Delete database».
  • A typed name for anything that can be undone.

For everyone

  • Dialogs are alertdialogs: the focus goes to «Cancel», or to the field when a name must be typed.
  • The typed name is matched exactly, and Enter confirms only when it matches.
  • Undo stays in the toast long enough to reach with the keyboard, and the toast is announced.

Props

ConfirmDialogTypeDefault
actionrequired

The confirm button says exactly what it does: «Delete table», never «OK».

string
onCloserequired () => void
onConfirmrequired () => void
titlerequired

The question, naming the thing: «Delete the table orders?»

ReactNode
busy boolean
children

What will happen, in plain words, and whether it can be undone.

ReactNode
confirmWord

For the most destructive actions: the person must type this word (usually the name of the thing) to enable the button.

string
open boolean true
tone

danger: it cannot be undone. primary: it can, but it matters.

"primary" | "danger" danger
PopconfirmTypeDefault
actionrequired

The confirm button says what it does: «Remove».

string
onConfirmrequired () => void
titlerequired

The question, naming the thing.

ReactNode
triggerrequired

The button that asks.

ReactElement<any, string | JSXElementConstructor<any>>
children

One sentence on what will happen.

ReactNode
placement "bottom start" | "bottom end" | "bottom" | "top" bottom
tone "primary" | "danger" primary
  1. On a desktop
  2. On a phone
  3. When to use it
  4. For everyone
  5. Props

    Search pages, sections and terms