Toast engine

A toast is a short message that shows up in a corner and goes away by itself. Call toast() from anywhere: an event handler, a function after a request. The Toaster in AppShell shows it.

What it does
Shows a short message in a corner that goes away by itself, from anywhere in your code.
Exported from the package?
Yes. Import toast, getToasts and the types ToastAction, ToastHandle and ToastCloseReason from @davidnet-net/svelte-ui. The Toaster in AppShell shows the toasts.
Toast, Banner or SectionMessage?
Use a toast for something that just happened and needs no answer. Use a Banner for something about the whole app that stays until it is solved. Use a SectionMessage for a message in the content that people must read, next to what it is about.

Which one do I use?

What I needUse
Confirm that something workedtoast
Know which toasts are showinggetToasts
Say something about the whole app that staysBanner engine
Offer a shortcut to take a change backUndo toast

toast

typescript
typescript
function toast(
	title: string,
	content?: string,
	icon?: iconType,
	durationMS?: number,
	appearance?: ToastAppearance,
	location?: ToastLocation
): ToastHandle
function toast(options: {
	title: string;
	content?: string;
	icon?: iconType;
	durationMS?: number;
	appearance?: ToastAppearance;
	location?: ToastLocation;
	actions?: ToastAction[];
	onClose?: (reason: ToastCloseReason) => void;
}): ToastHandle

Shows a toast and returns a handle with its id and a dismiss(). Give the arguments in this order, or give one object with the same names plus actions and onClose. A title longer than 29 characters works, but logs a warning, because it no longer fits well. While the pointer is over the toast or focus is inside it, the timer waits, and it continues with the time that was left when you leave.

When to use

Use it to confirm something that worked (saved, copied, sent), or to say that something small went wrong and can be tried again.

When not to use, and what to use instead

A toast goes away: do not use it for something people must read or act on, like an error that blocks them, and never make a button in a toast the only way to do something. The button is a shortcut. Use a SectionMessage next to the cause. For something about the whole app that stays, use a Banner. Without durationMS the toast stays until it is dismissed.

Options

NameTypeDefaultDescription
title Requiredstring—The headline of the toast.
content string—A line of text under the title.
icon iconType—An icon to show.
durationMS number—How long it stays, in milliseconds. Leave it out and it stays until dismissed.
appearance "danger" | "warning" | "primary" | "discover" | "success" | "subtle""primary"The look of the toast.
location "topLeft" | "topCenter" | "topRight" | "bottomLeft" | "bottomCenter" | "bottomRight""bottomLeft"The corner of the screen.
actions ToastAction[]—Buttons under the text. Each has a `label`, an `onClick`, an optional `appearance` and `icon`, and `closeOnClick` (default true). Only in the object form.
onClose (reason: "timeout" | "dismissed" | "action") => void—Called exactly once when the toast goes away, with the reason: `timeout`, `dismissed` or `action`. Only in the object form.

Live example

Result Toasts shown by this page: 0 Last toast: none Toasts showing now: 0
typescript
typescript
import { toast } from "@davidnet-net/svelte-ui";

toast("Saved", "Your changes are in.");
toast("Saved", "Your changes are in.", "task_alt", 3000, "success");
toast("Could not save", "Check your connection and try again.", undefined, 6000, "danger", "topRight");

// The same, as an object. This is how you give actions and onClose
const { id, dismiss } = toast({
	title: "Saved",
	content: "Your changes are in.",
	icon: "task_alt",
	durationMS: 3000,
	appearance: "success"
});

getToasts

typescript
typescript
function getToasts(): ToastItem[]

getToasts() returns the reactive list of the toasts that are showing, each with a dismiss(). The Toaster component reads it.

When to use

Use it only if you build your own Toaster, or want to dismiss a toast from code.

When not to use, and what to use instead

Do not use it to show a toast: call toast. Do not change the list yourself.

Live example

Result Toasts showing now: 0

Undo toast

typescript
typescript
interface ToastAction {
	label: string;
	onClick: () => void;
	appearance?: ButtonAppearance;
	icon?: iconType;
	closeOnClick?: boolean; // default true
}
type ToastCloseReason = "timeout" | "dismissed" | "action";
interface ToastHandle {
	readonly id: string;
	dismiss: () => void;
}

A toast with an Undo action and an onClose that makes the change final. onClose runs once, whichever way the toast ends: timeout when the time is up, dismissed when it was closed, action when a button closed it. So undo in the action, and commit in onClose when the reason is not action. A page that closes does not call onClose: if the change must be kept then, also finish it on pagehide.

When to use

Use it for a change that can be taken back for a short time, like a delete that is only done on screen until the time is up. The Undo button is a shortcut: the same thing must also be possible somewhere else, like a trash or a history, because the toast goes away.

When not to use, and what to use instead

Do not use it when people must decide before the change happens: ask first in a Modal. For something that stays and needs an answer, use a Banner or a SectionMessage.

Live example

Point at the toast, or press Tab until focus is in it: the time stops. Leave and it continues with the time that was left.

Result Cards: 3 Waiting for a toast: 0 Deleted for good: 0 Undone: 0 Last closed by: none
typescript
typescript
import { toast } from "@davidnet-net/svelte-ui";

function deleteCard(card: Card) {
	hide(card); // only on screen for now

	toast({
		title: "Card deleted",
		content: "Undo to bring it back.",
		icon: "delete",
		durationMS: 8000,
		actions: [{ label: "Undo", icon: "undo", onClick: () => show(card) }],
		onClose: (reason) => {
			// "action" is the Undo button: the card is back, so there is nothing to finish
			if (reason !== "action") void removeForGood(card);
		}
	});
}

// A closing page does not call onClose: finish what is waiting on pagehide as well
addEventListener("pagehide", () => void removeAllWaiting());