Sub topic 2
Toast variants and modes
This chapter assumes you can already dispatch a ShowToastEvent from a component. It covers the two properties that control how a toast looks and how long it stays: variant and mode.
Variants
The variant property sets the color and icon of the toast. It takes one of four string values.
| Variant | Use it for |
|---|---|
| `info` | Neutral information. This is the default. |
| `success` | An action finished as expected. |
| `warning` | The action worked, but the user should check something. |
| `error` | The action failed. |
An unrecognized value falls back to info, so a typo such as 'sucess' produces no error. It just shows the wrong styling.
Modes
The mode property controls how long the toast stays on screen and whether the user can close it.
| Mode | Behavior |
|---|---|
| `dismissible` | Shows a close button and disappears after about 3 seconds. This is the default. |
| `pester` | Disappears after about 3 seconds and has no close button. |
| `sticky` | Stays until the user clicks the close button. |
Pick the mode based on how much the user needs to read or act on the message. A routine success can use the default. A failure with a message the user must read should be sticky.
A helper that covers all cases
Repeating the event construction in every handler gets noisy. A small method keeps the variant and mode choices in one place.
import { LightningElement } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class ToastVariants extends LightningElement {
showToast(title, message, variant, mode) {
this.dispatchEvent(
new ShowToastEvent({ title, message, variant, mode })
);
}
handleSuccess() {
this.showToast('Success', 'The contact was saved.', 'success', 'dismissible');
}
handleWarning() {
this.showToast('Check the phone number', 'The number looks short.', 'warning', 'pester');
}
handleError() {
this.showToast('Save failed', 'Required fields are missing.', 'error', 'sticky');
}
handleInfo() {
this.showToast('Heads up', 'Changes sync every few minutes.', 'info');
}
}In handleInfo, mode is undefined, so the toast uses the default dismissible mode.
Common errors
- Using
errorwith a short mode. An error toast that vanishes after 3 seconds is easy to miss. Pairerrorwithsticky. - Overusing
sticky. Several sticky toasts pile up and cover the page. Reserve it for messages that need attention. - Expecting toasts to work everywhere. Behavior depends on the container. In Lightning Experience and the Salesforce mobile app,
modeis honored. In some other containers, such as Experience Cloud sites, the toast may render differently, so test where your component will run. - Putting long text in
message. Toasts are for one or two short sentences. Put longer explanations in the page or a modal.
Exercise
A save fails and the user must see the server's error message before doing anything else. Which variant and mode should the toast use, and why?
Show answer
Use variant error and mode sticky. The error variant signals failure with the right color and icon. The sticky mode keeps the toast on screen until the user closes it, so the message is not missed the way a 3-second toast could be.
Exercise
You dispatch a ShowToastEvent with title and message but no variant or mode. What does the user see?
Show answer
An info toast in dismissible mode. It has a close button and disappears after about 3 seconds.