Nasimunreleased, after 0.12.0
ComponentsOverlays

ConfirmDialog

An alert dialog before a destructive or money-moving action, with its impact, a reason and a typed phrase.

Since 0.1.0Client componentstable
Open alone
confirm-dialog/delete.tsx
'use client';

import { Button, ConfirmDialog } from '@nyxon/nasim';
import { useState } from 'react';

const copy = {
  open: 'حذف سرویس',
  title: 'سرویس «ماهانه ۵۰ گیگ» حذف شود؟',
  description: 'اشتراک همین حالا قطع می‌شود و حجم باقی‌مانده برنمی‌گردد.',
  confirm: 'حذف سرویس',
};

// tone="danger" for what cannot be undone. onConfirm may return a promise: the dialog waits,
// shows the spinner, and closes when it resolves.
export default function Delete() {
  const [isOpen, setOpen] = useState(false);
  return (
    <>
      <Button variant="danger" onPress={() => setOpen(true)}>
        {copy.open}
      </Button>
      <ConfirmDialog
        isOpen={isOpen}
        onOpenChange={setOpen}
        tone="danger"
        title={copy.title}
        description={copy.description}
        confirmLabel={copy.confirm}
        onConfirm={() => new Promise((done) => setTimeout(done, 1200))}
      />
    </>
  );
}

Import

import { ConfirmDialog } from '@nyxon/nasim';

Usage

Controlled: isOpen and onOpenChange. title, description (what happens, what it affects, whether it can be undone), confirmLabel (name the action, not «تأیید») and onConfirm. tone="danger" for what cannot be undone. It is a client component.

<ConfirmDialog
  isOpen={open}
  onOpenChange={setOpen}
  tone="danger"
  title={t.services.deleteTitle}
  description={t.services.deleteWarning}
  confirmLabel={t.services.delete}
  onConfirm={() => removeService.mutateAsync(id)}
/>

onConfirm may return a promise: the button shows the spinner, Escape and the backdrop do nothing meanwhile, and the dialog closes when it resolves. If it rejects, or throws, the dialog stays open; report the error yourself. When the caller already knows the action is running (a mutation's isPending), pass isPending: the dialog only shows it and does not close itself. isConfirmDisabled draws the confirm disabled while the action is not yet allowed; the cancel stays live.

Examples

Delete

Shown above: the action waits for its promise, then the dialog closes.

A reason and a typed phrase

Three props carry the weight of an action that cannot be undone:

  • impact says what will be lost, in the screen's words, as a warning in the dialog's text;
  • reason adds a field ("optional" or "required", with reasonMinLength); onConfirm receives it trimmed;
  • confirmText is a phrase the person types before the confirm wakes up (the shop's name). It is compared after folding Arabic ي, ى and ك into ی and ک, Persian and Arabic digits into digits, dropping half-spaces and repeated spaces, and ignoring Latin case.
Open alone
confirm-dialog/reason.tsx
'use client';

import { Button, ConfirmDialog } from '@nyxon/nasim';
import { useState } from 'react';

const copy = {
  open: 'حذف فروشگاه',
  title: 'فروشگاه «نمونه» حذف شود؟',
  description: 'همهٔ پلن‌ها، سفارش‌ها و گزارش‌های این فروشگاه پاک می‌شوند.',
  impact: '۱۲ کاربر فعال قطع می‌شوند.',
  confirm: 'حذف فروشگاه',
  shop: 'نمونه',
};

// The weight of an action that cannot be undone, in three props: what is lost (impact), why
// (a required reason) and the shop's name typed out before the button wakes up.
export default function Reason() {
  const [isOpen, setOpen] = useState(false);
  return (
    <>
      <Button variant="danger" onPress={() => setOpen(true)}>
        {copy.open}
      </Button>
      <ConfirmDialog
        isOpen={isOpen}
        onOpenChange={setOpen}
        tone="danger"
        title={copy.title}
        description={copy.description}
        impact={copy.impact}
        reason="required"
        reasonMinLength={10}
        confirmText={copy.shop}
        confirmLabel={copy.confirm}
        onConfirm={() => setOpen(false)}
      />
    </>
  );
}

Rules

  • For destructive or money-moving actions only; anything else is a Modal.
  • The reason and the phrase live inside the dialog: closed, they are gone, and the next confirmation starts empty.

Accessibility

It is an alert dialog, named by its title and described by its description, its children and its impact, so they are read when it opens. The focus starts in the reason field, else in the phrase field, else on the cancel: the least destructive control, never the action. Escape and the cancel close it; once something is written, a press outside no longer does. The focus returns to the control that opened it when it closes.

API

PropTypeDefaultDescription
isOpenbooleanrequired
onOpenChange(isOpen: boolean) => voidrequired
titlestringrequired
descriptionReactNode—What happens, what it affects, and whether it can be undone.
childrenReactNode—The same as description, for a body of several paragraphs.
impactReactNode—What will be lost, in the screen's words («۱۲ کاربر فعال قطع می‌شوند»): a warning under the text.
confirmLabelstring—Name the action («حذف سرویس»), not just «تأیید».
cancelLabelstring—
tone"danger" | "default"—"danger" for destructive or irreversible actions.
onConfirm(reason: string) => void | Promise<unknown>requiredRuns the action with the reason as written (trimmed; "" without a reason field). May return a promise: the dialog waits, then closes; on a rejection or a throw it stays open (report it).
isPendingboolean—The owner knows when the action is running (a mutation's isPending): the dialog then only shows it, does not wait for onConfirm and does not close itself; close it when the action succeeds.
isConfirmDisabledboolean—The action is not yet allowed (a box still to tick): the confirm button is drawn disabled.
reason"optional" | "required"—A reason field: "required" keeps the confirm disabled until it is written.
reasonLabelstring—Names the reason field; defaults to «دلیل».
reasonMinLengthnumber—How many characters a required reason needs at least. Default 1.
confirmTextstring—A phrase the person types before the action is allowed (the store's name), for what cannot be undone.
confirmTextLabelstring—Names the phrase field; defaults to «برای تأیید، «…» را بنویسید».

Modal (a dialog for anything else) and Button with variant="danger" (the control that opens it).

On this page