Feedback

Toast

A brief notification in a corner of the screen, announced politely to screen readers.

Class nameTypeDescription
toastplf:toastComponentThe container fixed to the corner
toast-closeplf:toast-closePartThe close button, which draws an x while empty
toast-close-iconplf:toast-close-iconPartYour own SVG, in place of the drawn x; an icon class there doesn't need it
toast-actionplf:toast-actionPartAn action button
toast-countdownplf:toast-countdownPartThe time-left bar the script adds
toast-countdown-trackplf:toast-countdown-trackPartThe countdown bar's track the script adds

Default

Each message is an Alert, so it takes Alert’s classes and colors.

Showing a message announces it to screen readers without moving focus.

Colors

Add data-plf-duration="0" to an error so it stays until it is closed.

Action

A message with an action stays until it is closed. Your click handler runs the action, then the message hides, unless the handler calls preventDefault().

After a form submits

Show a message from your own code with PLF.Toast.show(), such as after a request succeeds: it creates the message, and a toast container in the page’s first root the first time, and removes the message once it hides. PLF.Button.loading() keeps the button busy meanwhile.

While a modal dialog is open, messages outside it cannot be shown, so give the dialog its own toast container; PLF.Toast.show() does so by itself.

JavaScript

Toast needs JavaScript. The Full script (pelatform-ui.min.js) starts every component on page load, with no code of your own; with ESM imports, call init() yourself, as in Installation. For markup you add later, see Dynamic content.

<link rel="stylesheet" href="pelatform-ui.min.css">
<script src="pelatform-ui.min.js" defer></script>
<link rel="stylesheet" href="pelatform-ui.prefixed.min.css">
<script src="pelatform-ui.min.js" defer></script>
Attribute Purpose
data-plf-toast The message: an <article> directly in the toast container, a named <section>, with a unique id and hidden; its title names it. The script adds a polite live region to the container for its announcements
data-plf-toast-title The title; exactly one
data-plf-toast-description The description; at most one
data-plf-toast-action An action <button>; at most one. It keeps the message open until it is closed, and hides it after your click handler unless the handler calls preventDefault()
data-plf-duration Milliseconds before closing, 5000 by default; 0 keeps the toast open. A timed toast shows a countdown and pauses while hovered or focused
data-plf-trigger="toast:show", "toast:hide" On a <button> in the same root and dialog, shows or hides the toast whose id is in data-plf-target; each toast holds one toast:hide close button, which needs no target and is named Dismiss notification unless it has a name
Method Behavior
PLF.Toast.getOrCreateInstance(element, options) Returns the instance, creating it for valid markup; a numeric options.duration overrides the attribute
PLF.Toast.getInstance(element) Returns the instance or null
PLF.Toast.show(options) Creates and shows a message: its title, and optionally its description, color (success, error, warning or info), duration, action ({label, onClick}) and root; removes it once it hides. While a Modal is open, the message shows inside that dialog and hides when it closes. Returns its instance, or null when it cannot show
instance.show(trigger) Shows the toast; focus returns to the optional trigger if the toast closes while focused. Returns false when canceled, already visible or outside an open modal
instance.hide() Hides the toast; returns false when canceled or already hidden
instance.getState() Returns {visible, paused, duration}
instance.destroy() Hides the toast without events and removes listeners and timers
Event When
plf:toast:before-show Before showing; cancelable
plf:toast:show After showing
plf:toast:before-hide Before hiding; cancelable
plf:toast:hide After hiding

Each event’s detail:

Property Value
instance The Toast instance
reason api, trigger, escape, timeout or action
trigger The button that showed or hid it, the focused element for escape, or the trigger given to show(); otherwise null
visible true for before-show and show, false for before-hide and hide
Key Action
Tab, Shift+Tab Reach the action and close buttons in page order; focus is not trapped
Enter, Space Activates the focused button
Escape Closes the toast that contains focus, through plf:toast:before-hide

React

Toaster from @pelatform/react shows the messages toast() creates, in the markup PLF.Toast.show() gives them: render it once, as in your layout, and call toast() from any code, such as after a request.

Prop Values Default
aria-label The container’s name Notifications
closeLabel The name of each close button Dismiss notification

toast(options), or toast(title), takes PLF.Toast.show()’s options and returns a function that hides the message; toast.success(), toast.error(), toast.warning() and toast.info() set its color.

Option Values Default
title The title, announced with the description Required
description Text under the title None
color success, error, warning, info Neutral
duration Milliseconds before it hides, paused while hovered or focused; 0 keeps it open 5000, or 0 with an action
action {label, onClick}: onClick runs, then the message hides unless it calls preventDefault() None

A call before the Toaster mounts waits for it. While a Modal stays open, the message shows inside that dialog and hides when it closes. Other props are the container <section>’s own, className and ref included.

CSS variables

A message takes Alert’s --plf-alert-* variables and the shadow-lg utility.

Variable Default
--plf-toast-width 24rem, at most the screen width less the gutters
--plf-toast-gap Space 3 (12px), between and inside messages

Built with Pelatform UIv0.1.0

Components

Getting started

Foundations

Components

Actions

Data display

Navigation

Feedback

Data input

Layout

Advanced

Integrations

Blocks

Application

Authentication

Icons