Home / lwc / lwc-show-toast-patterns
lwc-show-toast-patterns
ShowToastEvent in LWC: variants (success/warning/error/info), modes (dismissible/pester/sticky), message formatting, NOT supported in LWR Experience Cloud sites, alternatives (lightning-alert, inline banners). NOT for custom notification types (use admin/custom-notification-types). NOT for SLDS popovers.
Open the skill folder on GitHub
LWC ShowToast Patterns
Activate when surfacing user feedback in LWC. ShowToastEvent (from lightning/platformShowToastEvent) is the idiomatic toast API in Lightning Experience and Experience Cloud Aura sites — but NOT supported in LWR (Lightning Web Runtime) sites. Pick the right tool per context.
Before Starting
- Determine runtime. Lightning Experience / mobile Salesforce app / Aura Experience Cloud → ShowToastEvent works. LWR → NOT supported, use alternatives.
- Pick the variant.
success/warning/error/info— each has distinct styling. - Pick the mode.
dismissible(default, 3s auto),pester(stays until dismissed, errors only),sticky(stays until dismissed).
Core Concepts
Basic dispatch
import { LightningElement } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class MyCmp extends LightningElement {
notify() {
this.dispatchEvent(new ShowToastEvent({
title: 'Saved',
message: 'Record updated successfully',
variant: 'success',
mode: 'dismissible'
}));
}
}
Variants
| Variant | When to use |
|---|---|
success | Confirmation of successful action |
warning | Non-blocking caution |
error | Failure requiring attention |
info | Neutral status |
Modes
| Mode | Behavior |
|---|---|
dismissible (default) | Auto-dismisses after 5 seconds; user can close |
pester | Stays until user dismisses (only valid for error variant) |
sticky | Stays until user dismisses |
Message placeholders and links
this.dispatchEvent(new ShowToastEvent({
title: 'Record Saved',
message: 'See {0}',
messageData: [
{ url: '/lightning/r/Account/0012x.../view', label: 'Acme' }
],
variant: 'success'
}));
{0} is replaced by the object at index 0 — either plain text or {url, label} for a link.
LWR Experience Cloud caveat
ShowToastEvent is silently ignored on LWR sites. Alternatives:
lightning-alert/lightning-confirmmodals- Inline banner component using
lightning-icon+ SLDS notification classes - Custom toast implementation with
lightning-layout+ CSS animation
Common Patterns
Pattern: Error toast with sticky mode
this.dispatchEvent(new ShowToastEvent({
title: 'Save failed',
message: error.body.message,
variant: 'error',
mode: 'sticky'
}));
Pattern: Link-in-message toast
this.dispatchEvent(new ShowToastEvent({
title: 'Record Created',
message: 'View {0}',
messageData: [
{ url: `/lightning/r/Account/${recordId}/view`, label: accountName }
],
variant: 'success'
}));
Pattern: LWR-compatible fallback
// lwrToast.js — a custom banner component
@api message;
@api variant;
isShown = false;
show() { this.isShown = true; setTimeout(() => this.isShown = false, 3000); }
Decision Guidance
| Context | Feedback mechanism |
|---|---|
| Lightning Experience | ShowToastEvent |
| Mobile Salesforce app | ShowToastEvent |
| Aura Experience Cloud site | ShowToastEvent |
| LWR Experience Cloud site | Custom banner, lightning-alert, or inline component |
| Blocking confirmation needed | lightning-confirm (not a toast) |
| Rich content / interactive | LightningModal (not a toast) |
Recommended Workflow
- Confirm target runtime (Lightning vs LWR) before writing.
- Dispatch
ShowToastEventwith explicitvariantandmode. - For errors, use
stickyorpesterso users can read. - For cross-runtime components, check
@salesforce/client/formFactorand fall back to a banner in LWR. - Test toasts in the actual runtime, not just LEX (LWR renders differently).
- For accessibility, keep toast messages ≤75 characters so screen readers announce cleanly.
- Document that LWR does not render toasts — downstream teams must be warned.
Review Checklist
- [ ] Target runtime verified (LWR alternatives used where needed)
- [ ] Variant matches semantic intent
- [ ] Error variants use
stickyorpester(notdismissible) - [ ] Message kept short for accessibility
- [ ] Link-in-message uses
messageDataplaceholders - [ ] Fallback implemented for LWR sites
Salesforce-Specific Gotchas
- LWR Experience Cloud silently swallows
ShowToastEvent— no console warning, just no toast. Uselightning-alertor a custom banner. pestermode is only valid forvariant='error'. With other variants it falls back todismissible.- Toasts dispatched before the component is fully rendered may be lost. Use
renderedCallbackor defer to after async work completes. - Custom toast styling is not supported.
slds-notify_toastclasses apply to the built-in component, not reproducible outside the framework.
Output Artifacts
| Artifact | Description |
|---|---|
| Toast dispatch helper | Utility wrapping ShowToastEvent with defaults |
| LWR fallback component | Banner for Experience Cloud LWR |
| Accessibility snippet | Short message + role='alert' pattern |
Related Skills
lwc/lwc-lightning-modal— when blocking user attention is requiredlwc/lwc-accessibility-patterns— semantic feedback patternsadmin/custom-notification-types— for async/server-side notifications