PUI. Flash
(pui v1.0.0-beta.17)
Copy Markdown
Toast notification system for LiveView applications.
Basic Setup
Add the flash group to your layout:
<PUI.Flash.flash_group flash={@flash} />For LiveView pages with dynamic flashes:
<PUI.Flash.flash_group flash={@flash} live={true} />Sending Flashes
From a LiveView:
PUI.Flash.send_flash("Operation completed successfully!")With custom options:
PUI.Flash.send_flash(%PUI.Flash.Message{
type: :success,
message: "Saved!",
duration: 8,
class: "border-green-500"
})Phoenix Preset Toasts
Flash keys commonly used by Phoenix (:success, :error, :info, :warning)
are rendered as compact pill-shaped toasts with a type-colored icon. They are
positioned at top-center by default, show a visible close button, and truncate
to a single line.
{:noreply, put_flash(socket, :success, "Changes saved!")}
{:noreply, put_flash(socket, :error, "Could not save changes")}
{:noreply, put_flash(socket, :warning, "Session expires soon")}
{:noreply, put_flash(socket, :info, "New update available")}You can also trigger the preset toast style through send_flash by setting
one of those types:
PUI.Flash.send_flash(%PUI.Flash.Message{
type: :success,
message: "Connected!"
})Positioning
Position the flash container in different corners:
<PUI.Flash.flash_group flash={@flash} position="top-right" />
<PUI.Flash.flash_group flash={@flash} position="top-center" />
<PUI.Flash.flash_group flash={@flash} position="bottom-left" />Available positions: top-left, top-center, top-right,
bottom-left, bottom-center, bottom-right
Custom Content
Send HEEx content in flashes. When message is a HEEx template, the custom
markup overrides the preset toast styling:
PUI.Flash.send_flash(%PUI.Flash.Message{
type: :success,
message: ~H|<div class="flex items-center gap-2">
<.icon name="hero-check-circle" class="size-5" />
<span>Success!</span>
</div>|
})Plain-string messages with a preset type still render as the compact built-in toast with a type-colored icon.
Updating Flashes
Update an existing flash by ID:
PUI.Flash.update_flash(%PUI.Flash.Message{
id: "my-flash",
message: "Updated!"
})Configuration
| Attribute | Type | Default | Description |
|---|---|---|---|
flash | map | required | Phoenix flash map |
live | boolean | false | Enable LiveComponent for dynamic updates |
position | string | "top-center" | Container position |
limit | integer | 5 | Maximum number of visible flashes |
auto_dismiss | integer | 5000 | Auto-dismiss delay in ms |
show_close | boolean | true | Show close button |
Message Struct
Create flash messages with the Message struct:
%PUI.Flash.Message{
message: "Hello!", # Required
type: nil, # :info, :success, :warning, :error, or nil
preset: false, # True for Phoenix preset toast styling
duration: 5, # Seconds until auto-dismiss
auto_dismiss: true, # Auto-dismiss enabled
dismissable: true, # Allow manual dismiss
show_close: true, # Show close button
class: "" # Additional CSS classes
}
Summary
Functions
Flash container with positioning support.
Individual flash message component.
Use .flash_group to place flash container in your layout.
Callback implementation for Phoenix.LiveComponent.handle_event/3.
Callback implementation for Phoenix.LiveComponent.mount/1.
Callback implementation for Phoenix.LiveComponent.render/1.
Callback implementation for Phoenix.LiveComponent.update/2.
Functions
Flash container with positioning support.
Attributes
position(:string) - Defaults to"top-center".- Global attributes are accepted.
Slots
inner_block
Individual flash message component.
When preset is true (set automatically for Phoenix flash keys such as
:success, :error, :info, and :warning), the message renders as a
compact toast with a type-colored icon and a dark pill-shaped container.
Attributes
id(:string)position(:string) - Defaults to"top-center".type(:atom) - Defaults to:info.preset(:boolean) - Defaults tofalse.class(:string) - Defaults to"".show_close(:boolean) - Defaults totrue.- Global attributes are accepted.
Slots
inner_block
Use .flash_group to place flash container in your layout.
If you want to use in liveview page, pass option live={true}
example:
<PUI.Flash.flash_group flash={@flash} live={true}>Attributes
flash(:map) (required)live(:boolean) - Defaults tofalse.limit(:integer) - Defaults to5.position(:string) - Defaults to"top-center". Must be one of"top-left","top-right","top-center","bottom-left","bottom-right", or"bottom-center".auto_dismiss(:integer) - Defaults to5000.show_close(:boolean) - Defaults totrue.
Callback implementation for Phoenix.LiveComponent.handle_event/3.
Callback implementation for Phoenix.LiveComponent.mount/1.
Callback implementation for Phoenix.LiveComponent.render/1.
Callback implementation for Phoenix.LiveComponent.update/2.