# Dialog

## Usage

`Dialog` presents one modal task above the page. It uses the native `<dialog>` element, so it traps focus, locks the page behind a backdrop, and returns focus to the element that opened it. Pair it with `DialogHeader` to get a focusable title, an optional subtitle, and a close button.

```vue
<script setup lang="ts">
import { Button } from '@astryx-vue/core/Button'
import { Dialog, DialogHeader } from '@astryx-vue/core/Dialog'
import { ref } from 'vue'

const isOpen = ref(false)
</script>

<template>
  <Button label="Share project" variant="secondary" @click="isOpen = true" />
  <Dialog :is-open="isOpen" @open-change="isOpen = $event">
    <DialogHeader title="Share project" has-close-button @open-change="isOpen = $event" />
    <p>Copy the link and send it to your team.</p>
  </Dialog>
</template>
```

### Anatomy

- **Header**: Title, optional subtitle, and close button. The title receives focus on open and labels the dialog via aria-labelledby.
- **Body**: The main content area: text, forms, lists, or any layout.
- **Footer**: Action buttons like Save/Cancel or Accept/Decline, aligned to the end.
- **Backdrop**: Semi-transparent overlay behind the dialog that blocks page interaction.

## Best practices

| Guidance | Practices |
| --- | --- |
| Do | Choose the right purpose: info for dismissable content, form to prevent accidental backdrop dismissal, required when the user must respond. |
| Do | Include a clear title in the header so users immediately understand what the dialog is asking. |
| Do | Use purpose="form" for dialogs with inputs so the user can't accidentally lose data by clicking the backdrop. |
| Do | Keep dialogs focused on a single task; if the content grows beyond what fits, consider a full page instead. |
| Don't | Use a dialog for simple messages that could be shown inline or as a toast notification. |
| Don't | Nest dialogs inside other dialogs; restructure the flow into steps within a single dialog instead. |
| Don't | Use the fullscreen variant for simple confirmations; it is meant for complex content like editors or long forms. |

## Examples

### Adaptive presentation

Dialog stays the default. On a coarse pointer at lg and below, the same profile form opens as a bottom sheet.

```vue
<script setup lang="ts">
import type { BottomSheetHeight, BottomSheetPurpose } from '@astryx-vue/core/BottomSheet'
import type { DialogPurpose } from '@astryx-vue/core/Dialog'
import { useMediaQuery } from '@astryx-vue/core'
import { BottomSheet } from '@astryx-vue/core/BottomSheet'
import { Button } from '@astryx-vue/core/Button'
import { Dialog, DialogHeader } from '@astryx-vue/core/Dialog'
import { Heading } from '@astryx-vue/core/Heading'
import { Layout, LayoutContent, LayoutFooter } from '@astryx-vue/core/Layout'
import { HStack, VStack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
import { TextArea } from '@astryx-vue/core/TextArea'
import { TextInput } from '@astryx-vue/core/TextInput'
import { ref } from 'vue'

const TOUCH_ORIENTED_LG_QUERY = '(max-width: 1024px) and (pointer: coarse) and (hover: none)'

type AdaptivePresentation = 'dialog' | 'fullscreen' | 'bottom-sheet'

const isOpen = ref(false)
const name = ref('Ruby Cheung')
const email = ref('ruby@example.com')
const notes = ref('')
const isTouchOriented = useMediaQuery(TOUCH_ORIENTED_LG_QUERY)

const purpose: DialogPurpose = 'form'
const touchPresentation: AdaptivePresentation = 'bottom-sheet'
const bottomSheetHeight: BottomSheetHeight = 'tall'

function resolvedPresentation(): AdaptivePresentation {
  return isTouchOriented.value ? touchPresentation : 'dialog'
}
</script>

<template>
  <Button label="Edit profile" @click="isOpen = true" />
  <BottomSheet
    v-if="resolvedPresentation() === 'bottom-sheet'"
    v-model:is-open="isOpen"
    :height="bottomSheetHeight"
    label="Edit profile"
    :purpose="purpose as BottomSheetPurpose"
  >
    <VStack :gap="4">
      <Heading :level="3">
        Edit profile
      </Heading>
      <Text type="supporting" color="secondary">
        Dialog remains the default presentation. This example explicitly opts into a Bottom Sheet only at lg and below when the device has a coarse pointer and no hover. Pass the presentation prop to make tests or unusual environments deterministic.
      </Text>
      <Text type="supporting" color="secondary">
        In Bottom Sheet presentation, purpose="form" blocks scrim clicks and swipe dismissal while preserving Escape. Use this opt-in only when that contract is acceptable; keep AlertDialog/destructive confirmations on Dialog unless a product deliberately chooses otherwise.
      </Text>
      <TextInput v-model="name" label="Name" />
      <TextInput v-model="email" label="Email" type="email" />
      <TextArea v-model="notes" label="Notes" :rows="6" />
      <HStack :gap="2" h-align="end" wrap="wrap">
        <Button label="Cancel" variant="secondary" @click="isOpen = false" />
        <Button label="Save profile" variant="primary" @click="isOpen = false" />
      </HStack>
    </VStack>
  </BottomSheet>
  <Dialog
    v-else
    :is-open="isOpen"
    max-height="75dvh"
    :purpose="purpose"
    :variant="resolvedPresentation() === 'fullscreen' ? 'fullscreen' : 'standard'"
    :width="480"
    @open-change="isOpen = $event"
  >
    <Layout>
      <template #header>
        <DialogHeader title="Edit profile" @open-change="isOpen = $event" />
      </template>
      <LayoutContent>
        <VStack :gap="4">
          <Text type="supporting" color="secondary">
            Dialog remains the default presentation. This example explicitly opts into a Bottom Sheet only at lg and below when the device has a coarse pointer and no hover. Pass the presentation prop to make tests or unusual environments deterministic.
          </Text>
          <Text type="supporting" color="secondary">
            In Bottom Sheet presentation, purpose="form" blocks scrim clicks and swipe dismissal while preserving Escape. Use this opt-in only when that contract is acceptable; keep AlertDialog/destructive confirmations on Dialog unless a product deliberately chooses otherwise.
          </Text>
          <TextInput v-model="name" label="Name" />
          <TextInput v-model="email" label="Email" type="email" />
          <TextArea v-model="notes" label="Notes" :rows="6" />
        </VStack>
      </LayoutContent>
      <template #footer>
        <LayoutFooter>
          <HStack :gap="2" h-align="end" wrap="wrap">
            <Button label="Cancel" variant="secondary" @click="isOpen = false" />
            <Button label="Save profile" variant="primary" @click="isOpen = false" />
          </HStack>
        </LayoutFooter>
      </template>
    </Layout>
  </Dialog>
</template>
```

### Confirmation

Asks before a destructive action. The inline preview stays on the page; its buttons open the real dialog.

```vue
<script setup lang="ts">
import { Dialog, useImperativeDialog } from '@astryx-vue/core/Dialog'
import { h } from 'vue'
import ConfirmBody from './confirm-body.vue'

const dialog = useImperativeDialog({ width: 400, purpose: 'form' })

function openDialog() {
  dialog.show(() => h(ConfirmBody, { requestClose: () => dialog.hide() }))
}
</script>

<template>
  <Dialog is-open is-inline :width="400" purpose="form">
    <ConfirmBody :request-close="openDialog" />
  </Dialog>
  <component :is="dialog.Host" />
</template>
```

### Form

Collects a name and bio without leaving the page. purpose form keeps Escape and ignores backdrop clicks.

```vue
<script setup lang="ts">
import { Dialog, useImperativeDialog } from '@astryx-vue/core/Dialog'
import { h } from 'vue'
import FormBody from './form-body.vue'

const dialog = useImperativeDialog({ purpose: 'form', width: 480 })

function openDialog() {
  dialog.show(() => h(FormBody, { requestClose: () => dialog.hide() }))
}
</script>

<template>
  <Dialog is-open is-inline purpose="form" :width="480">
    <FormBody :request-close="openDialog" />
  </Dialog>
  <component :is="dialog.Host" />
</template>
```

### Fullscreen

variant fullscreen fills the viewport for documentation and other long reading.

```vue
<script setup lang="ts">
import { Button } from '@astryx-vue/core/Button'
import { Card } from '@astryx-vue/core/Card'
import { Dialog, DialogHeader } from '@astryx-vue/core/Dialog'
import { Layout, LayoutContent, LayoutFooter } from '@astryx-vue/core/Layout'
import { HStack, VStack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
import { ref } from 'vue'

const isOpen = ref(false)

const sections = [
  { title: 'Getting started', body: 'Create your first project by clicking New Project in the sidebar. Choose a template or start from scratch.' },
  { title: 'Team members', body: 'Invite collaborators from Settings > Team. Each member can have Admin, Editor, or Viewer permissions.' },
  { title: 'Billing', body: 'Free plans include up to 3 projects. Upgrade to Pro for unlimited projects and priority support.' },
  { title: 'API access', body: 'Generate API keys from Settings > Developer. Rate limits are 1,000 requests per minute on free plans.' },
  { title: 'Data export', body: 'Export your data anytime from Settings > Data. Exports are available as CSV or JSON within 24 hours.' },
]
</script>

<template>
  <Card>
    <VStack :gap="3">
      <VStack :gap="1">
        <Text type="body" weight="bold">
          Help &amp; Documentation
        </Text>
        <Text type="supporting" color="secondary">
          5 articles · Last updated Apr 2026
        </Text>
      </VStack>
      <Button label="Open documentation" variant="secondary" @click="isOpen = true" />
    </VStack>
  </Card>
  <Dialog :is-open="isOpen" variant="fullscreen" @open-change="isOpen = $event">
    <Layout>
      <template #header>
        <DialogHeader
          title="Documentation"
          subtitle="Everything you need to get started"
          has-close-button
          @open-change="isOpen = $event"
        />
      </template>
      <LayoutContent>
        <VStack :gap="4">
          <VStack v-for="section in sections" :key="section.title" :gap="1">
            <Text type="body" weight="bold">
              {{ section.title }}
            </Text>
            <Text type="body">
              {{ section.body }}
            </Text>
          </VStack>
        </VStack>
      </LayoutContent>
      <template #footer>
        <LayoutFooter>
          <HStack h-align="end">
            <Button label="Close" variant="primary" @click="isOpen = false" />
          </HStack>
        </LayoutFooter>
      </template>
    </Layout>
  </Dialog>
</template>
```

### Scrollable

maxHeight keeps the dialog short and scrolls the terms inside it.

```vue
<script setup lang="ts">
import { Dialog, useImperativeDialog } from '@astryx-vue/core/Dialog'
import { h } from 'vue'
import ScrollBody from './scroll-body.vue'

const dialog = useImperativeDialog({ maxHeight: '50dvh' })

function openDialog() {
  dialog.show(() => h(ScrollBody, { requestClose: () => dialog.hide() }))
}
</script>

<template>
  <Dialog is-open is-inline :max-height="360">
    <ScrollBody :request-close="openDialog" />
  </Dialog>
  <component :is="dialog.Host" />
</template>
```

### Required

purpose required blocks Escape and the backdrop. The user has to choose Cancel or Transfer.

```vue
<script setup lang="ts">
import { Dialog, useImperativeDialog } from '@astryx-vue/core/Dialog'
import { h } from 'vue'
import RequiredBody from './required-body.vue'

const dialog = useImperativeDialog({ purpose: 'required' })

function openDialog() {
  dialog.show(() => h(RequiredBody, { requestClose: () => dialog.hide() }))
}
</script>

<template>
  <Dialog is-open is-inline purpose="required">
    <RequiredBody :request-close="openDialog" />
  </Dialog>
  <component :is="dialog.Host" />
</template>
```

## Accessibility

`Dialog` is built on the native `<dialog>` element, so the modal behavior, the focus trap, and the focus handoff on close all come from the browser rather than from extra configuration.

The dialog is shown with `showModal()`, which makes the rest of the page inert while it is open. When it opens, initial focus goes to the first element with `data-autofocus`; if no element has that attribute, focus goes to the `DialogHeader` title.

The dialog takes its name from the `DialogHeader` title, which the component associates with the dialog through `aria-labelledby`. An explicit `aria-label` or `aria-labelledby` replaces that name with the one you provide.

Escape and backdrop clicks do not close the dialog directly. They emit `openChange(false)`, and the parent decides whether to close. When the dialog closes, focus returns to the element that was focused when it opened, as long as that element is still in the page.

## Limitations

Two constraints shape what you can put inside a dialog, and both are worth knowing before you combine it with other components.

- Tooltip and Popover render into `body`. Inside an open `Dialog` they sit beneath the modal top layer, so they cannot be seen or used. Do not nest them inside a dialog until they are moved into the dialog's own layer.
- Dialogs cannot be nested. If a flow needs several steps, restructure it as steps within a single dialog rather than opening a second one.

## Layout

The standard variant sizes itself with `width` and `maxHeight`, and clamps to the viewport with spacing gutters on narrow screens. `position` places it at a static offset. Logical `start` and `end` offsets mirror under RTL.

## Dialog Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `isOpen` | `boolean` | — | Whether the dialog is open. Required. |
| `isInline` | `boolean` | `false` | Renders content in normal flow without the modal dialog, backdrop, or focus behavior. For documentation previews only. |
| `width` | `number \| string` | `400` | Preferred width. Numbers are pixels. Clamped to the viewport on narrow screens. Ignored by `fullscreen`. |
| `maxHeight` | `number \| string` | `'75vh'` | Maximum height. Numbers are pixels. Ignored by `fullscreen`. |
| `position` | `DialogPosition` | Centered | Static offsets: `top`, `bottom`, `start`, `end`. Logical `start` and `end` mirror under RTL. Ignored by `fullscreen`. |
| `variant` | `'standard' \| 'fullscreen'` | `'standard'` | Standard dialog or a dialog that fills the viewport. |
| `purpose` | `'required' \| 'form' \| 'info'` | `'info'` | `required` disables Escape and backdrop dismissal. `form` allows Escape only. `info` allows both. |
| `padding` | `0 \| 0.5 \| 1 \| 1.5 \| 2 \| 3 \| 4 \| 5 \| 6 \| 8 \| 10` | Theme default (16px) | Inner padding, mapped to the `--astryx-spacing-*` tokens. |

## Events

- `openChange(isOpen: boolean)`: fires with `false` when the user requests dismissal through Escape or a backdrop click, depending on `purpose`. The parent sets `isOpen` to close.

## Slots

- `default`: dialog content. Usually a `DialogHeader` followed by body content and actions.

## DialogHeader Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `title` | `string` | — | Dialog title. Receives default focus on open and names the dialog. Required. |
| `subtitle` | `string` | — | Secondary text below the title. Not rendered when empty and no `subtitle` slot is set. |
| `hasCloseButton` | `boolean` | `false` | Shows a close button that emits `openChange(false)`. |
| `endContentEdgeCompensation` | `'inline' \| 'block' \| 'all'` | `all` when a close button is shown | Which edges of the trailing slot are compensated. Omitted with a close button compensates block and inline end. |
| `closeButtonLabel` | `string` | Active language | Accessible name of the close button. Defaults to Close in English and 关闭 in Chinese. |
| `hasDivider` | `boolean` | `false` | Draws a themed rule along the bottom edge, using `--astryx-color-border`. |

## DialogHeader Events and Slots

- `openChange(isOpen: boolean)`: fires with `false` from the close button.
- `title`: inline content in place of the title string.
- `subtitle`: inline content for the subtitle.
- `startContent`: content before the title, such as a back button.
- `endContent`: content after the title, before the close button.

## useImperativeDialog

- `show(content, options?)`: opens the dialog with content. Options merge with the defaults passed to the hook.
- `hide()`: closes the dialog.
- `isOpen`: readonly ref of the open state.
- `Host`: component to render once in the template.

## Theme

```css
.my-dialog {
  --astryx-dialog-background: #fff;
  --astryx-dialog-foreground: #000;
}
```

- `--astryx-dialog-background`: surface color.
- `--astryx-dialog-foreground`: text color.
- `--astryx-dialog-overlay`: backdrop color.
- `--astryx-dialog-shadow`: surface shadow.
- `--astryx-dialog-padding`: default inner padding when the `padding` prop is not set.
- `--astryx-dialog-title-color`: header title color.
