SutraUI.Flash (Sutra UI v0.2.0)
View SourceFlash notification component for displaying temporary messages.
Flash messages are used to provide feedback to users after actions, such as form submissions, errors, or successful operations.
Examples
# Basic flash messages
<.flash kind={:info} flash={@flash} />
<.flash kind={:error} flash={@flash} title="Error!" />
# With custom content via inner_block
<.flash kind={:info}>
Custom message with <strong>formatting</strong>
</.flash>Flash Kinds
| Kind | Usage | Icon |
|---|---|---|
:info | Success messages, confirmations, general information | info icon |
:error | Error messages, validation failures, warnings | circle-alert icon |
Setting Flash Messages
In LiveView, use put_flash/3:
def handle_event("save", params, socket) do
case save_data(params) do
{:ok, _} ->
{:noreply, put_flash(socket, :info, "Changes saved successfully")}
{:error, _} ->
{:noreply, put_flash(socket, :error, "Failed to save changes")}
end
endIn controllers, use put_flash/3 from Phoenix.Controller:
def create(conn, params) do
case create_resource(params) do
{:ok, resource} ->
conn
|> put_flash(:info, "Created successfully")
|> redirect(to: ~p"/resources/#{resource}")
{:error, _} ->
conn
|> put_flash(:error, "Failed to create")
|> render(:new)
end
endAccessibility
The flash component follows WAI-ARIA alert pattern guidelines:
| Feature | Implementation |
|---|---|
| Role | role="alert" announces content to screen readers immediately |
| Close button | type="button" with aria-label="close" |
| Icons | Decorative icons hidden from screen readers via aria-hidden |
| Dismissal | Click anywhere on flash or close button to dismiss |
JS Helpers
The module exports show/2 and hide/2 functions for animated visibility:
# Show with animation
SutraUI.Flash.show("#my-element")
# Hide with animation
SutraUI.Flash.hide("#my-element")
# Chain with other JS commands
JS.push("event") |> SutraUI.Flash.hide("#flash")Related
SutraUI.Toast- For non-blocking, auto-dismissing notificationsSutraUI.Alert- For persistent, inline alert messages
Summary
Functions
Renders a flash message.
Hides an element with a fade and scale animation.
Shows an element with a fade and scale animation.
Functions
Renders a flash message.
Flash messages automatically hide when clicked or when the close button is pressed. They also clear the flash from the socket.
Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
id | string | "flash-{kind}" | Unique identifier for the flash element |
flash | map | %{} | The flash map from socket assigns |
title | string | nil | Optional title displayed above the message |
kind | atom | required | :info or :error - determines styling and icon |
Slots
| Slot | Description |
|---|---|
inner_block | Optional custom content (overrides flash message lookup) |
Examples
# Basic usage - reads message from flash map
<.flash kind={:info} flash={@flash} />
# With title
<.flash kind={:error} flash={@flash} title="Validation Error" />
# With custom content (ignores flash map)
<.flash kind={:info}>
Your file <strong>report.pdf</strong> has been uploaded.
</.flash>
# Standalone error (no flash map needed when using inner_block)
<.flash kind={:error} title="Connection Lost">
Please check your internet connection.
</.flash>Attributes
id(:string) - The id of the flash container. Defaults tonil.flash(:map) - The flash map from the socket. Defaults to%{}.title(:string) - Optional title for the flash. Defaults tonil.kind(:atom) (required) - Used for styling and flash lookup. Must be one of:info, or:error.- Global attributes are accepted. Additional HTML attributes.
Slots
inner_block- The optional inner block that renders the flash message.
Hides an element with a fade and scale animation.
Parameters
| Parameter | Type | Description |
|---|---|---|
js | Phoenix.LiveView.JS | Optional JS struct to chain commands |
selector | string | CSS selector for the element to hide |
Examples
# Basic usage
hide("#my-modal")
# Chain with other commands
JS.push("close") |> hide("#modal")Animation
- Duration: 200ms ease-in
- From:
opacity-100 translate-y-0 scale-100 - To:
opacity-0 translate-y-4 scale-95
Shows an element with a fade and scale animation.
Parameters
| Parameter | Type | Description |
|---|---|---|
js | Phoenix.LiveView.JS | Optional JS struct to chain commands |
selector | string | CSS selector for the element to show |
Examples
# Basic usage
show("#my-modal")
# Chain with other commands
JS.push("open") |> show("#modal")Animation
- Duration: 300ms ease-out
- From:
opacity-0 translate-y-4 scale-95 - To:
opacity-100 translate-y-0 scale-100