Alert
A short message that draws attention, with an optional icon.
Plain HTML
<script setup lang="ts">
import * as Alert from '@/components/ui/alert';
</script>
<template>
<div class="stack">
<Alert.Root>
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
aria-hidden="true"
>
<circle cx="12" cy="12" r="10" />
<path d="m9 12 2 2 4-4" />
</svg>
<Alert.Title>Success! Your changes have been saved</Alert.Title>
<Alert.Description
>This is an alert with an icon, a title and a description.</Alert.Description
>
</Alert.Root>
<Alert.Root variant="destructive">
<Alert.Title>Unable to process your payment.</Alert.Title>
<Alert.Description>Please verify your billing information and try again.</Alert.Description>
</Alert.Root>
</div>
</template>
<style scoped>
.stack {
display: grid;
gap: var(--dui-space-3);
}
</style>
Installation
npx distill-ui add alert- Set up the tokens once, if you haven't: see manual installation.
- Copy these files into your project:
<script setup lang="ts">
const { variant = 'default' } = defineProps<{ variant?: 'default' | 'destructive' }>();
</script>
<template>
<div role="alert" class="alert" :data-variant="variant"><slot /></div>
</template>
<!--
Options you can set from a parent or on the alert itself:
--dui-alert-border, --dui-alert-radius, --dui-alert-bg, --dui-alert-fg
-->
<style scoped>
.alert {
position: relative;
display: grid;
grid-template-columns: 1fr;
row-gap: var(--dui-space-0-5);
width: 100%;
padding: var(--dui-space-3) var(--dui-space-4);
border: 1px solid var(--dui-alert-border, var(--dui-color-border));
border-radius: var(--dui-alert-radius, var(--dui-radius-lg));
background: var(--dui-alert-bg, var(--dui-color-card));
color: var(--dui-alert-fg, var(--dui-color-card-foreground));
font-size: var(--dui-text-sm);
line-height: var(--dui-text-sm-line-height);
/* An optional icon passed as the first child gets its own column. */
&:has(> :deep(svg)) {
grid-template-columns: auto 1fr;
column-gap: var(--dui-space-2-5);
}
& > :deep(svg) {
grid-row: span 2;
width: 1rem;
height: 1rem;
translate: 0 0.125rem;
color: currentColor;
}
}
.alert[data-variant='destructive'] {
/* The destructive text token passes AA contrast in both themes. */
color: var(--dui-alert-fg, var(--dui-color-destructive-text));
}
</style>
<template>
<div class="description"><slot /></div>
</template>
<style scoped>
.description {
grid-column: -2;
text-wrap: pretty;
}
</style>
<template>
<div class="title"><slot /></div>
</template>
<style scoped>
.title {
/* The last column, whether or not the alert has an icon. */
grid-column: -2;
font-weight: var(--dui-weight-medium);
}
</style>
export { default as Root } from './Alert.vue';
export { default as Title } from './AlertTitle.vue';
export { default as Description } from './AlertDescription.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-border | var(--dui-color-border) |
--dui-alert-radius | var(--dui-radius-lg) |
--dui-alert-bg | var(--dui-color-card) |
--dui-alert-fg | var(--dui-color-card-foreground) |