Ir al contenido
Español

Cómo funciona

A 380px dialog that only handles confirm or cancel: no close button, no custom footer. It opens from the element that had focus (the button that opened it), growing out of that point, and closes back toward it. A tinted 44px circle carries the tone icon; the two pill buttons share the width equally. When onConfirm returns a Promise it takes over submitting: the confirm button shows a spinner at the same width, Cancel, Esc and the mask are blocked, success calls onClose, and failure expands an error line above the buttons so a retry is one click away. With tone="danger" focus starts on Cancel, so a habitual Enter deletes nothing.

Úsalo para

  • Before an irreversible action: delete, clear, refund, unlink (tone="danger").
  • Actions with a clear cost that take effect on click, such as cancelling a booking with a fee (tone="default").
  • Confirmations that wait for an API: loading and retry are handled for you.

Busca otra opción cuando

  • Undoable actions: run them directly and offer "Undo" in a toast.
  • Anything that needs input or long content: use a modal; this dialog has one title, one description and two buttons.
  • Light confirmations inside a table row: use a popover confirm that does not cover the page.
  • Do not open a confirm over another modal or drawer: swap the content inside it.
  • Pure notifications with no choice: use a toast, not a dialog with a single "Got it".

Uso

import { useState } from 'react'
import { ConfirmDialog } from './ConfirmDialog.jsx'

export function DeleteTripButton({ trip }) {
  const [open, setOpen] = useState(false)
  return (
    <>
      <button aria-label={`Delete ${trip.name}`} onClick={() => setOpen(true)}><TrashIcon /></button>
      <ConfirmDialog
        open={open}
        onClose={() => setOpen(false)}
        tone="danger"
        title={`Delete “${trip.name}”?`}
        description="This cannot be undone: the trip and its receipts are removed."
        confirmText="Delete trip"
        // Returning a Promise shows loading, closes on success, and stays with errorText on failure
        onConfirm={() => deleteTrip(trip.id)}
        errorText="Not deleted: you are offline, please try again"
      />
    </>
  )
}

Propiedades

PropiedadTipoPor defectoQué hace
openboolean—Whether the dialog is open.
onClose() => void—Asks to close (Cancel, mask click, Esc, and after a successful confirm); the caller sets open to false.
titleReactNode—Title: name the object and the consequence ("Delete 3 messages? This cannot be undone"), not "Notice".
descriptionReactNode—Extra detail on what will happen; linked through aria-describedby.
iconReactNode | false—The icon in the tinted circle above the title. By default a warning sign for danger and a question mark otherwise; pass a node to replace it or false to hide it.
confirmTextReactNode—Text of the confirm button, required: write the action ("Delete trip"), not "OK".
cancelTextReactNode'Cancel'Text of the cancel button.
tone'default' | 'danger''default'danger: danger-styled confirm button and focus starts on Cancel; default: primary button and focus starts on confirm.
onConfirm() => void | Promise<unknown>—Called on confirm. Returning a Promise gives loading, no closing meanwhile, auto-close on success and an error on failure; otherwise it closes right after.
errorTextReactNode | ((error: unknown) => ReactNode)'That did not work. Please try again.'Message shown on failure; pass a function to choose it from the error.
labelsobject—Replaces built-in text (pending: the screen-reader message while working). Shallow-merged over the English defaults.
maskClosablebooleantrueWhether clicking the mask closes it (same as Cancel); never while submitting.
closeOnEscbooleantrueWhether Esc closes it (same as Cancel); never while submitting.
containedbooleanfalseConfines it to the nearest positioned ancestor (absolute); by default it portals to body and covers the viewport.
...restHTMLAttributes<HTMLDivElement>—className and other attributes go to the panel (the role="alertdialog" element).

Accesibilidad

  • The panel is role="alertdialog" with aria-modal; title and description are linked with aria-labelledby and aria-describedby, so both are read on open.
  • Focus starts on Cancel for dangerous actions and on the confirm button otherwise; Tab cycles between the two buttons; focus returns to the opener on close.
  • While open, siblings are inert; outside contained mode page scrolling is locked; Esc is not passed outward.
  • While submitting, the confirm button has aria-busy and keeps focus, a screen-reader-only message says it is working, and Cancel, Esc and the mask are blocked.
  • The error appears in a role="alert" region and carries an icon as well as color.
  • Buttons are 44px pills; the tone icon is decorative (aria-hidden), since the title carries the meaning; reduced motion only fades the panel and slows the spinner without stopping it.

Movimiento

Enter: the mask (dimmed, 6px blur) fades in over 240ms; the panel grows from the opener, scale 0.9 to 1 with a fade, 240ms ease-out (without an opener it rises 12px and scales from 0.96). Exit is faster, 160ms ease-in, shrinking back toward the opener. The error expands in 240ms; pill buttons press to 0.97 in 100ms. Reduced motion: fades only.

Añádelo a tu proyecto

Copia la instrucción en Codex o Claude Code. El agente lee el código publicado, te muestra lo que va a escribir y añade el componente junto a los tuyos.

ConfirmDialog.jsxconfirm-dialog.csstokens.cssREADME.md

Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.

Capas
Panel inferior viene con Pro. La vista previa aparece al desbloquearlo.