Alert Dialog
A dialog that interrupts to ask for a decision. Clicking outside does not close it.
Reka UI Alert Dialog
<script setup lang="ts">
import * as AlertDialog from '@/components/ui/alert-dialog';
</script>
<template>
<AlertDialog.Root>
<AlertDialog.Trigger variant="outline">Delete account</AlertDialog.Trigger>
<AlertDialog.Content>
<AlertDialog.Header>
<AlertDialog.Title>Are you absolutely sure?</AlertDialog.Title>
<AlertDialog.Description>
This action cannot be undone. This will permanently delete your account.
</AlertDialog.Description>
</AlertDialog.Header>
<AlertDialog.Footer>
<AlertDialog.Cancel>Cancel</AlertDialog.Cancel>
<AlertDialog.Action>Continue</AlertDialog.Action>
</AlertDialog.Footer>
</AlertDialog.Content>
</AlertDialog.Root>
</template>
Installation
npx distill-ui add alert-dialogIt also copies Button, which Alert Dialog uses.
It installs reka-ui if you don't have it yet.
- Set up the tokens once, if you haven't: see manual installation.
- Install the packages it needs:
npm install reka-ui@2.11.0 - Copy Button too, which it uses.
- Copy these files into your project:
<script setup lang="ts">
import {
AlertDialogRoot,
useForwardPropsEmits,
type AlertDialogEmits,
type AlertDialogProps
} from 'reka-ui';
// Use `v-model:open` to control it from outside.
const props = defineProps<AlertDialogProps>();
const emits = defineEmits<AlertDialogEmits>();
const forwarded = useForwardPropsEmits(props, emits);
</script>
<template>
<AlertDialogRoot v-bind="forwarded">
<slot />
</AlertDialogRoot>
</template>
<script setup lang="ts">
import { AlertDialogAction } from 'reka-ui';
import { Button, type ButtonSize, type ButtonVariant } from '../button/index.js';
// Confirms: runs its @click, then closes. `<AlertDialog.Action @click="remove">`.
const { variant = 'default', size } = defineProps<{ variant?: ButtonVariant; size?: ButtonSize }>();
</script>
<template>
<AlertDialogAction as-child>
<Button :variant="variant" :size="size"><slot /></Button>
</AlertDialogAction>
</template>
<script setup lang="ts">
import { AlertDialogCancel } from 'reka-ui';
import { Button, type ButtonSize, type ButtonVariant } from '../button/index.js';
// Closes without doing anything. Reka focuses it when the dialog opens.
const { variant = 'outline', size } = defineProps<{ variant?: ButtonVariant; size?: ButtonSize }>();
</script>
<template>
<AlertDialogCancel as-child>
<Button :variant="variant" :size="size"><slot /></Button>
</AlertDialogCancel>
</template>
<script setup lang="ts">
import {
AlertDialogContent,
AlertDialogOverlay,
useForwardPropsEmits,
type AlertDialogContentEmits,
type AlertDialogContentProps
} from 'reka-ui';
defineOptions({ inheritAttrs: false });
// An alert dialog needs an answer, so Reka doesn't close it on an outside click.
const props = defineProps<AlertDialogContentProps>();
const emits = defineEmits<AlertDialogContentEmits>();
const forwarded = useForwardPropsEmits(props, emits);
</script>
<template>
<!-- No portal: the content is position: fixed, so it stays inside your styles and tokens. -->
<AlertDialogOverlay as-child>
<div class="overlay"></div>
</AlertDialogOverlay>
<AlertDialogContent v-bind="forwarded" as-child>
<div v-bind="$attrs" class="content"><slot /></div>
</AlertDialogContent>
</template>
<!--
Options you can set from a parent or on the alert dialog content itself:
--dui-alert-dialog-overlay-bg, --dui-alert-dialog-max-width, --dui-alert-dialog-padding, --dui-alert-dialog-radius, --dui-alert-dialog-bg, --dui-alert-dialog-fg
-->
<style scoped>
/* Reka waits for animationend before removing the dialog, so this animates with
the shared keyframes on [data-state] rather than transitions. */
.overlay {
position: fixed;
inset: 0;
z-index: var(--dui-z-overlay);
background: var(--dui-alert-dialog-overlay-bg, var(--dui-color-overlay));
backdrop-filter: blur(4px);
&[data-state='open'] {
animation: distill-fade-in var(--dui-duration-fast) var(--dui-ease-out);
}
&[data-state='closed'] {
animation: distill-fade-out var(--dui-duration-fast) var(--dui-ease-out);
}
}
.content {
position: fixed;
inset: 0;
z-index: var(--dui-z-modal);
display: grid;
gap: var(--dui-space-6);
width: 100%;
max-width: min(var(--dui-alert-dialog-max-width, 32rem), calc(100% - var(--dui-space-8)));
height: fit-content;
margin: auto;
padding: var(--dui-alert-dialog-padding, var(--dui-space-6));
border-radius: var(--dui-alert-dialog-radius, var(--dui-radius-xl));
background: var(--dui-alert-dialog-bg, var(--dui-color-popover));
color: var(--dui-alert-dialog-fg, var(--dui-color-popover-foreground));
box-shadow: 0 0 0 1px color-mix(in oklch, var(--dui-color-foreground) 10%, transparent);
font-size: var(--dui-text-sm);
line-height: var(--dui-text-sm-line-height);
outline: none;
&[data-state='open'] {
animation: distill-zoom-in var(--dui-duration-fast) var(--dui-ease-out);
}
&[data-state='closed'] {
animation: distill-zoom-out var(--dui-duration-fast) var(--dui-ease-out);
}
}
</style>
<script setup lang="ts">
import { AlertDialogDescription } from 'reka-ui';
</script>
<template>
<!-- Reka gives it an id and points the dialog's aria-describedby at it. -->
<AlertDialogDescription as-child>
<p class="description"><slot /></p>
</AlertDialogDescription>
</template>
<style scoped>
.description {
margin: 0;
color: var(--dui-color-muted-foreground);
font-size: var(--dui-text-sm);
line-height: var(--dui-text-sm-line-height);
}
</style>
<template>
<div class="footer"><slot /></div>
</template>
<style scoped>
.footer {
display: flex;
flex-direction: column-reverse;
gap: var(--dui-space-2);
@media (width >= 40rem) {
flex-direction: row;
justify-content: flex-end;
}
}
</style>
<template>
<div class="header"><slot /></div>
</template>
<style scoped>
.header {
display: flex;
flex-direction: column;
gap: var(--dui-space-2);
}
</style>
<script setup lang="ts">
import { AlertDialogTitle } from 'reka-ui';
</script>
<template>
<!-- Reka gives it an id and points the dialog's aria-labelledby at it. -->
<AlertDialogTitle as-child>
<h2 class="title"><slot /></h2>
</AlertDialogTitle>
</template>
<style scoped>
.title {
margin: 0;
font-size: var(--dui-text-base);
line-height: 1;
font-weight: var(--dui-weight-medium);
}
</style>
<script setup lang="ts">
import { AlertDialogTrigger } from 'reka-ui';
import { Button, type ButtonSize, type ButtonVariant } from '../button/index.js';
// Renders our Button, so it takes the same variant and size props.
const { variant = 'default', size } = defineProps<{ variant?: ButtonVariant; size?: ButtonSize }>();
</script>
<template>
<AlertDialogTrigger as-child>
<Button :variant="variant" :size="size"><slot /></Button>
</AlertDialogTrigger>
</template>
export { default as Root } from './AlertDialog.vue';
export { default as Trigger } from './AlertDialogTrigger.vue';
export { default as Content } from './AlertDialogContent.vue';
export { default as Header } from './AlertDialogHeader.vue';
export { default as Footer } from './AlertDialogFooter.vue';
export { default as Title } from './AlertDialogTitle.vue';
export { default as Description } from './AlertDialogDescription.vue';
export { default as Action } from './AlertDialogAction.vue';
export { default as Cancel } from './AlertDialogCancel.vue';
Customizing
Set any of these on the component or on a parent element. See Customizing for how they work.
| Custom property | Default |
|---|---|
--dui-alert-dialog-overlay-bg | var(--dui-color-overlay) |
--dui-alert-dialog-max-width | 32rem |
--dui-alert-dialog-padding | var(--dui-space-6) |
--dui-alert-dialog-radius | var(--dui-radius-xl) |
--dui-alert-dialog-bg | var(--dui-color-popover) |
--dui-alert-dialog-fg | var(--dui-color-popover-foreground) |