Banner engine

A banner is a message across the top of the app. addBanner() puts one in the stack of AppShell from anywhere in your code, and gives you a handle to change or remove it.

What it does
Puts a message across the top of the app from anywhere in your code, and gives you a handle to change or remove it.
Exported from the package?
Yes. Import addBanner, removeBanner, clearBanners and getBanners from @davidnet-net/svelte-ui. The banner stack of AppShell shows them.
Banner, Toast or SectionMessage?
Use a banner for something that is true for the whole app until it is solved: a service is down, the session is about to end, an update is ready. Use a Toast for something that just happened. Use a SectionMessage for a message that belongs to one part of a page. The Banner component is the same thing for a banner you place yourself.

Which one do I use?

What I needUse
Show a message about the whole appaddBanner
Change a banner that is showingBannerHandle.update
Remove one bannerBannerHandle.dismiss, removeBanner
Remove all bannersclearBanners
List the bannersgetBanners

addBanner

typescript
typescript
function addBanner(banner: BannerInput): BannerHandle

interface BannerHandle {
	readonly id: string;
	dismiss: () => void;
	update: (patch: Partial<BannerInput>) => void;
}

Adds a banner and returns a handle with id, dismiss() and update(patch). Banners with a higher priority come first. If you drop every reference to the handle, the banner is removed once it is garbage collected, but do not rely on that: call dismiss() when your component goes away.

When to use

Use it for a state of the app that people need to know about, and that you can take back when it is over.

When not to use, and what to use instead

For a message that confirms an action use a Toast. For a message next to the content it is about, use a SectionMessage. More than one or two banners at once is too many: combine them.

Options

NameTypeDefaultDescription
message Requiredstring—The text of the banner. It is plain text, because a banner made by the engine is plain data.
title string—A short title above the message.
appearance "danger" | "warning" | "primary" | "discover" | "success""primary"The look of the banner.
icon iconType—An icon to show.
iconVariant "outlined" | "filled"—The style of the icon.
actions BannerAction[]—Buttons in the banner. Each has a label, an onClick and an optional appearance.
closable booleantrueWhether the person can close the banner.
durationMS number—Removes the banner after this many milliseconds. Leave it out and it stays.
priority number0A higher number comes first in the stack.
onDismiss () => void—Called once, right before the banner goes, however it was removed.

Live example

Result Banners added by this page: 0 Last banner: none Banners showing now: 0
typescript
typescript
import { addBanner } from "@davidnet-net/svelte-ui";

const banner = addBanner({
	appearance: "warning",
	title: "Heads up",
	message: "Your session is about to expire.",
	actions: [{ label: "Extend session", onClick: extendSession }]
});

banner.update({ message: "Session extended.", appearance: "success" });
banner.dismiss();

removeBanner

typescript
typescript
function removeBanner(id: string): void
function clearBanners(): void
function getBanners(): BannerRecord[]

removeBanner(id) removes one banner by its id, clearBanners() removes all of them, and getBanners() returns the reactive list. Removing a banner that is already gone does nothing.

When to use

Use removeBanner when you only have the id, and clearBanners when you leave a part of the app. Otherwise keep the handle and call dismiss().

When not to use, and what to use instead

clearBanners also removes banners that other code added, like the status banner of AppShell. Do not call it to remove only your own.

Live example

Result Banners showing now: 0