Feedback and live updates
Petal Components gives a Phoenix LiveView application a shared way to show work in progress, completion, warnings, errors, and short-lived results. Server-rendered semantics describe the meaning of a message first. Browser hooks then add delivery, timing, motion, and client-side reactions without changing the main status of the page.
Feedback model
Feedback is split by how long it matters and how much attention it needs. Persistent messages stay with the related content. Progress shows known movement toward a goal. Toasts report events without taking space in the page flow. Visual effects mark a result but do not replace a useful text message.
| Feedback need | Primary surface | User outcome |
|---|---|---|
| A message tied to page content | Alert | The user can read the message in context and may take an action or dismiss it. |
| Known work toward a limit | Progress bar or ring | The user can judge completion from a value and an optional stage message. |
| An event that should not move the page | Toast | The user receives a transient notice with optional action or dismissal. |
| Unknown work time | Loading spinner | The user sees that work is active without a false percentage. |
| A successful or celebratory result | Confetti or another visual effect | The result gets emphasis while text and status remain the reliable channel. |
The library keeps these surfaces compatible with LiveView updates. A server can render a new state in the normal page patch, or it can send a named event when the browser must react immediately.
Status semantics
Status semantics tell assistive technology whether a change is routine or urgent. The library uses status for normal updates and alert for messages that need assertive attention.
Contextual alerts
The alert component is a persistent message surface for content that belongs near the related task or page state. It renders the message as a status for informational, successful, and neutral colors. Warning and danger colors use the more urgent alert role. A heading and body form the accessible message, while actions and dismissal remain optional controls. lib/petal_components/alert.ex#L48-L121
An alert can be dismissed with a LiveView JavaScript command. The built-in behavior hides the message with a short transition and runs the supplied command. This lets an application remove the visible message and still tell its own state or event system that dismissal occurred. lib/petal_components/alert.ex#L37-L40
References: lib/…/alert.ex• L48-121 lib/…/alert.ex• L7-19
Completion indicators
Progress represents determinate work: the application knows the current value and its maximum. The bar and ring share this meaning, so a page can change shape without changing how a user reads completion. When no value exists, the component leaves the numeric state indeterminate instead of announcing a made-up percentage. lib/petal_components/progress.ex#L17-L22 lib/petal_components/progress.ex#L202-L216
The optional status text carries stage changes such as “Downloading assets”. It uses a polite live region, which allows a screen reader to hear meaningful stage updates without hearing every numeric change. A spinner is the separate choice for activity with no known end point. lib/petal_components/progress.ex#L60-L64 lib/petal_components/loading.ex#L4-L31
References: lib/…/progress.ex• L17-26 lib/…/progress.ex• L60-107 lib/…/progress.ex• L320-327 lib/…/loading.ex• L4-31
Server event contracts
LiveView event contracts connect server state to browser-only feedback. The server sends a named event with a small payload. The mounted component accepts the event, applies it to its own DOM, and keeps the rest of the page under normal LiveView control.
Toast events
The toast API sends petal:toast for a new or changed notice and petal:toast-dismiss for removal. An id makes a notice addressable, so a loading message can become a success message in place. An action carries an event name and value back to the LiveView. :error is normalized to danger, which keeps server errors aligned with the alert severity model. lib/petal_components/toast.ex#L47-L95
The root layout normally renders one toast group. The group bridges put_flash messages into the same delivery path, so controller and redirect flows can use transient notifications without a second notification system. The browser warns about extra groups and ignores their duplicate global events. lib/petal_components/toast.ex#L33-L44 lib/petal_components/toast.ex#L97-L149 assets/…/petal_components.js• L3395-3407
Celebration events
Confetti uses the same server-to-browser event pattern for a visual result. The server sends pc-confetti, while a client action can dispatch pc:confetti without a round trip. Both paths target a mount when needed and accept the same burst settings. lib/petal_components/confetti.ex#L9-L27
This diagram shows the main LiveView feedback path from state change to user-visible result.
flowchart LR
A[Server state change] --> B{Feedback need}
B -->|Context message| C[Rendered alert]
B -->|Known completion| D[Rendered progress]
B -->|Transient event| E[LiveView push_event]
E --> F[Browser hook]
F --> G[Toast or visual effect]
C --> H[Accessible status or alert]
D --> I[Progress semantics and polite stage status]
G --> J[Dismissal or follow-up event]
References: lib/…/toast.ex• L49-92 lib/…/confetti.ex• L9-27 assets/…/petal_components.js• L3417-3433 assets/…/petal_components.js• L3610-3671
Browser feedback behavior
The browser owns behavior that depends on time, pointer input, or animation frames. The server still owns the message and its meaning. This split lets a LiveView update a loading toast, while the browser controls its timer, layout, and exit motion.
Transient notification lifecycle
The toast group inserts new notices at the front of one global stack. It collapses the stack by default and expands it on mouse hover. Auto-dismiss timers pause while the stack is open or while a pointer holds a toast. A progress strip can show the remaining time. Users can also close a notice, activate its action, or swipe it away. assets/…/petal_components.js• L3435-3497 assets/…/petal_components.js• L3674-3693 assets/…/petal_components.js• L3730-3849
Loading toasts are sticky until the server updates or dismisses them. Other toasts use the group duration unless the event supplies another value. The group caps the visible stack and hides older notices from assistive technology when they fall beyond the visible limit. assets/…/petal_components.js• L3609-3671 assets/…/petal_components.js• L3695-3727
Animated feedback lifecycle
Animated text renders its full value on the server. The browser may then rotate words or reveal characters over time. If JavaScript is absent, the user still receives the text. If reduced motion is enabled, the motion hooks stop or leave the server-rendered result in place. lib/petal_components/text_animation.ex#L1-L14 assets/…/petal_components.js• L929-997 assets/…/petal_components.js• L1176-1257
Number changes follow the same rule: the browser animates a value when motion is allowed, but a LiveView update remains authoritative. A celebration canvas is marked hidden from assistive technology and is skipped for reduced-motion users, so visual emphasis does not become duplicate or unwanted spoken content. assets/…/petal_components.js• L929-967 assets/…/petal_components.js• L1047-1099
References: assets/…/petal_components.js• L3435-3497 assets/…/petal_components.js• L3609-3849 lib/…/text_animation.ex• L1-14 assets/…/petal_components.js• L929-997 assets/…/petal_components.js• L1176-1257
Accessible live updates
Live regions are reserved for changes that a user needs to know without moving focus. The component that owns the change chooses the announcement level: polite for routine progress and copy confirmation, assertive for urgent alerts. Decorative icons and celebration graphics stay out of the spoken message.
Announcement levels
The main announcement choices are:
| Meaning | Semantic behavior |
|---|---|
| Routine status or completion | role="status" or aria-live="polite" |
| Urgent danger or warning | role="alert" where the component defines that severity |
| Visual-only decoration | Hidden from assistive technology |
Toast severity follows its own browser contract: danger is an alert and the other toast kinds are status updates. This differs from contextual alerts, where both warning and danger are assertive. Engineers should choose the surface based on the message context rather than assume that the color alone has one global announcement level. assets/…/petal_components.js• L3620-3653 lib/petal_components/alert.ex#L57-L65
Update frequency
Progress status should describe a meaningful stage instead of repeating every percentage. Copy feedback uses a short polite live region and clears it after the visual confirmation ends. This keeps the announcement useful while the visual state returns to its normal form. lib/petal_components/progress.ex#L86-L107 lib/petal_components/input.ex#L200-L210 assets/…/petal_components.js• L278-305
References: lib/…/alert.ex• L57-65 lib/…/progress.ex• L86-107 assets/…/petal_components.js• L3620-3653 lib/…/input.ex• L200-210 assets/…/petal_components.js• L278-305
Related pages
Notification delivery is covered in Notifications and alerts. Timed, animated, and decorative state changes are covered in Visual feedback effects.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the names and payload shapes for
petal:toast,petal:toast-dismiss, andpc-confettialigned between the LiveView API and the browser hooks. - Recheck the one-group rule when changing toast mounting or global event handling. A second group can silently duplicate notices.
- Recheck alert and toast role mapping when adding a severity or changing its normalization. The two surfaces do not use the same assertive set.
- Keep server-rendered fallback content and reduced-motion behavior when changing text, number, or celebration effects.
- Keep progress
value,max, andariavalues aligned when changing completion display or stage status.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| More than one toast group is mounted | Global notices can appear twice or be ignored by the extra group. | Render one group in the root layout. |
| A loading toast is never updated or dismissed | The notice stays visible because loading uses a sticky duration. | Address the toast by its id and send a result or dismissal event when work ends. |
| A progress status is used for every numeric tick | Screen readers receive noisy announcements. | Use status for meaningful stage changes and let progress semantics carry the value. |
| A visual effect is the only completion signal | Users who cannot see or use motion may miss the result. | Send a text status or render a contextual alert with the state change. |
| Reduced motion is ignored by a new effect | Users who opt out still receive unwanted animation. | Preserve the reduced-motion branch and the server-rendered final state. |
Key files
| File | Why to read it |
|---|---|
lib/petal_components/alert.ex | Defines contextual message semantics, actions, and dismissal behavior. |
lib/petal_components/toast.ex | Defines the server contract for transient notices and flash bridging. |
| assets/…/petal_components.js | Shows browser delivery, timing, dismissal, role mapping, and animation behavior. |
lib/petal_components/progress.ex | Defines determinate completion and polite stage updates. |
lib/petal_components/confetti.ex | Defines the server and client paths for celebration feedback. |
lib/petal_components/text_animation.ex | Shows the server-rendered fallback for animated text. |
lib/petal_components/input.ex | Illustrates a short, polite live region for copy confirmation. |