Introducing a new way to review your PRs.

Read the announcement

petalframework / petal_components

Phoenix + Live View HEEX Components

View on GitHub

24 documents · Updated Sep 2, 2026

docs / notifications-and-alerts

Notifications and alerts

Petal Components provides feedback patterns that tell people about a condition, a completed action, or work in progress. Alerts stay in the page, toasts report short-lived events, loading indicators show indeterminate work, and live regions make changing information available to assistive technology. This page is part of Feedback and live updates.

Feedback message types

The feedback components separate persistent page context from transient events. This lets a product choose whether a message should remain with the related content or appear in a shared notification area.

ComponentPurposeTypical lifetimeAnnouncement model
AlertShow an important message in page contextUntil the user or page removes italert for danger and warning; status for other supported colors
ToastReport a transient event from the server or browserTimed, sticky, or dismissed by the useralert for danger; status for other kinds
SpinnerShow that work has started when its end time is not knownWhile the parent operation runsVisual progress indicator; add a suitable accessible name in the surrounding UI
Live regionAnnounce a change without moving focusWhile its changing text is relevantUsually polite; assertive announcements are reserved for urgent messages

References: lib/…/alert.ex• L7-47 lib/…/toast.ex• L47-76 lib/…/loading.ex• L4-16 lib/…/alert.ex• L7-19

Alerts

An alert is a page-level message that stays beside the content it explains. Its color expresses the message kind, while its semantic role controls how assistive technology announces it. Danger and warning use the assertive alert role; info, success, and gray use the less interruptive status role.

An alert can include a heading, message, icon, and action content. It renders only when it has a message, so an empty state does not create an empty announcement target. Dismissal is optional: a supplied LiveView command adds a close action and hides the alert, while button properties support an existing application event and its own behavior.

References: lib/…/alert.ex• L48-122 lib/…/alert.ex• L42-75 test/…/alert_test.exs• L231-269 test/…/alert_test.exs• L376-447

Toast groups

A toast is a short notification placed in a shared group. The group is the page-wide mount point for server events, browser events, and flash messages. Keep one group in the layout because these event paths are global; if more than one group is mounted, the first mounted group owns the events and later groups stay inert for them.

The group keeps new toasts at the front of a stack and limits the visible set while collapsed. Older toasts remain queued behind the visible set and surface as earlier toasts leave. The group can sit in any corner or center edge of the viewport, and its default duration applies to toasts that do not set their own duration.

The diagram shows how a notification reaches the owning group and then becomes a visible or queued toast.

flowchart LR
  A[LiveView event] --> D[Toast group owner]
  B[Browser event] --> D
  C[Flash message] --> D
  D --> E[Toast stack]
  E --> F[Visible toasts]
  E --> G[Queued toasts]
  F --> H[Dismiss or timeout]
  H --> G

References: lib/…/toast.ex• L12-43 lib/…/toast.ex• L97-150 assets/…/petal_components.js• L3377-3433 assets/…/petal_components.js• L3517-3530 test/…/toast_group.test.js• L21-70

Toast lifecycle

Toasts accept info, success, warning, danger, loading, and neutral kinds. The older error name maps to danger. A toast may carry a title, description, action, close control, and timeout progress. A loading toast is sticky by default so an async operation can later update the same id to a result or dismiss it when the operation stops.

The lifecycle has three related controls:

  1. A server or browser event creates a toast, or updates the existing toast with the same id.
  2. An action can send an event back to the LiveView before the toast closes.
  3. A close button, swipe, timeout, or server dismissal removes the toast and emits a dismissal event from the group.

This id-based update path connects a long-running operation to its final result without creating a second notification. The server can also dismiss one toast by id or dismiss all toasts together.

References: lib/…/toast.ex• L18-31 lib/…/toast.ex• L49-92 assets/…/petal_components.js• L3472-3497 assets/…/petal_components.js• L3609-3693 test/…/toast_test.exs• L50-75

Pause and dismissal behavior

The toast stack expands when a mouse enters it and pauses all active timers. A short leave grace period prevents the stack from opening and closing while the pointer crosses gaps between toasts. When the pointer leaves, timers resume with the time that remained before the pause.

Touch uses a different rule. A press-and-hold pauses the stack and release resumes it, while a horizontal move beyond the swipe threshold dismisses the active toast. Only one pointer gesture can control the stack at a time, and releasing outside the group still ends the gesture so timers do not remain paused.

Close buttons dismiss a toast directly. Server dismissal uses the toast id or the all-toasts command. Dismissal starts the exit state and then removes the item even when a transition does not run.

References: assets/…/petal_components.js• L3435-3470 assets/…/petal_components.js• L3674-3693 assets/…/petal_components.js• L3729-3849 test/…/toast_pause.test.js• L28-156

Loading indicators

The spinner is an indeterminate loading indicator for work with no known percentage or end time. It can be shown or hidden without removing the component and supports small, medium, and large sizes. When the work has a known amount, use a determinate progress component instead; progress status text can then announce meaningful stage changes through a polite live region.

References: lib/…/loading.ex• L4-31 lib/…/progress.ex• L17-26 lib/…/progress.ex• L60-64 lib/…/progress.ex• L69-127 test/…/loading_test.exs• L5-53

Announcement semantics

An accessibility live region is an element whose text changes are announced without moving keyboard focus. The components use polite announcements for routine updates and reserve role="alert" for urgent errors or warnings. Icons and decorative loading shapes do not carry the message by themselves; the text and role provide the meaning.

Toast and alert roles are assigned from their severity. Other components use focused live regions for narrower changes: progress status announces stage text, copy controls announce completion, carousel controls announce the current slide, and chat content exposes a polite log. These regions keep the visual interaction and the spoken update aligned without repeating decorative text.

The accessibility audit view exercises alerts and the spinner with the wider component set. Its audit test is an opt-in browser check, so the live-region contract is also expressed in the component markup and focused behavior tests.

References: lib/…/alert.ex• L56-65 assets/…/petal_components.js• L3634-3653 lib/…/progress.ex• L86-107 lib/…/input.ex• L184-209 lib/…/field.ex• L648-676 lib/…/carousel.ex• L252-259 lib/…/chat.ex• L86-97 lib/…/chat.ex• L695-703 lib/…/a11y_live.ex• L67-101 test/…/a11y_live_test.exs• L1-15

Notifications report state changes through text and semantics. Visual-only effects provide a separate, non-text signal and should not replace an alert, toast, loading indicator, or live-region message when the user needs to know what happened. See Visual feedback effects for that related pattern.

References: lib/…/alert.ex• L7-19 lib/…/toast.ex• L61-94

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the event names and payload shape aligned between send_toast/3, dismiss_toast/2, and the PetalToast hook. A mismatch can make server notifications appear but prevent updates or dismissal.
  • Preserve the single-owner rule when changing toast mount points or event delivery. A second active group can duplicate global notifications.
  • Keep severity-to-role mapping aligned for alerts and toasts. A change can turn a routine update into an interruptive announcement or hide an urgent error from assistive technology.
  • When changing toast timing or gesture rules, preserve the outside-release path and the remaining-time calculation. Otherwise a toast can stay paused forever or expire under the user's pointer.
  • When changing dynamic status text, keep the live region separate from decorative content. Repeated visual values can otherwise produce duplicate or noisy announcements.

Important failure modes

TriggerConsequenceGuard
More than one toast group is mountedA global toast can render more than onceMount one group in the root layout and remove extra groups
A loading toast has no stable idThe result creates a new toast instead of updating the active operationUse one id for the loading and result events
A touch tap is treated as mouse hoverThe stack can remain paused and later toasts can lose their timersUse pointer type for hover and press-and-hold for touch
A swipe ends outside the stack without a release handlerAll toast timers can remain pausedKeep the window-level release path active during the gesture
Routine content uses an assertive alert roleScreen readers interrupt the current readingUse status or a polite live region for non-urgent updates
A live region is changed with the same textAssistive technology may not announce a new eventChange the region text when the state or message changes

Key files

FileWhy to read it
lib/petal_components/alert.exDefines persistent message purpose, severity roles, content, and dismissal choices.
lib/petal_components/toast.exDefines the server-facing notification contract and the single toast-group mount point.
assets/…/petal_components.jsDefines toast ownership, stack behavior, timing, pause, gesture dismissal, and role assignment.
lib/petal_components/loading.exDefines the indeterminate loading signal.
lib/petal_components/progress.exShows the boundary between indeterminate loading and determinate progress with status announcements.
lib/petal_components_web/a11y_live.exProvides the broad accessibility audit surface for these feedback patterns.
test/…/toast_group.test.jsDocuments the global event ownership rule and promotion after teardown.
test/…/toast_pause.test.jsDocuments pause, touch, swipe, and outside-release behavior.