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.
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
Name
Type
Default
Description
messageRequired
string
—
The text of the banner. It is plain text, because a banner made by the engine is plain data.
Buttons in the banner. Each has a label, an onClick and an optional appearance.
closable
boolean
true
Whether the person can close the banner.
durationMS
number
—
Removes the banner after this many milliseconds. Leave it out and it stays.
priority
number
0
A higher number comes first in the stack.
onDismiss
() => void
—
Called once, right before the banner goes, however it was removed.
Live example
ResultBanners added by this page: 0Last banner: noneBanners showing now: 0
codetypescript
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
codetypescript
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.