skip section navigation

Toasts

usage

svelte
<script lang="ts">
	import { Card, createToast, type ToastEntry } from '@ciabi/loafkit-ui';
	async function saveNote(fail = false) {
		try {
			await createToast.promise(async () => {
				await new Promise<void>(resolve => setTimeout(resolve, 600));
				if (fail) throw new Error('could not save the note. try again.');
				return 'garden-notes';
			}, {
				loading: 'saving your note…', success: name => `${name} is saved.`,
				error: error => error instanceof Error ? error.message : 'could not save the note.'
			}, { id: 'docs-note-save', error: { duration: Infinity } });
		} catch {
			return;
		}
	}
</script>

{#snippet noteToast(entry: ToastEntry)}
	<Card variant="elevated">
		<p>your note is ready to share.</p>
		<button onclick={entry.dismiss}>keep editing</button>
	</Card>
{/snippet}
<button id="toast-methods-success--error--loading--custom" onclick={() => createToast.loading('syncing your note…', { id: 'docs-note-sync' })}>start sync</button>
<button onclick={() => createToast.success('your note is synced.', { id: 'docs-note-sync', duration: 2000 })}>finish sync</button>
<button onclick={() => createToast.error('could not sync the note. try again.', { id: 'docs-note-sync', duration: Infinity })}>sync failed</button>
<button id="toast-methods-promise" onclick={() => saveNote()}>save note</button>
<button onclick={() => saveNote(true)}>try failed save</button>
<button onclick={() => createToast.custom(noteToast, { announcement: 'your note is ready to share.' })}>show custom note</button>
<button id="toast-methods-dismiss" onclick={() => createToast.dismiss()}>dismiss notifications</button>
<button id="toast-methods-remove" onclick={() => createToast.remove()}>remove immediately</button>

call from a browser event or onMount. timers pause on hover, keyboard focus and window blur. motion respects reduced-motion preferences. keep field validation beside its field, and feedback for native modal dialogs inside the dialog.

update or close one notification

updates keep the same ID and retain earlier options.

svelte
<script lang="ts">
	import { createToast, type ToastHandle } from '@ciabi/loafkit-ui';
	let notice = $state<ToastHandle>();
</script>

<button onclick={() => notice = createToast.loading('saving your note…', { duration: Infinity })}>start save</button>
<button disabled={!notice} onclick={() => notice?.update('your note is saved.', {
	type: 'success', duration: 2000 // replace the explicit Infinity override
})}>finish save</button>
<button disabled={!notice} onclick={() => notice?.()}>dismiss with animation</button>
<button disabled={!notice} onclick={() => notice?.remove()}>remove immediately</button>
<!-- the ID also works with createToast.dismiss(id) and createToast.remove(id). -->
<p>save notification: {notice?.id ?? 'not started'}</p>

configure shared and independent toasts

createToast uses toaster; a controller you create has its own defaults and entries.

svelte
<script>
	import { onDestroy } from 'svelte';
	import { createToast, toaster, createToaster } from '@ciabi/loafkit-ui';
	const notes = createToaster({ position: 'top-left' });
	onDestroy(() => notes.destroy()); // dispose only the controller this component owns

	function configureShared() {
		toaster.configure({ position: 'top-center', spacing: 12,
			toastOptions: { success: { duration: 3000 } } });
		// moves inherited placements and changes gaps; existing countdowns keep running.
		// duration applies on the next show/update; supplied per-type objects replace earlier ones.
	}
</script>

<button onclick={configureShared}>configure shared toasts</button>
<button onclick={() => createToast.success('your note is saved.')}>shared confirmation</button>
<button onclick={() => notes.success('your note is saved.')}>independent confirmation</button>

or render a custom controller yourself

set autoMount: false and supply one Toasts renderer. omitting it requires your own headless UI and announcements.

svelte
<script>
	import { onDestroy } from 'svelte';
	import { Toasts, createToaster } from '@ciabi/loafkit-ui';
	const notes = createToaster({ autoMount: false, position: 'top-left' });
	const entries = notes.entries;
	const settings = notes.settings;
	onDestroy(() => notes.destroy());
	// Svelte cleans up the $store subscriptions below on unmount.
</script>

<button onclick={() => notes.show('your note is ready.')}>show note</button>
<button onclick={() => notes.pause('reading')}>pause timers</button>
<button onclick={() => notes.resume('reading')}>resume timers</button>
<!-- pause/resume reasons must match; exiting entries remain until removeDelay ends. -->
<p>{$entries.filter(entry => entry.visible).length} visible Ā· {$settings.position} stack</p>
<Toasts toaster={notes} />

API reference

createToast(message, options?)

Function
message

plain text or a Svelte snippet receiving the current entry.

options
Default: {}

overrides instance and per-type defaults.

returns

callable dismiss function with id, update(message, options?) and remove().

ToastContent

Type

a string or a Svelte snippet; used for toast messages and rich content.

string

plain text to display and announce.

snippet

Svelte content receiving the current entry, including its dismiss function.

ToastOptions

Type

per-toast overrides and controller defaults.

id
Default: generated

deduplicates within a controller. updates retain per-toast options and restart the timer.

type
Default: 'blank'

sets the default icon, duration and surface treatment. custom has no default surface.

duration
Default: by type

milliseconds before dismissal: blank/error/custom 4000, success 2000, loading Infinity. Infinity persists until dismissed.

removeDelay
Default: 240

milliseconds retained after dismissal for the exit animation. remove() bypasses this delay.

dismissible
Default: false for finite durations

shows a dismiss button. always true for persistent entries.

position
Default: 'bottom-right'

top-left, top-center, top-right, bottom-left, bottom-center or bottom-right.

icon
Default: by type

replaces the default success/error/loading icon. null hides it. blank and custom have no default icon.

iconTheme

foreground and background colors for the icon wrapper.

class

extra classes on the animated item. use global CSS for caller-defined classes.

style

CSS declarations on the animated item.

ariaProps

announcement role and priority. defaults to a polite status announcement.

announcement

announcement text for rich content. strings announce themselves; snippets otherwise use a content live region.

ToastOptions['ariaProps']

Type

announcement settings within ToastOptions.

role
Default: 'status'

role on the managed announcement region.

aria-live
Default: 'polite' (or 'assertive' for alert)

announcement priority. off suppresses automatic announcements.

ToastPromiseMessages

Type

messages passed to createToast.promise.

loading

content while the task runs.

success

result text or a callback returning text or a snippet.

error

failure text or a callback returning text or a snippet.

createToaster(options?)

Function
options
Default: {}

initial configuration for an independent controller.

returns

independent state, show helpers, configuration and lifecycle methods. dispose it with destroy() when its owner unmounts.

ToasterOptions

Type

configuration passed to createToaster or configure.

autoMount
Default: true

mounts Toasts on document.body on first show. set false to render one Toasts yourself or subscribe headlessly.

position
Default: 'bottom-right'

placement for new and displayed entries that inherit the instance default.

spacing
Default: 8

gap between entries in pixels.

reverseOrder
Default: false

false places newest entries first in each stack; true places oldest first.

toastOptions
Default: {}

shared and per-type defaults. precedence: built-in, shared, type, per-toast.

Toasts

Component
toaster

the independent controller to render. mount one renderer per controller to avoid duplicate announcements.

children

custom item rendering. retains layout, motion and persistent dismissal; supply announcement text for rich content.

class
Default: ''

extra classes on each placement container.

style

CSS declarations on each placement container. set offsets or position: absolute inside a positioned parent.