# Dialog

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

The centred modal: a title with one line of what it is for, a scrolling body, and a footer whose last button commits.

`import { EcoDialog } from "vue-ecosphere"`

## Picker

Open it, filter the list, pick one. Tab stays inside; Escape closes.

Vue (vue-ecosphere):

```vue
<EcoButton variant="primary" icon="add-line" @click="open = true">Add a destination</EcoButton>

<EcoDialog v-model:open="open" title="Add a destination"
  subtitle="Pick one of the destinations already configured, or register a new one."
  note="* Required" confirm-label="Add destination" :confirm-disabled="!picked" @confirm="add">
  <div class="dlg__find">
    <i class="ri-search-line" aria-hidden="true"></i>
    <input class="input" type="text" placeholder="Filter destinations…" aria-label="Filter destinations" />
  </div>
  <button class="dlg__opt" type="button">
    <span class="avatar avatar--sm" aria-hidden="true">PC</span>
    <span class="dlg__opt-b"><span class="dlg__opt-n">Primary cluster</span><span class="dlg__opt-h">eu-west-1 · 4 nodes</span></span>
  </button>
  …
</EcoDialog>
```

React (react-ecosphere):

```tsx
const [open, setOpen] = useState(false);
const [picked, setPicked] = useState<string | null>(null);

<EcoButton variant="primary" icon="add-line" onClick={() => setOpen(true)}>Add a destination</EcoButton>

<EcoDialog open={open} onOpenChange={setOpen} title="Add a destination"
  subtitle="Pick one of the destinations already configured, or register a new one."
  note="* Required" confirmLabel="Add destination" confirmDisabled={!picked} onConfirm={add}>
  <div className="dlg__find">
    <i className="ri-search-line" aria-hidden="true"></i>
    <input className="input" type="text" placeholder="Filter destinations…" aria-label="Filter destinations" />
  </div>
  <button className="dlg__opt" type="button">
    <span className="avatar avatar--sm" aria-hidden="true">PC</span>
    <span className="dlg__opt-b"><span className="dlg__opt-n">Primary cluster</span><span className="dlg__opt-h">eu-west-1 · 4 nodes</span></span>
  </button>
  …
</EcoDialog>
```

## Destructive

No body: a confirmation only has to say what is lost and name the action.

Vue (vue-ecosphere):

```vue
<EcoDialog v-model:open="open" danger title="Delete “Cold storage”?"
  subtitle="Everything routed here stops on save. Ninety days of retained objects are deleted with it, and this cannot be undone."
  confirm-label="Delete destination" @confirm="remove" />
```

React (react-ecosphere):

```tsx
<EcoDialog open={open} onOpenChange={setOpen} danger title="Delete “Cold storage”?"
  subtitle="Everything routed here stops on save. Ninety days of retained objects are deleted with it, and this cannot be undone."
  confirmLabel="Delete destination" onConfirm={remove} />
```

## Loading

confirm-loading while the commit runs: the button shows its spinner and the dialog holds until you close it. For a plain yes-or-no question, useConfirm does this for you.

Vue (vue-ecosphere):

```vue
<EcoDialog v-model:open="open" title="Rotate the API key?"
  subtitle="Services using the current key keep working for 24 hours, then need the new one."
  confirm-label="Rotate key" :confirm-loading="rotating" @confirm="rotate" />

<script setup lang="ts">
const rotating = ref(false);
async function rotate() {
  rotating.value = true;
  try {
    await api.rotateKey();
    open.value = false;
  } finally {
    rotating.value = false;
  }
}
</script>
```

React (react-ecosphere):

```tsx
const [open, setOpen] = useState(false);
const [rotating, setRotating] = useState(false);

async function rotate() {
  setRotating(true);
  try {
    await api.rotateKey();
    setOpen(false);
  } finally {
    setRotating(false);
  }
}

<EcoDialog open={open} onOpenChange={setOpen} title="Rotate the API key?"
  subtitle="Services using the current key keep working for 24 hours, then need the new one."
  confirmLabel="Rotate key" confirmLoading={rotating} onConfirm={rotate} />
```

## Must be answered

closable false: no ×, and Escape and the scrim do nothing. Keep it for a step the app cannot continue without, and give the footer the one way on.

Vue (vue-ecosphere):

```vue
<EcoDialog v-model:open="open" :closable="false" title="Before you continue"
  subtitle="We keep a record of your choice. You can change the optional ones later in Privacy settings.">
  <EcoCheckbox v-model="agreed">I have read and agree to the Terms of service</EcoCheckbox>
  <template #footer>
    <EcoButton variant="primary" size="sm" :disabled="!agreed" @click="accept">Continue</EcoButton>
  </template>
</EcoDialog>
```

React (react-ecosphere):

```tsx
const [agreed, setAgreed] = useState(false);

<EcoDialog open={open} onOpenChange={setOpen} closable={false} title="Before you continue"
  subtitle="We keep a record of your choice. You can change the optional ones later in Privacy settings."
  footer={<EcoButton variant="primary" size="sm" disabled={!agreed} onClick={accept}>Continue</EcoButton>}>
  <EcoCheckbox checked={agreed} onChange={setAgreed}>I have read and agree to the Terms of service</EcoCheckbox>
</EcoDialog>
```

## Wide, with a list of sub-records

dlg--wide for a two-column form; dlgrow for the sub-records.

Vue (vue-ecosphere):

```vue
<EcoDialog v-model:open="open" wide title="New class"
  subtitle="A class is a grade level. Its sections are created with it, and each one gets a register the moment it has a teacher and a room.">
  <div class="fgrid">…</div>
  <div class="dlgrow">…</div>
  <template #footer="{ cancel }">
    <p class="dlg__note">Registers open once a section has both a teacher and a room.</p>
    <span class="sp"></span>
    <EcoButton variant="tertiary" size="sm" @click="cancel">Cancel</EcoButton>
    <EcoButton variant="primary" size="sm" @click="create">Create class</EcoButton>
  </template>
</EcoDialog>
```

React (react-ecosphere):

```tsx
<EcoDialog open={open} onOpenChange={setOpen} wide title="New class"
  subtitle="A class is a grade level. Its sections are created with it, and each one gets a register the moment it has a teacher and a room."
  renderFooter={({ cancel }) => (
    <>
      <p className="dlg__note">Registers open once a section has both a teacher and a room.</p>
      <span className="sp"></span>
      <EcoButton variant="tertiary" size="sm" onClick={cancel}>Cancel</EcoButton>
      <EcoButton variant="primary" size="sm" onClick={create}>Create class</EcoButton>
    </>
  )}>
  <div className="fgrid">…</div>
  <div className="dlgrow">…</div>
</EcoDialog>
```

## API

Vue (vue-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `v-model:open` | `boolean` | false | Shows dlg and dlgscrim (teleported to body). Escape, the × and the scrim close it; focus is trapped and returns to the opener; body scroll locks. |
| `title` | `string` | — | dlg__t; labels the dialog (#title slot). |
| `subtitle` | `string` | — | dlg__s: what it is for — or, when destructive, what is lost and that it cannot be undone. |
| `confirmLabel` | `string` | — | The commit button, last in the footer. Name the consequence, never “OK”. Omit for no default footer. |
| `cancelLabel` | `string` | "Cancel" | Tertiary button before the commit. |
| `danger` | `boolean` | false | Commit becomes btn--danger, the only filled button. |
| `wide` | `boolean` | false | dlg--wide (720px) for a two-column form. |
| `note` | `string` | — | Left footer note (dlg__n). Without it the footer is dlg__f--end. |
| `confirmDisabled` | `boolean` | false | Disable the commit until the dialog is complete. |
| `confirmLoading` | `boolean` | false | The commit is running: the commit button shows loading, Cancel and × are disabled, and Escape and the scrim do nothing. Close it yourself on success; clear it on failure. |
| `dismissible` | `boolean` | true | Close on a scrim click. |
| `closable` | `boolean` | true | Offer the × and close on Escape and the scrim. False for a dialog that must be answered: only the footer's buttons close it. |
| `alert` | `boolean` | false | An interruption that asks for a decision, like a confirmation: role="alertdialog". The subtitle describes every dialog (aria-describedby), so it is read with the title. |
| `#default / #footer` | `slot` | — | Body (dlg__body) and a replacement footer ({ close, cancel }). |
| `@confirm / @cancel / @close` | `() => void` | — | Commit clicked; Cancel clicked; closed by any route. |

React (react-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `open / onOpenChange` | `boolean` | false | Shows dlg and dlgscrim (portalled to body). Escape, the × and the scrim close it; focus is trapped and returns to the opener; body scroll locks. Or defaultOpen, uncontrolled. |
| `title` | `ReactNode` | — | dlg__t; labels the dialog. Text, or a node. |
| `subtitle` | `ReactNode` | — | dlg__s: what it is for — or, when destructive, what is lost and that it cannot be undone. |
| `confirmLabel` | `string` | — | The commit button, last in the footer. Name the consequence, never “OK”. Omit for no default footer. |
| `cancelLabel` | `string` | "Cancel" | Tertiary button before the commit. |
| `danger` | `boolean` | false | Commit becomes btn--danger, the only filled button. |
| `wide` | `boolean` | false | dlg--wide (720px) for a two-column form. |
| `note` | `ReactNode` | — | Left footer note (dlg__n). Without it the footer is dlg__f--end. |
| `confirmDisabled` | `boolean` | false | Disable the commit until the dialog is complete. |
| `confirmLoading` | `boolean` | false | The commit is running: the commit button shows loading, Cancel and × are disabled, and Escape and the scrim do nothing. Close it yourself on success; clear it on failure. |
| `dismissible` | `boolean` | true | Close on a scrim click. |
| `closable` | `boolean` | true | Offer the × and close on Escape and the scrim. False for a dialog that must be answered: only the footer's buttons close it. |
| `alert` | `boolean` | false | An interruption that asks for a decision, like a confirmation: role="alertdialog". The subtitle describes every dialog (aria-describedby), so it is read with the title. |
| `children / footer` | `ReactNode / (scope: { close, cancel }) => ReactNode` | — | Body (dlg__body) and a replacement footer. |
| `onConfirm / onCancel / onClose` | `() => void` | — | Commit clicked; Cancel clicked; closed by any route. |
