Animated Toast Stack
Preview
Toasts render fixed on the screen. Change position to open a toast from that edge.
<script setup lang="ts">
import { AnimatedToastStack, useAnimatedToastStack, type ToastInput, type ToastPosition } from 'beui-vue'
import { h, onBeforeUnmount, shallowRef } from 'vue'
const positions: ToastPosition[] = ['top-left', 'top-center', 'top-right', 'bottom-left', 'bottom-center', 'bottom-right']
const position = shallowRef<ToastPosition>('bottom-right')
const { toasts, showToast, updateToast, dismissToast, clearToasts } = useAnimatedToastStack({ defaultDuration: 3600, limit: 5 })
const timers = new Set<number>()
// The same overrides the upstream preview passes: a slightly translucent surface and a sparkles glyph for neutral toasts.
const classNames = { surface: 'toast-demo-surface' }
const icons = { neutral: h('svg', { viewBox: '0 0 24 24', width: 14, height: 14, fill: 'none', stroke: 'currentColor', 'stroke-width': 2, 'stroke-linecap': 'round', 'stroke-linejoin': 'round', 'aria-hidden': 'true' }, [h('path', { d: 'M9.937 15.5A2 2 0 0 0 8.5 14.063l-6.135-1.582a.5.5 0 0 1 0-.962L8.5 9.936A2 2 0 0 0 9.937 8.5l1.582-6.135a.5.5 0 0 1 .963 0L14.063 8.5A2 2 0 0 0 15.5 9.937l6.135 1.581a.5.5 0 0 1 0 .964L15.5 14.063a2 2 0 0 0-1.437 1.437l-1.582 6.135a.5.5 0 0 1-.963 0z' })]) }
const examples: (ToastInput & { label: string })[] = [
{ label: 'Title only', status: 'success', title: 'Saved', duration: 0 },
{ label: 'Promise', status: 'loading', title: 'Publishing component', description: 'Bundling source, preview, and registry metadata.', duration: 0 },
{ label: 'Success', status: 'success', title: 'Component published', description: 'Registry endpoint and raw source are available.' },
{ label: 'Error', status: 'error', title: 'Snapshot failed', description: 'Retry after the browser target settles.' },
]
function show(input: ToastInput) {
const id = showToast(input)
if (input.status !== 'loading') return
const timer = window.setTimeout(() => {
timers.delete(timer)
updateToast(id, { status: 'success', title: 'Publish complete', description: 'Toast updated in-place from loading to success.', duration: 3200 })
}, 1800)
timers.add(timer)
}
function move(next: ToastPosition) {
position.value = next
showToast({ status: 'info', title: 'Position changed', description: `New toasts open from ${next}.` })
}
onBeforeUnmount(() => { for (const timer of timers) window.clearTimeout(timer) })
</script>
<template>
<div class="toast-demo">
<AnimatedToastStack :toasts="toasts" :position="position" placement="fixed" :max-visible="4" :class-names="classNames" :icons="icons" @dismiss="dismissToast" />
<div class="copy"><strong>Open a real toast</strong><p>Toasts render fixed on the screen. Change position to open a toast from that edge.</p></div>
<div class="row">
<button v-for="example in examples" :key="example.label" type="button" class="action" @click="show(example)">
<svg viewBox="0 0 24 24" aria-hidden="true"><path :d="example.status === 'loading' ? 'M20 12a8 8 0 1 1-8-8' : example.status === 'error' ? 'm6 6 12 12M18 6 6 18' : 'm4 12 5 5L20 6'" /></svg>{{ example.label }}
</button>
<button type="button" class="clear" @click="clearToasts">Clear</button>
</div>
<div class="row edges" role="group" aria-label="Toast position">
<button v-for="edge in positions" :key="edge" type="button" :aria-pressed="position === edge" @click="move(edge)">{{ edge }}</button>
</div>
</div>
</template>
<style scoped>
.toast-demo { min-height: 18rem; width: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1.5rem; }
.copy { display: flex; flex-direction: column; align-items: center; gap: .5rem; text-align: center; } .copy strong { display: block; font-size: .875rem; line-height: 1.25rem; font-weight: 500; color: var(--beui-foreground); }
.copy p { max-width: 24rem; margin: 0; padding: 0; font-size: .75rem; line-height: 1.25rem; color: var(--beui-muted-foreground); }
.row { display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: .5rem; }
/* The upstream `press` utility: a 120ms scale on press. */
.row button { font: inherit; font-size: .75rem; line-height: 1rem; font-weight: 500; padding: 0 1rem; height: 2.25rem; border-radius: 999px; border: 0; cursor: pointer; transition: transform 120ms cubic-bezier(.16, 1, .3, 1), color .15s cubic-bezier(.4, 0, .2, 1), background-color .15s cubic-bezier(.4, 0, .2, 1), border-color .15s cubic-bezier(.4, 0, .2, 1); }
.row button:active { transform: scale(.97); }
.row .action { display: inline-flex; align-items: center; gap: .5rem; color: var(--beui-foreground); background: var(--beui-card); border: 1px solid var(--beui-border); }
.row .action:hover { border-color: var(--beui-border-strong); }
.action svg { width: 14px; height: 14px; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; fill: none; }
.clear { background: transparent; color: var(--beui-muted-foreground); }
.clear:hover { background: color-mix(in srgb, var(--beui-foreground) 6.5%, transparent); color: var(--beui-foreground); }
.edges { gap: .375rem; } .edges button { height: auto; padding: .25rem .625rem; font-size: 11px; line-height: inherit; color: var(--beui-muted-foreground); background: color-mix(in srgb, var(--beui-foreground) 4%, transparent); }
.edges button:hover { background: color-mix(in srgb, var(--beui-foreground) 8%, transparent); color: var(--beui-foreground); }
.edges button[aria-pressed=true] { background: var(--beui-foreground); color: var(--beui-background); }
.row button:focus-visible { outline: 2px solid var(--beui-ring); outline-offset: 2px; }
</style>
<style>
.toast-demo-surface { background: color-mix(in srgb, var(--beui-card) 95%, transparent); }
</style><script setup lang="ts">
import { AnimatePresence, LayoutGroup } from 'motion-v'
import { computed, onBeforeUnmount, onMounted, shallowRef, watch, type VNodeChild } from 'vue'
import ToastItem from './core/ToastItem.vue'
import { animatedToastStackMessages, type AnimatedToast, type ToastClassNames, type ToastPosition, type ToastStatus } from './core/toasts'
defineOptions({ inheritAttrs: false })
const props = withDefaults(defineProps<{
toasts: readonly AnimatedToast[]
onDismiss?: (id: string) => void
position?: ToastPosition
placement?: 'static' | 'fixed' | 'absolute'
fixed?: boolean
portal?: boolean
/** A selector or an element; defaults to `document.body`. */
portalRoot?: string | Element | null
maxVisible?: number
className?: string
classNames?: ToastClassNames
/** Replace the glyph shown for a status. */
icons?: Partial<Record<ToastStatus, VNodeChild>>
/** Override any built-in string. */
labels?: Partial<typeof animatedToastStackMessages>
}>(), {
position: 'bottom-right',
placement: undefined,
fixed: false,
portal: undefined,
portalRoot: undefined,
maxVisible: 4,
labels: undefined,
})
defineSlots<{
/** Replace a toast's whole inner content (the React `renderToast`). */
toast?(props: { toast: AnimatedToast }): unknown
/** Alias of `toast`, named after the React `renderToast` prop. */
renderToast?(props: { toast: AnimatedToast }): unknown
/** Replace the status glyph inside the built-in layout. */
icon?(props: { toast: AnimatedToast; status: ToastStatus }): unknown
}>()
const text = computed(() => ({ ...animatedToastStackMessages, ...props.labels }))
const location = computed(() => props.placement ?? (props.fixed ? 'fixed' : 'static'))
const usePortal = computed(() => props.portal ?? location.value === 'fixed')
const isBottom = computed(() => props.position.startsWith('bottom'))
const visible = computed(() => props.toasts.slice(-props.maxVisible))
/**
* The React original resolves its portal target in an effect, so a portalled
* stack renders nothing on the server and nothing until the DOM exists. Same
* rule here: `null` means "not resolved yet", which suppresses the output.
*/
const portalTarget = shallowRef<Element | null>(null)
function resolvePortalTarget() {
if (typeof document === 'undefined') return
if (!usePortal.value) {
portalTarget.value = null
return
}
const root = props.portalRoot
portalTarget.value = typeof root === 'string'
? document.querySelector(root) ?? document.body
: root ?? document.body
}
onMounted(resolvePortalTarget)
watch([() => props.portalRoot, usePortal], resolvePortalTarget)
onBeforeUnmount(() => { portalTarget.value = null })
</script>
<template>
<Teleport v-if="!usePortal || portalTarget" :to="portalTarget ?? 'body'" :disabled="!usePortal">
<ol
v-bind="$attrs"
class="be-toasts"
:class="[classNames?.root, className]"
:data-position="position"
:data-placement="location"
:data-edge="isBottom ? 'bottom' : 'top'"
:aria-label="text.list"
aria-live="polite"
aria-atomic="false">
<!-- When the stack is full, the oldest toast fades out where it is, then the rest
slide down into its slot. Upstream gets that slide because React re-renders every
item when the list changes, so each one measures its layout. In Vue the items that
stay do not re-render; the LayoutGroup has them measure together when one leaves.
The exiting toast stays in flow (no popLayout), so the slide waits for its fade. -->
<LayoutGroup>
<AnimatePresence :initial="false">
<ToastItem
v-for="(item, index) in visible"
:key="item.id"
:toast="item"
:index="index"
:dismiss="onDismiss"
:class-names="classNames"
:icons="icons"
:dismiss-label="text.dismiss">
<template v-if="$slots.toast || $slots.renderToast" #default="scope"><slot v-if="$slots.toast" name="toast" v-bind="scope" /><slot v-else name="renderToast" v-bind="scope" /></template>
<template v-if="$slots.icon" #icon="scope"><slot name="icon" v-bind="scope" /></template>
</ToastItem>
</AnimatePresence>
</LayoutGroup>
</ol>
</Teleport>
</template>
<style scoped>
.be-toasts {
box-sizing: border-box;
display: flex;
flex-direction: column;
gap: .5rem;
width: calc(100vw - 2rem);
max-width: 24rem;
padding: 0;
margin: 0;
list-style: none;
pointer-events: none;
}
.be-toasts[data-edge=bottom] { flex-direction: column-reverse; }
.be-toasts[data-placement=fixed] { position: fixed; z-index: 90; }
.be-toasts[data-placement=absolute] { position: absolute; z-index: 20; }
/* The offsets only apply once the stack leaves the flow; a static stack sits
wherever its parent put it. */
.be-toasts:not([data-placement=static])[data-position^=top] { top: max(1rem, env(safe-area-inset-top)); }
.be-toasts:not([data-placement=static])[data-position^=bottom] { bottom: max(1.5rem, env(safe-area-inset-bottom)); }
.be-toasts:not([data-placement=static])[data-position$=left] { left: max(1rem, env(safe-area-inset-left)); }
.be-toasts:not([data-placement=static])[data-position$=center] { left: 50%; transform: translateX(-50%); }
.be-toasts:not([data-placement=static])[data-position$=right] { right: max(1rem, env(safe-area-inset-right)); }
</style>Install
Add it with your package manager, or copy the source manually.
Run it in a project set up with shadcn-vue. The source is copied to components/beui and its dependencies are added to your package.json. Read the shadcn-vue guide
$ bunx --bun shadcn-vue@latest add @beui-vue/animated-toast-stack$ bun add beui-vueDownload the release archive to your project root, then install the local file.
$ bun add ./beui-vue-0.1.0.tgzImport the shared styles once in your application.
import AnimatedToastStack from '@/components/beui/AnimatedToastStack.vue'
import '@/components/beui/styles.css'API Reference
toastsreadonly AnimatedToast[]—onDismiss?(id: string) => void—position?ToastPositionbottom-rightplacement?"static" | "fixed" | "absolute"undefinedfixed?booleanfalseportal?booleanundefinedportalRoot?string | Element | nullA selector or an element; defaults to `document.body`.
undefinedmaxVisible?number4className?string—classNames?ToastClassNames—icons?Partial<Record<ToastStatus, VNodeChild>>Replace the glyph shown for a status.
—#renderToast?{ toast: AnimatedToast }Alias of `toast`, named after the React `renderToast` prop.
—labels?Partial<typeof animatedToastStackMessages>Override any built-in string.
undefined#toast?{ toast: AnimatedToast }Replace a toast's whole inner content (the React `renderToast`).
—#icon?{ toast: AnimatedToast; status: ToastStatus }Replace the status glyph inside the built-in layout.
—Related components
Animated Badge
Status badge with animated state icons, pulse feedback and compact size variants.
Loader
Loading indicator with seventeen variants: spinner, dots, bars, dot-matrix, dither, morph, comet, scramble, metaballs, newton, helix, percent, and five terminal-style ascii spinners. Scales from one size prop, uses currentColor, and reduced-motion swaps every transform for a calm opacity pulse.
Text Animation
Animated text primitives for spring reveals, chromatic sweeps, shimmer loading states, letter-cascade swaps and character scrambles.
Updated