# ConfirmDialog

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

- Import: `import { ConfirmDialog } from '@nyxon/nasim';`
- Server Component: no, it needs a client component
- Since: 0.1.0; status: stable
- Source: libs/nasim/src/composites/ConfirmDialog.tsx
- Describes: @nyxon/nasim unreleased, after 0.12.0
- Page: https://nasim.nyxon.tech/docs/components/overlays/confirm-dialog

<Example name="confirm-dialog/delete" height="420">
  ```tsx title="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))}
        />
      </>
    );
  }
  ```
</Example>

## Import [#import]

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

## Usage [#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.

```tsx
<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 [#examples]

### Delete [#delete]

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

### A reason and a typed phrase [#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.

<Example name="confirm-dialog/reason" height="600">
  ```tsx title="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)}
        />
      </>
    );
  }
  ```
</Example>

## Rules [#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 [#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 [#api]

| Prop                | Type                                           | Default  | Description                                                                                                                                                                                       |
| ------------------- | ---------------------------------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `isOpen`            | `boolean`                                      | required |                                                                                                                                                                                                   |
| `onOpenChange`      | `(isOpen: boolean) => void`                    | required |                                                                                                                                                                                                   |
| `title`             | `string`                                       | required |                                                                                                                                                                                                   |
| `description`       | `ReactNode`                                    | —        | What happens, what it affects, and whether it can be undone.                                                                                                                                      |
| `children`          | `ReactNode`                                    | —        | The same as `description`, for a body of several paragraphs.                                                                                                                                      |
| `impact`            | `ReactNode`                                    | —        | What will be lost, in the screen's words («۱۲ کاربر فعال قطع می‌شوند»): a warning under the text.                                                                                                 |
| `confirmLabel`      | `string`                                       | —        | Name the action («حذف سرویس»), not just «تأیید».                                                                                                                                                  |
| `cancelLabel`       | `string`                                       | —        |                                                                                                                                                                                                   |
| `tone`              | `"danger" \| "default"`                        | —        | "danger" for destructive or irreversible actions.                                                                                                                                                 |
| `onConfirm`         | `(reason: string) => void \| Promise<unknown>` | required | Runs 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).      |
| `isPending`         | `boolean`                                      | —        | 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. |
| `isConfirmDisabled` | `boolean`                                      | —        | 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.                                                                                                                      |
| `reasonLabel`       | `string`                                       | —        | Names the reason field; defaults to «دلیل».                                                                                                                                                       |
| `reasonMinLength`   | `number`                                       | —        | How many characters a required reason needs at least. Default 1.                                                                                                                                  |
| `confirmText`       | `string`                                       | —        | A phrase the person types before the action is allowed (the store's name), for what cannot be undone.                                                                                             |
| `confirmTextLabel`  | `string`                                       | —        | Names the phrase field; defaults to «برای تأیید، «…» را بنویسید».                                                                                                                                 |

## Related [#related]

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