Toast
A short notification that appears in a corner and goes away on its own.
Our own toast store
<script setup lang="ts">
import { Button } from '@/components/ui/button';
import { toast } from '@/components/ui/toast';
// Place <Toaster /> once in App.vue, then call toast() from anywhere.
function showToast() {
toast('Event has been created', {
description: 'Sunday, December 3 at 9:00 AM',
action: { label: 'Undo', onClick: () => toast('Undone') }
});
}
</script>
<template>
<div class="row">
<Button variant="outline" @click="showToast">Show toast</Button>
<Button variant="outline" @click="toast.success('Profile saved')">Success</Button>
<Button variant="outline" @click="toast.error('Something went wrong')">Error</Button>
</div>
</template>
<style scoped>
.row {
display: flex;
flex-wrap: wrap;
gap: var(--dui-space-2);
}
</style>
Installation
npx distill-ui add toast- Set up the tokens once, if you haven't: see manual installation.
- Copy these files into your project:
<script setup lang="ts">
import { toaster } from './toaster.js';
</script>
<template>
<section
class="toaster"
aria-label="Notifications"
@mouseenter="toaster.pause()"
@mouseleave="toaster.resume()"
>
<div
v-for="t in toaster.toasts"
:key="t.id"
role="alert"
class="toast"
:data-variant="t.data.variant ?? 'default'"
>
<div class="text">
<div class="title">{{ t.data.title }}</div>
<div v-if="t.data.description" class="description">{{ t.data.description }}</div>
</div>
<button
v-if="t.data.action"
type="button"
class="action"
@click="
t.data.action.onClick();
toaster.remove(t.id);
"
>
{{ t.data.action.label }}
</button>
<button type="button" class="close" aria-label="Close" @click="toaster.remove(t.id)">
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d="M18 6 6 18" />
<path d="m6 6 12 12" />
</svg>
</button>
</div>
</section>
</template>
<!--
Options you can set from a parent or on the toaster itself:
--dui-toast-width, --dui-toast-radius, --dui-toast-bg, --dui-toast-fg
-->
<style scoped>
.toaster {
position: fixed;
inset: auto var(--dui-space-4) var(--dui-space-4) auto;
display: flex;
flex-direction: column;
gap: var(--dui-space-2);
z-index: var(--dui-z-toast);
width: min(var(--dui-toast-width, 22rem), calc(100% - var(--dui-space-8)));
}
.toast {
position: relative;
display: flex;
align-items: center;
gap: var(--dui-space-3);
padding: var(--dui-space-4);
padding-inline-end: var(--dui-space-8);
border-radius: var(--dui-toast-radius, var(--dui-radius-lg));
background: var(--dui-toast-bg, var(--dui-color-popover));
color: var(--dui-toast-fg, var(--dui-color-popover-foreground));
box-shadow:
0 0 0 1px color-mix(in oklch, var(--dui-color-foreground) 10%, transparent),
var(--dui-shadow-lg);
font-size: var(--dui-text-sm);
line-height: var(--dui-text-sm-line-height);
outline: none;
transition:
opacity var(--dui-duration-slow) var(--dui-ease-out),
translate var(--dui-duration-slow) var(--dui-ease-out);
@starting-style {
opacity: 0;
translate: 0 var(--dui-space-2);
}
}
.toast[data-variant='success'] {
border-inline-start: 3px solid var(--dui-color-success);
}
.toast[data-variant='error'] {
border-inline-start: 3px solid var(--dui-color-destructive);
}
.text {
display: grid;
gap: var(--dui-space-0-5);
flex: 1;
min-width: 0;
}
.title {
font-weight: var(--dui-weight-medium);
}
.description {
color: color-mix(in oklch, var(--dui-color-muted-foreground) 70%, var(--dui-color-foreground));
}
.action {
flex-shrink: 0;
height: 1.75rem;
padding-inline: var(--dui-space-2-5);
border: 0;
border-radius: var(--dui-radius-md);
background: var(--dui-color-primary);
color: var(--dui-color-primary-foreground);
font: inherit;
font-size: var(--dui-text-xs);
font-weight: var(--dui-weight-medium);
cursor: pointer;
}
.close {
position: absolute;
top: var(--dui-space-2);
right: var(--dui-space-2);
display: inline-flex;
align-items: center;
justify-content: center;
width: 1.5rem;
height: 1.5rem;
padding: 0;
border: 0;
border-radius: var(--dui-radius-sm);
background: transparent;
color: var(--dui-color-foreground);
cursor: pointer;
opacity: 0.6;
&:hover {
opacity: 1;
}
svg {
width: 0.875rem;
height: 0.875rem;
fill: none;
stroke: currentColor;
stroke-width: 2;
stroke-linecap: round;
}
}
.action,
.close {
&:focus-visible {
outline: none;
box-shadow: 0 0 0 var(--dui-ring-width)
color-mix(in oklch, var(--dui-color-ring) 50%, transparent);
opacity: 1;
}
}
</style>
export { default as Toaster } from './Toaster.vue';
export { toast, toaster, type ToastData, type ToastVariant } from './toaster.js';
import { reactive } from 'vue';
/*
* One toaster for the whole app. Call `toast()` from anywhere and the <Toaster />
* component (placed once, usually in App.vue) shows it.
*/
export type ToastVariant = 'default' | 'success' | 'error';
export type ToastData = {
title: string;
description?: string;
variant?: ToastVariant;
action?: { label: string; onClick: () => void };
};
type ToastOptions = Omit<ToastData, 'title'> & { duration?: number };
type Toast = { id: string; data: ToastData };
const closeDelay = 5000;
let nextId = 0;
// Each toast closes after its delay. Hovering the toaster pauses every timer and
// leaving it restarts them with the time they had left.
const timers = new Map<string, { remaining: number; started: number; handle?: number }>();
function start(id: string) {
const timer = timers.get(id);
if (!timer) return;
timer.started = Date.now();
timer.handle = window.setTimeout(() => toaster.remove(id), timer.remaining);
}
export const toaster = reactive({
toasts: [] as Toast[],
add(data: ToastData, duration = closeDelay) {
const id = `toast-${nextId++}`;
this.toasts.push({ id, data });
timers.set(id, { remaining: duration, started: 0 });
start(id);
return id;
},
remove(id: string) {
window.clearTimeout(timers.get(id)?.handle);
timers.delete(id);
this.toasts = this.toasts.filter((t) => t.id !== id);
},
pause() {
for (const timer of timers.values()) {
window.clearTimeout(timer.handle);
timer.remaining -= Date.now() - timer.started;
}
},
resume() {
for (const id of timers.keys()) start(id);
}
});
function show(title: string, { duration, ...data }: ToastOptions = {}) {
return toaster.add({ title, ...data }, duration);
}
/** `toast('Saved')`, `toast.success('Saved')`, `toast.error('Failed', { description })`. */
export const toast = Object.assign(show, {
success: (title: string, options: Omit<ToastOptions, 'variant'> = {}) =>
show(title, { ...options, variant: 'success' }),
error: (title: string, options: Omit<ToastOptions, 'variant'> = {}) =>
show(title, { ...options, variant: 'error' }),
dismiss: (id: string) => toaster.remove(id)
});
Customizing
Set any of these on the component or on a parent element. See Customizing for how they work.
| Custom property | Default |
|---|---|
--dui-toast-width | 22rem |
--dui-toast-radius | var(--dui-radius-lg) |
--dui-toast-bg | var(--dui-color-popover) |
--dui-toast-fg | var(--dui-color-popover-foreground) |