Alert Dialog

ARIAGitHubnpm
A modal dialog that interrupts the user with important content and expects a response.
<script setup lang="ts">
import {
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogOverlay,
  AlertDialogPortal,
  AlertDialogRoot,
  AlertDialogTitle,
  AlertDialogTrigger,
} from 'akar';
import './styles.css';

function handleAction() {
  // eslint-disable-next-line no-alert
  alert('clicked action button!');
}
</script>

<template>
  <div class="akar-demo akar-demo-AlertDialog">
    <AlertDialogRoot>
      <AlertDialogTrigger class="Button green">
        Delete account
      </AlertDialogTrigger>
      <AlertDialogPortal>
        <AlertDialogOverlay class="AlertDialogOverlay" />
        <AlertDialogContent class="AlertDialogContent">
          <AlertDialogTitle class="AlertDialogTitle">
            Are you absolutely sure?
          </AlertDialogTitle>
          <AlertDialogDescription class="AlertDialogDescription">
            This action cannot be undone. This will permanently delete your account and remove your data from our servers.
          </AlertDialogDescription>
          <div :style="{ display: 'flex', gap: 25, justifyContent: 'flex-end' }">
            <AlertDialogCancel class="Button mauve">
              Cancel
            </AlertDialogCancel>
            <AlertDialogAction
              class="Button red"
              @click="handleAction"
            >
              Yes, delete account
            </AlertDialogAction>
          </div>
        </AlertDialogContent>
      </AlertDialogPortal>
    </AlertDialogRoot>
  </div>
</template>

Features

  • Focus is automatically trapped.
  • Can be controlled or uncontrolled.
  • Manages screen reader announcements with Title and Description components.
  • Esc closes the component automatically.

Installation

Install the component from your command line.

pnpm add akar

Anatomy

Import all parts and piece them together.

<script setup lang="ts">
import {
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogOverlay,
  AlertDialogPortal,
  AlertDialogRoot,
  AlertDialogTitle,
  AlertDialogTrigger,
} from 'akar';
</script>

<template>
  <AlertDialogRoot>
    <AlertDialogTrigger />
    <AlertDialogPortal>
      <AlertDialogOverlay />
      <AlertDialogContent>
        <AlertDialogTitle />
        <AlertDialogDescription />
        <AlertDialogCancel />
        <AlertDialogAction />
      </AlertDialogContent>
    </AlertDialogPortal>
  </AlertDialogRoot>
</template>

API Reference

Root

Contains all the parts of an alert dialog.

PropDefaultType
defaultOpen
boolean

The open state of the dialog when it is initially rendered. Use when you do not need to control its open state.

open
boolean

The controlled open state of the dialog. Can be binded as v-model:open.

unmountOnHide
true
boolean

When set to false, the dialog content will not be unmounted when closed, but instead hidden with CSS.

Useful for SEO or when you want to improve performance by not remounting the component on every open.

EventPayload
update:open
value: boolean

Event handler called when the open state of the dialog changes.

Slot (default)Payload
open
boolean

Current open state

close
(): void

Close the dialog

AlertDialogRoot.vue source

Trigger

A button that opens the dialog.

PropDefaultType
as
"button"
AsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChild
boolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

AlertDialogTrigger.vue source

Data attribute Value
[data-state]
"open" | "closed"

Portal

When used, portals your overlay and content parts into the body.

PropDefaultType
defer
boolean

Defer the resolving of a Teleport target until other parts of the application have mounted (requires Vue 3.5.0+)

reference

disabled
boolean

Disable teleport and render the component inline

reference

forceMount
boolean

Used to force mounting when more control is needed. Useful when controlling animation with Vue animation libraries.

to
string | HTMLElement

Vue native teleport component prop :to

reference

AlertDialogPortal.vue source

Overlay

A layer that covers the inert portion of the view when the dialog is open.

PropDefaultType
as
"div"
AsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChild
boolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

forceMount
boolean

Used to force mounting when more control is needed. Useful when controlling animation with Vue animation libraries.

AlertDialogOverlay.vue source

Data attribute Value
[data-state]
"open" | "closed"

Content

Contains content to be rendered when the dialog is open.

PropDefaultType
as
"div"
AsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChild
boolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

disableOutsidePointerEvents
boolean

When true, hover/focus/click interactions will be disabled on elements outside the DismissableLayer. Users will need to click twice on outside elements to interact with them: once to close the DismissableLayer, and again to trigger the element.

forceMount
boolean

Used to force mounting when more control is needed. Useful when controlling animation with Vue animation libraries.

EventPayload
closeAutoFocus
event: Event

Event handler called when auto-focusing on close. Can be prevented.

escapeKeyDown
event: KeyboardEvent

Event handler called when the escape key is down. Can be prevented.

focusOutside
event: FocusOutsideEvent

Event handler called when the focus moves outside of the DismissableLayer. Can be prevented.

interactOutside
event: PointerDownOutsideEvent | FocusOutsideEvent

Event handler called when an interaction happens outside the DismissableLayer. Specifically, when a pointerdown event happens outside or focus moves outside of it. Can be prevented.

openAutoFocus
event: Event

Event handler called when auto-focusing on open. Can be prevented.

pointerDownOutside
event: PointerDownOutsideEvent

Event handler called when a pointerdown event happens outside of the DismissableLayer. Can be prevented.

AlertDialogContent.vue source

Data attribute Value
[data-state]
"open" | "closed"

Cancel

A button that closes the dialog. This button should be distinguished visually from AlertDialogAction buttons.

PropDefaultType
as
"button"
AsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChild
boolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

AlertDialogCancel.vue source

Action

A button that closes the dialog. These buttons should be distinguished visually from the AlertDialogCancel button.

PropDefaultType
as
"button"
AsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChild
boolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

AlertDialogAction.vue source

Title

An accessible name to be announced when the dialog is opened. Alternatively, you can provide aria-label or aria-labelledby to AlertDialogContent and exclude this component.

PropDefaultType
as
"h2"
AsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChild
boolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

AlertDialogTitle.vue source

Description

An accessible description to be announced when the dialog is opened. Alternatively, you can provide aria-describedby to AlertDialogContent and exclude this component.

PropDefaultType
as
"p"
AsTag | Component

The element or component this component should render as. Can be overwritten by asChild.

asChild
boolean

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read our Composition guide for more details.

AlertDialogDescription.vue source

Examples

Close after asynchronous form submission

Use the controlled props to programmatically close the Alert Dialog after an async operation has completed.

<script setup>
import {
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogOverlay,
  AlertDialogPortal,
  AlertDialogRoot,
  AlertDialogTitle,
  AlertDialogTrigger,
} from 'akar'

const wait = () => new Promise(resolve => setTimeout(resolve, 1000))
const open = ref(false)
</script>

<template>
  <AlertDialogRoot v-model:open="open">
    <AlertDialogTrigger>Open</AlertDialogTrigger>
    <AlertDialogPortal>
      <AlertDialogOverlay />
      <AlertDialogContent>
        <form
          @submit.prevent="
            (event) => {
              wait().then(() => open = false);
            }
          "
        >
          <!-- some inputs -->
          <button type="submit">
            Submit
          </button>
        </form>
      </AlertDialogContent>
    </AlertDialogPortal>
  </AlertDialogRoot>
</template>

Custom portal container

Customise the element that your alert dialog portals into.

<script setup>
import { ref } from 'vue'

const container = ref(null)
</script>

<template>
  <div>
    <AlertDialogRoot>
      <AlertDialogTrigger />
      <AlertDialogPortal :to="container">
        <AlertDialogOverlay />
        <AlertDialogContent>...</AlertDialogContent>
      </AlertDialogPortal>
    </AlertDialogRoot>

    <div ref="container" />
  </div>
</template>

Accessibility

Adheres to the Alert and Message Dialogs WAI-ARIA design pattern.

Keyboard Interactions

Key Description
Space
Opens/closes the dialog.
Enter
Opens/closes the dialog.
Tab
Moves focus to the next focusable element.
Shift + Tab
Moves focus to the previous focusable element.
Esc
Closes the dialog and moves focus to AlertDialogTrigger.