Toast

Toast delivers brief, non-blocking notifications that appear on screen to confirm actions, signal errors, or surface information. It supports five status variants, a built-in progress bar, position control, dismiss-previous behavior, and a static Toast.show() helper that requires no manual instantiation.

All active instances are tracked in a static Map, and each instance exposes .remove() and .dispose() for full lifecycle control.


Getting Started

Import the CSS, then call Toast.show() with a message and an optional status.

import { Toast } from 'frontalign';

Toast.show({
  message: 'Your changes have been saved.',
  status: 'success'
});

Structure

FrontAlign injects the toast element directly into document.body. No anchor element is required.

<div class="toast is-success is-bottom" role="status" aria-live="polite">
  <span class="toast-icon"></span>
  <span class="toast-message">Your changes have been saved.</span>
  <div class="toast-progress"></div>
</div>
ElementClass / AttributePurpose
Root.toastOuter wrapper injected into document.body. Receives status and position modifier classes.
Icon.toast-iconStatus icon element. Rendered only when showIcon is true.
Message.toast-messageNotification text content.
Progress bar.toast-progressBuilt-in countdown bar that animates linearly over the configured duration.

Status Variants

Toast ships with five status types. Each maps to a CSS modifier class on the root element.

Toast.show({ message: 'Record saved.',            status: 'success' });
Toast.show({ message: 'Connection failed.',        status: 'danger'  });
Toast.show({ message: 'Storage almost full.',      status: 'warning' });
Toast.show({ message: 'A new version available.',  status: 'info'    });
Toast.show({ message: 'General notification.',     status: 'default' });
StatusCSS ClassUse Case
defaultis-defaultGeneral neutral notifications.
successis-successConfirmed actions, completed tasks.
dangeris-dangerErrors, failures, destructive events.
warningis-warningPotential issues, caution alerts.
infois-infoInformational messages, tips.

Position

Toasts can be anchored to the top or bottom of the screen. The default is bottom.

Toast.show({ message: 'Appears at the top.',    position: 'top'    });
Toast.show({ message: 'Appears at the bottom.', position: 'bottom' });

Duration

Controls how long the toast stays on screen in milliseconds. Clamped between 1000 and 10000.

Toast.show({ message: 'Short toast.',   duration: 1000  });
Toast.show({ message: 'Default toast.', duration: 4000  });
Toast.show({ message: 'Long toast.',    duration: 10000 });

Dismiss Previous

Set dismissPrevious: true to remove all currently visible toasts before showing the new one.

Toast.show({
  message: 'Previous toasts in this position were removed.',
  status: 'info',
  position: 'bottom',
  dismissPrevious: true
});

Icon

Set showIcon: true to render a status icon inside the toast. By default no icon is shown.

Toast.show({ message: 'Toast with icon.',    status: 'success', showIcon: true  });
Toast.show({ message: 'Toast without icon.', status: 'default', showIcon: false });

Auto Clean

Set autoClean: true for fire-and-forget toasts. The instance calls .dispose() automatically after removal, freeing it from memory.

Toast.show({
  message: 'Toast instance auto-cleans after removal.',
  autoClean: true
});

Instance Methods

MethodDescription
.remove()Immediately removes the toast from the DOM and cancels the timer. Calls .dispose() automatically if autoClean is true.
.dispose()Clears the timer, nullifies internal references, and removes the instance from Toast.instances.
const toast = new Toast({ message: 'Item deleted.', status: 'danger' });

// Later
toast.remove();

// Full cleanup
toast.dispose();

Static Instance Management

All active toast instances are stored in a static Map and accessible via Toast.instances.

// Inspect all active toasts
Toast.instances.forEach((instance, id) => {
  console.log(id, instance);
});

Configuration Reference

OptionTypeDefaultDescription
messagestring"Hi, I am a toast message"Notification text displayed inside the toast.
statusstring"default"Visual variant: success, danger, warning, info, or default.
positionstring"bottom"Screen position: top or bottom.
durationnumber4000Time in milliseconds before auto-dismiss. Clamped between 1000 and 10000.
dismissPreviousbooleanfalseRemoves all visible toasts before showing the new one.
showIconbooleanfalseRenders a status icon inside the toast.
autoCleanbooleanfalseAutomatically calls .dispose() after the toast is removed.

Class Reference

ClassElementDescription
.toastRootOuter wrapper. Receives status and position modifiers.
.is-success / .is-danger / .is-warning / .is-info / .is-defaultRoot modifierStatus variant applied to the root element.
.is-top / .is-bottomRoot modifierPosition variant applied to the root element.
.toast-iconIconStatus icon. Present only when showIcon is true.
.toast-messageMessageNotification text content.
.toast-progressProgress barBuilt-in countdown bar animated over the configured duration.

FrontAlign