Vitral 0.2.18
Messages

Toast

Notifications sent from anywhere with useToast, one or several at once, collapsed, pinned, with actions or following a promise, that pause their timer while hovered or focused.

Import

main.ts
import { Toast } from '@vitral/vue';

Severities

Four seconds each; hover one to keep it.

<script setup lang="ts">import { Button, Toast, useToast } from '@vitral/vue'; const toast = useToast();const severities = [    { severity: 'success', summary: 'Saved', detail: 'Your changes are live.' },    { severity: 'info', summary: 'Update available', detail: 'Restart to install version 2.4.' },    { severity: 'warn', summary: 'Storage almost full', detail: '92% of 15 GB used.' },    { severity: 'danger', summary: 'Upload failed', detail: 'The connection was reset.' },    { severity: 'secondary', summary: 'Synced', detail: 'Everything is up to date.' },    { severity: 'contrast', summary: 'Copied', detail: 'The link is on your clipboard.' }] as const; // A group of its own, so only this example's <Toast> shows these messages// when another <Toast> is on the page.function show(index: number) {    toast.add({ ...severities[index]!, group: 'severities', life: 4000 });}</script> <template>    <Toast group="severities" />    <Button v-for="(s, i) in severities" :key="s.severity" :label="s.severity" severity="secondary" @click="show(i)" /></template>

Sticky and grouped

Without a `life` a toast waits to be closed. A `group` sends it to another `<Toast>`, here one at the bottom.

<script setup lang="ts">import { Button, Toast, useToast } from '@vitral/vue'; const toast = useToast(); // Each <Toast> has a group, so it shows only the messages sent to it// when another <Toast> is on the page.function sticky() {    toast.add({ group: 'sticky', severity: 'info', summary: 'Stays until closed', detail: 'No life, so it waits for you.' });} function bottom() {    toast.add({ group: 'bc', severity: 'success', summary: 'Message sent', life: 3000 });}</script> <template>    <Toast group="sticky" />    <Toast position="bottom-center" group="bc" />    <Button label="Sticky" severity="secondary" @click="sticky" />    <Button label="Bottom centre" severity="secondary" @click="bottom" />    <Button label="Clear all" severity="secondary" variant="outlined" @click="toast.removeAll()" /></template>

Several at once

`add` takes a list as well as one message. `max` caps the cards on screen: with `overflow` at `'queue'`, the default, the rest wait and each one's life starts only when it shows; with `'replace'`, the oldest card that is not pinned closes to make room. `newest-on-top` turns the order round.

<script setup lang="ts">import { Button, Toast, useToast } from '@vitral/vue'; const toast = useToast();const batch = (group: string) =>    ['Invoice 1041', 'Invoice 1042', 'Invoice 1043', 'Invoice 1044', 'Invoice 1045'].map((name) => ({ group, severity: 'success' as const, summary: 'Sent', detail: name, life: 2500 }));</script> <template>    <Toast group="queue" position="top-left" :max="3" />    <Toast group="replace" position="bottom-left" :max="3" overflow="replace" />    <Button label="Five at once, queued" severity="secondary" @click="toast.add(batch('queue'))" />    <Button label="Five at once, replacing" severity="secondary" @click="toast.add(batch('replace'))" /></template>

Collapsed into one

Messages that share a `collapseKey` share a card: the newest text, its life started again, and a count of how many came. Press the button a few times.

<script setup lang="ts">import { Button, Toast, useToast } from '@vitral/vue'; const toast = useToast();let n = 0; function upload() {    n++;    toast.add({ group: 'collapse', collapseKey: 'upload', severity: 'success', summary: 'File uploaded', detail: `photo-${n}.jpg`, life: 4000 });}</script> <template>    <Toast group="collapse" position="top-center" />    <Button label="Upload a file" severity="secondary" @click="upload" /></template>

Pinned, with actions

A `pinned` message leads the stack, never times out and never waits behind `max`. `actions` puts buttons on the card; each closes it unless it says `keepOpen`, and the `action` event reports the press. `progress` draws the life left, holding while the card is read.

<script setup lang="ts">import { Button, Toast, useToast } from '@vitral/vue'; const toast = useToast(); function update() {    toast.add({        group: 'actions',        id: 'update',        pinned: true,        severity: 'info',        summary: 'A new version is ready',        detail: 'Reload to use it.',        actions: [            { label: 'Reload', severity: 'primary', onClick: () => toast.add({ group: 'actions', severity: 'success', summary: 'Reloaded', life: 2000 }) },            { label: 'Later' }        ]    });} function remove() {    toast.add({        group: 'actions',        severity: 'secondary',        summary: 'Message deleted',        life: 6000,        progress: true,        actions: [{ label: 'Undo', onClick: () => toast.add({ group: 'actions', severity: 'success', summary: 'Restored', life: 2000 }) }]    });}</script> <template>    <Toast group="actions" position="top-right" />    <Button label="Pinned, with actions" severity="secondary" @click="update" />    <Button label="Undo, with a progress bar" severity="secondary" @click="remove" /></template>

Following a promise

`promise` shows one card while the work runs, with a spinner and no close button, and turns it into the success or the error when it settles. `update` changes any card in place by its id.

<script setup lang="ts">import { Button, Toast, useToast } from '@vitral/vue'; const toast = useToast(); // Stands in for a request: half the time it fails.const upload = () => new Promise<number>((resolve, reject) => setTimeout(() => (Math.random() < 0.5 ? resolve(3) : reject(new Error('The server is busy.'))), 1500)); function send() {    toast        .promise(upload(), {            loading: { group: 'promise', summary: 'Uploading 3 files' },            success: (count) => ({ summary: `${count} files uploaded` }),            error: (error) => ({ summary: 'Upload failed', detail: (error as Error).message })        })        .catch(() => {});}</script> <template>    <Toast group="promise" position="bottom-center" />    <Button label="Upload" severity="secondary" @click="send" /></template>

A pile

`stacked` draws the cards as a pile, the newest in front; pointed at or focused, it spreads and every countdown waits. On a phone a card can be swiped sideways to close it, which `swipe-to-close` turns off, and the countdowns hold while the page is in the background, which `pause-on-page-hidden` turns off.

<script setup lang="ts">import { Button, Toast, useToast } from '@vitral/vue'; const toast = useToast();const severities = ['info', 'success', 'warn', 'danger'] as const;let n = 0; function send() {    n++;    toast.add({ group: 'pile', severity: severities[n % 4], summary: `Notification ${n}`, detail: 'Point at the pile to spread it.', life: 6000 });}</script> <template>    <Toast group="pile" position="bottom-right" stacked show-progress />    <Button label="Add to the pile" severity="secondary" @click="send" /></template>

API

Read from packages/vue/src/components/Toast/types.ts, so it says what the component actually accepts.

Props

NameTypeDescription
position ToastPositionDefaults to `'top-right'`.
group stringShows only the messages sent with the same `group`.
max numberAt most this many cards at once; pinned ones always show. What happens to the rest is `overflow`'s.
overflow 'queue' | 'replace'Past `max`: `'queue'` (the default) makes a new toast wait until a card leaves; `'replace'` closes the oldest card that is not pinned to make room.
newestOnTop booleanPut the latest at the head of the stack rather than at its end.
stacked booleanDraw the stack as a pile: the newest in front, a few behind it, the rest hidden, all spread out while it is pointed at or holds focus. The countdowns wait while it is spread.
showProgress booleanA bar on each timed card that shows the life left; a message's `progress` overrides it.
pauseOnPageHidden booleanHold every countdown while the page is in the background. On by default.
swipeToClose booleanLet a finger or a pen swipe a card sideways to close it. On by default.

Plus pt, dt and unstyled from BaseProps, see pass-through and unstyled mode.

Emits

EventPayloadDescription
close event: ToastEventClosed by its close button.
life-end event: ToastEventClosed because its `life` ran out.
action event: ToastEvent & { action: ToastAction }One of a message's `actions` was pressed.

Slots

NameSlot propsDescription
message (props: { message: ToastMessage })Replaces the icon and text of each message; the close button stays.
container (props: { message: ToastMessage; closeCallback: () => void })Replaces the whole card's inside, close button included.
closeicon ——