# Confirm

Source: https://ecosphere.dev/docs/feedback/confirm (Ecosphere 1.5). Install `vue-ecosphere` for Vue 3 or `react-ecosphere` for React 18.2+/19: same components, props, markup and stylesheet.

Ask a yes-or-no question from anywhere and await the answer: a dialog with no body that names the consequence, and runs the work with the commit button loading.

`import { useConfirm, EcoConfirmHost } from "vue-ecosphere"`

## Ask, then act

The promise resolves true for the commit and false for every way out. Replaces Modal.confirm and a-popconfirm.

Vue (vue-ecosphere):

```vue
<!-- once, near the app root -->
<EcoConfirmHost />

<script setup lang="ts">
import { useConfirm } from "vue-ecosphere";
const confirm = useConfirm();

async function archive() {
  const ok = await confirm({
    title: "Archive 12 requests?",
    text: "They leave the open list and stop sending reminders. You can restore them from Archived.",
    confirmLabel: "Archive requests",
  });
  if (ok) await api.archive(ids);
}
</script>
```

React (react-ecosphere):

```tsx
// once, near the app root
<EcoConfirmHost />

// anywhere: a component, an event handler, a plain module
import { useConfirm } from "react-ecosphere";
const confirm = useConfirm();

async function archive() {
  const ok = await confirm({
    title: "Archive 12 requests?",
    text: "They leave the open list and stop sending reminders. You can restore them from Archived.",
    confirmLabel: "Archive requests",
  });
  if (ok) await api.archive(ids);
}
```

## Destructive, with the work inside

Pass onConfirm and the commit shows its spinner while it runs; the dialog closes when it succeeds.

Vue (vue-ecosphere):

```vue
const ok = await confirm({
  title: "Delete this report?",
  text: "It disappears for everyone it was shared with. This cannot be undone.",
  confirmLabel: "Delete report",
  cancelLabel: "Keep it",
  danger: true,
  onConfirm: () => api.deleteReport(id), // commit shows loading; the dialog cannot be dismissed
});
if (ok) toast.success("Report deleted");
```

React (react-ecosphere):

```tsx
const ok = await confirm({
  title: "Delete this report?",
  text: "It disappears for everyone it was shared with. This cannot be undone.",
  confirmLabel: "Delete report",
  cancelLabel: "Keep it",
  danger: true,
  onConfirm: () => api.deleteReport(id), // commit shows loading; the dialog cannot be dismissed
});
if (ok) toast.success("Report deleted");
```

## When it fails

Throw from onConfirm to keep the dialog open. Say what went wrong yourself, with a toast; the user retries or cancels. This demo fails once, then succeeds.

Vue (vue-ecosphere):

```vue
const ok = await confirm({
  title: "Revoke Grace Kim’s access?",
  text: "She is signed out everywhere and loses access to 4 workspaces at once.",
  confirmLabel: "Revoke access",
  danger: true,
  onConfirm: async () => {
    try {
      await api.revoke(userId);
    } catch (e) {
      toast.danger("Access was not revoked", { description: "The directory did not answer. Nothing changed — try again." });
      throw e; // keeps the dialog open: the user can retry or cancel
    }
  },
});
```

React (react-ecosphere):

```tsx
const ok = await confirm({
  title: "Revoke Grace Kim’s access?",
  text: "She is signed out everywhere and loses access to 4 workspaces at once.",
  confirmLabel: "Revoke access",
  danger: true,
  onConfirm: async () => {
    try {
      await api.revoke(userId);
    } catch (e) {
      toast.danger("Access was not revoked", { description: "The directory did not answer. Nothing changed — try again." });
      throw e; // keeps the dialog open: the user can retry or cancel
    }
  },
});
```

## API

Vue (vue-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `useConfirm()` | `(options) => Promise<boolean>` | — | Returns confirm(). It resolves true once confirmed (after onConfirm succeeds), false on Cancel, ×, Escape or the scrim. Questions queue and show one at a time. |
| `options.title` | `string` | — | The question, as the dialog title: “Delete this report?”. |
| `options.text` | `string` | — | The subtitle: what happens, and for a destructive action what is lost. |
| `options.confirmLabel` | `string` | — | Required. The commit button: name the consequence (“Delete report”), never “OK”. |
| `options.cancelLabel` | `string` | "Cancel" | The button that backs out. “Keep it” reads well beside a deletion. |
| `options.danger` | `boolean` | false | The commit becomes btn--danger. |
| `options.onConfirm` | `() => unknown` | — | Runs on commit with the button loading; Cancel, ×, Escape and the scrim do nothing meanwhile. Settles → closes and resolves true. Throws → stays open; report the error yourself. |
| `<EcoConfirmHost>` | `component` | — | Renders the queue as an EcoDialog. Mount once near the app root. Without it, confirm() logs one console error and resolves false; on the server it resolves false. |

React (react-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `useConfirm()` | `(options) => Promise<boolean>` | — | Returns confirm(), the same function every time; call it from components, handlers or plain modules. It resolves true once confirmed (after onConfirm succeeds), false on Cancel, ×, Escape or the scrim. Questions queue and show one at a time. |
| `options.title` | `string` | — | The question, as the dialog title: “Delete this report?”. |
| `options.text` | `string` | — | The subtitle: what happens, and for a destructive action what is lost. |
| `options.confirmLabel` | `string` | — | Required. The commit button: name the consequence (“Delete report”), never “OK”. |
| `options.cancelLabel` | `string` | "Cancel" | The button that backs out. “Keep it” reads well beside a deletion. |
| `options.danger` | `boolean` | false | The commit becomes btn--danger. |
| `options.onConfirm` | `() => unknown` | — | Runs on commit with the button loading; Cancel, ×, Escape and the scrim do nothing meanwhile. Settles → closes and resolves true. Throws → stays open; report the error yourself. |
| `<EcoConfirmHost>` | `component` | — | Renders the queue as an EcoDialog. Mount once near the app root. Without it, confirm() logs one console error and resolves false; on the server it resolves false. |
