SearchCtrlK

  • ↑↓navigate
  • ↵open
  • escclose

Can’t find it? Contact us

Toast

A transient notification panel reporting the outcome of an action, in one of five semantic tones.

View code
tsx
<div mix={[hstack({ gap: 2, align: "center" })]}>
	<Button
		mix={[
			on("click", () =>
				pageToaster().add({ color: "success", title: "Status page published" }),
			),
		]}
	>
		Publish
	</Button>
	<Button
		color="danger"
		variant="outline"
		mix={[
			on("click", () =>
				pageToaster().add({
					color: "danger",
					title: "Couldn't upload checks.yml",
					description: "Unknown key “retires”. Fix it and try again.",
				}),
			),
		]}
	>
		Upload a bad file
	</Button>
</div>

{/* Once, in the app layout: */}
<Toasts labels={{ region: "Notifications", dismiss: "Dismiss" }} />

Demo apps

Whole pages built only from @sdxc/ui, each for an invented product with its own palette and type. They open in a new tab.

Every demo, landings and docs sites alike, is listed in the showcase.

Installation

An ordinary dependency: install it, import the theme once, and import the component where it is used.

Terminal
npm add @sdxc/ui remix
typescript
import "@sdxc/ui/theme.css";

Usage

typescript
import { Toast } from "@sdxc/ui/app";

Renders a single toast panel, tinted, bordered, and shadowed by Toast.Props.color, with role and aria-atomic defaulted (overridable); pair a mix-driven enterExit(), since it holds still.

Composition

The parts the component publishes, each a static property of the host.

  • Toast
  • Toast.IconRenders Toast.IconProps.children as the toast's leading icon, hidden from assistive technology since the toast's color and text already carry its meaning; swap in Toast.Loader while pending.
  • Toast.LoaderRenders Toast.LoaderProps.children as the toast's loading graphic, laid out identically to Toast.Icon so swapping between them keeps the layout stable; pair a spin() mix factory for its rotating loop.
  • Toast.ContentRenders Toast.ContentProps.children as the toast's body block: a column flex container that shrinks to fit alongside a leading icon or trailing action, holding Toast.Title and Toast.Description.
  • Toast.TitleRenders Toast.TitleProps.children as the toast's heading, in a native <h3> sized and weighted as a small, single-line label above the toast's description.
  • Toast.DescriptionRenders Toast.DescriptionProps.children as the toast's supporting message, in a native <p> set slightly translucent against the toast's foreground color so it reads as secondary to Toast.Title.
  • Toast.ActionRenders Toast.ActionProps.children as the toast's primary trailing control; type defaults to "button", ordered before the consumer's attributes so command/commandfor still run; use type="submit" for a form.
  • Toast.CancelRenders Toast.CancelProps.children as a neutral-tinted, secondary trailing control; type defaults to "button", ordered before the consumer's attributes so command/commandfor still run.
  • Toast.CloseRenders a dismiss control for the ancestor Toast: an icon-only <button> removable via a consumer's mixin or the commandfor/command pair; type="button" precedes those attributes so the pairing still runs.
  • Toast.RegionRenders the fixed viewport stacking every queued Toast at a corner via Toast.RegionProps.placement; pointer-events stays none so clicks pass through to content behind, restored per toast.

Examples

Stack

View code
tsx
<Button
	mix={[
		on("click", () => {
			for (let name of monitors) pageToaster().add({ title: `${name} paused` });
		}),
	]}
>
	Pause five monitors
</Button>

<Toasts labels={{ region: "Notifications", dismiss: "Dismiss" }} max={3} />

Undo after a delete

  • Checkout API
  • Search
  • Webhooks
View code
tsx
function remove(name: string) {
	let index = monitors.indexOf(name);
	monitors = monitors.filter((monitor) => monitor !== name);
	void handle.update();

	pageToaster().add(
		{
			title: "Monitor deleted",
			description: name,
			action: {
				label: "Undo",
				onAction: () => {
					monitors.splice(index, 0, name);
					void handle.update();
				},
			},
		},
		{ duration: TOAST_UNDO_DURATION },
	);
}

Promise

View code
tsx
pageToaster().promise(saveMonitor(), {
	loading: { title: "Saving monitor", loading: true },
	success: { title: "Monitor saved", color: "success" },
	error: { title: "Couldn't save monitor. Try again.", color: "danger" },
});

Flash from the server

Monitor deleted

Checkout API

View code
tsx
// In the layout's controller:
let flash = session.get("toast"); // set by the redirecting action
// { id: "6f1c…", color: "success", title: "Monitor deleted",
//   action: { label: "Undo", form: { action: "/monitors/restore", fields: { id } } } }

<Toasts
	labels={{ region: "Notifications", dismiss: "Dismiss" }}
	initial={flash ? [flash] : []}
/>

Action and cancel

Upload of checks.yml failed

View code
tsx
let open = true;

function close() {
	open = false;
	void handle.update();
}

open ? (
	<Toast
		id="upload-failed"
		color="danger"
		live="assertive"
		mix={[
			dismiss({ duration: null }),
			on<HTMLDivElement, "ui:dismiss">("ui:dismiss", close),
		]}
	>
		<Toast.Content>
			<Toast.Title>Upload of checks.yml failed</Toast.Title>
		</Toast.Content>
		<Toast.Action mix={[on<HTMLButtonElement, "click">("click", close)]}>Retry</Toast.Action>
		<Toast.Cancel commandfor="upload-failed" command="--ui-dismiss">
			Dismiss
		</Toast.Cancel>
	</Toast>
) : (
	<Button
		variant="outline"
		mix={[
			on<HTMLButtonElement, "click">("click", () => {
				open = true;
				void handle.update();
			}),
		]}
	>
		Show the failure again
	</Button>
)

Props

Read from the component's own types, so every prop, every default and every allowed value is listed.

PropTypeDefaultRequiredDescription
color"brand" | "neutral" | "success" | "warning" | "danger"—NoSemantic tone of the toast. Default: "neutral".
live"polite" | "assertive" | "off"—Noaria-live politeness for the toast's content. Default: "polite".
childrenRemixNode—YesThe toast's compound parts: Toast.Icon, Toast.Content, Toast.Action, or any other content.

Also accepts everything in TagProps<"div">.

Toast.Icon

PropTypeDefaultRequiredDescription
childrenRemixNode—YesThe icon graphic, typically a single SVG icon.

Also accepts everything in TagProps<"div">.

Toast.Loader

PropTypeDefaultRequiredDescription
childrenRemixNode—YesThe loading graphic shown in place of Toast.Icon while the toast represents pending work.

Also accepts everything in TagProps<"div">.

Toast.Content

PropTypeDefaultRequiredDescription
childrenRemixNode—YesThe toast's title, description, or other body content.

Also accepts everything in TagProps<"div">.

Toast.Title

PropTypeDefaultRequiredDescription
childrenRemixNode—YesThe toast's heading text.

Also accepts everything in TagProps<"h3">.

Toast.Description

PropTypeDefaultRequiredDescription
childrenRemixNode—YesThe toast's supporting message text.

Also accepts everything in TagProps<"p">.

Toast.Action

PropTypeDefaultRequiredDescription
childrenRemixNode—YesThe action's label.

Also accepts everything in TagProps<"button">.

Toast.Cancel

PropTypeDefaultRequiredDescription
childrenRemixNode—YesThe cancel control's label.

Also accepts everything in TagProps<"button">.

Toast.Close

PropTypeDefaultRequiredDescription
"aria-label"string—YesAccessible label for the icon-only control — required, since the button carries no visible text for assistive technology to read.

Also accepts everything in TagProps<"button">.

Toast.Region

PropTypeDefaultRequiredDescription
placement"top-start" | "top-center" | "top-end" | "bottom-start" | "bottom-center" | "bottom-end"—NoCorner of the viewport the stack renders in. Default: "bottom-end".
maxnumber—NoMost toasts shown at once; older ones wait out of sight until a newer one leaves. Default: 3.
stack"peek" | "list"—No"peek" folds older toasts behind the newest, scaled to 0.95 and 0.9, and fans them out while the pointer or focus is on the stack; "list" always shows them one under another. Default: "peek".
childrenRemixNode—NoEvery currently queued Toast.

Also accepts everything in TagProps<"div">.

Types

NameValuesWhat it decides
Color"brand" | "neutral" | "success" | "warning" | "danger"Semantic tone driving the host element's border, tint, and foreground color through the --ui-* variables for that color.
Live"polite" | "assertive" | "off"aria-live politeness applied to the host element.
Placement"top-start" | "top-center" | "top-end" | "bottom-start" | "bottom-center" | "bottom-end"Corner of the viewport Toast.Region renders in.
Stack"peek" | "list"How Toast.Region lays out more than one toast.

Other components this module publishes.

  • pageToaster
@sdxc/ui

Can’t find it? Contact us