Toasts
usage
<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.
<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.
<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.
<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?)
ToastContent
a string or a Svelte snippet; used for toast messages and rich content.
ToastOptions
per-toast overrides and controller defaults.
id- Default:
generateddeduplicates 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 typemilliseconds before dismissal: blank/error/custom 4000, success 2000, loading Infinity. Infinity persists until dismissed.
removeDelay- Default:
240milliseconds retained after dismissal for the exit animation. remove() bypasses this delay.
dismissible- Default:
false for finite durationsshows 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 typereplaces 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']
announcement settings within ToastOptions.
ToastPromiseMessages
messages passed to createToast.promise.
createToaster(options?)
ToasterOptions
configuration passed to createToaster or configure.
autoMount- Default:
truemounts 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:
8gap between entries in pixels.
reverseOrder- Default:
falsefalse 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
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.