Toast
Brief notifications in a corner. They pile up neatly, expand on hover, pause while you read them and can be swiped away.
Moey.toast('Event has been created');
Moey.toast.success('Your changes are live.', { title: 'Saved' });
Moey.toast.error({ title: 'Payment failed', message: 'Card ending 4242 was declined.' });
Moey.toast({
message: 'Order #10421 deleted',
action: { label: 'Undo', onClick: () => restore(10421) },
});
Moey.toast.promise(saveOrder(), {
loading: 'Saving…',
success: (order) => `Order #${order.id} saved`,
error: 'Could not save the order',
});
No JavaScript of your own needed for simple cases:
<button class="btn" data-moey-toast="Saved" data-moey-toast-type="success">Save</button>
<!-- any option as data-moey-toast-*: title, type, duration, position, variant, progress -->
<!-- rich content: data-moey-toast="#my-template" (a <template>) -->
Position & style
Set where toasts appear once for the whole page — no JavaScript — on <body> (or <html>). A single button or call can still choose its own:
<body data-moey-toast-position="top-center"> <!-- every toast on this page -->
<button data-moey-toast="Copied" data-moey-toast-position="bottom-center"> <!-- just this one -->
Moey.toast('Copied', { position: 'bottom-center' }); // same, from code
Positions: top-start · top-center · top-end · bottom-start · bottom-center · bottom-end (default). start/end follow the page's direction (right-to-left languages flip them); top-left, top-right, bottom-left and bottom-right always mean the physical side. Other page-wide defaults work the same way: data-moey-toast-duration, -variant, -progress, -close-button, -expand, -max, -visible. On phones every position spans the width of the screen.
Options
| Option | What it does |
|---|---|
message · title | Text (or a DOM node). Set html: true for trusted HTML. |
type | success · error · warning · info · loading — color, icon and role |
icon | A Moey icon name ('rocket'), a node, or false |
action · cancel | { label, onClick(event, toast) } — closes the toast unless you preventDefault() |
duration | ms (default 5000; 0 stays until closed; loading toasts stay until updated) |
position | top-start · top-center · top-end · bottom-start · bottom-center · bottom-end (or -left / -right) |
variant · theme | A strength: subtle · soft · solid · vivid · deep (none = a neutral card) · any color name (green, red…) |
progress · closeButton · dismissible | Countdown bar · ✕ button · can be closed by the user at all |
id | Showing a toast with the same id updates it instead of adding another |
onDismiss · onAutoClose | Callbacks |
const t = Moey.toast.loading('Uploading 0%');
t.update({ message: 'Uploading 60%' });
t.update({ type: 'success', message: 'Uploaded' }); // gets the normal duration again
t.dismiss(); // or Moey.toast.dismiss() for all
Moey.toast.config({ position: 'top-center', duration: 4000, max: 5, visible: 3, expand: false, progress: false });
- Pile — up to 3 show stacked; hover or focus expands them.
expand: truekeeps them expanded. - Timers pause while the toasts are hovered or focused and while the tab is in the background.
- Swipe (or drag) a toast sideways to dismiss it.
- Over dialogs — toasts stay on top of an open dialog and remain clickable.
- Accessible — announced by screen readers (errors immediately); Alt+T jumps to the newest toast.
Events: moey:toast when one appears · moey:closed when one is gone.
Colors
Toasts take their color from the type (success is green, error red, warning amber, info cyan) — or any color with theme, in any strength.
Moey.toast('Welcome back!', { theme: 'pink', variant: 'subtle' });
Moey.toast('Order placed', { theme: 'indigo', variant: 'vivid' });
Undo
Do the action right away and offer an undo — friendlier than asking "Are you sure?" first.
Moey.toast({
message: 'Order #10421 archived',
action: { label: 'Undo', onClick: () => restoreOrder(10421) },
});
Progress that updates
Show one toast and keep updating it by its id instead of adding new ones.
const t = Moey.toast.loading('Uploading 0%');
t.update({ message: 'Uploading 60%' });
t.update({ type: 'success', message: '3 files uploaded' }); // gets the normal duration