Toasts with the modern interaction grammar: a collapsed stack that
expands on hover, per-toast timeout progress, pause on hover, swipe to
dismiss, and six positions - plus the LiveView-native parts nobody else
has: server-pushed toasts, id-addressed updates (morph a loading toast
into a success one), action buttons that push LiveView events, and an
automatic put_flash bridge.
Setup
Render one group in your root layout (usually just before </body>):
<.toast_group flash={@flash} />The PetalToast hook ships in the JS bundle (hooks: { ...PetalComponents }).
Sending toasts from LiveView
socket |> Toast.send_toast(:success, title: "Saved", description: "All changes stored.")
# a sticky loading toast, later morphed by id into a result
socket |> Toast.send_toast(:loading, id: "export", title: "Exporting...")
socket |> Toast.send_toast(:success, id: "export", title: "Export ready", duration: 4000)
# an action button that pushes an event back to your LiveView
socket
|> Toast.send_toast(:info,
title: "Message archived",
action: %{label: "Undo", event: "undo-archive", value: %{id: msg.id}}
)put_flash bridge
Anything set with put_flash(:info, ...) / put_flash(:error, ...)
renders as a toast automatically and the flash is cleared - controllers,
redirects and old-school flows get modern toasts for free.
From plain JavaScript
window.dispatchEvent(new CustomEvent("petal:toast", {
detail: {kind: "success", title: "Copied"}
}))
Summary
Functions
Dismisses a toast by id, or every toast with :all - the retraction
half of the API. Useful when an async job is cancelled (dismiss the
loading toast instead of morphing it) or on logout.
Pushes a toast to the client. Returns the socket.
The toast mount point. Render once in your root layout.
Functions
Dismisses a toast by id, or every toast with :all - the retraction
half of the API. Useful when an async job is cancelled (dismiss the
loading toast instead of morphing it) or on logout.
socket |> Toast.dismiss_toast("export")
socket |> Toast.dismiss_toast(:all)
Pushes a toast to the client. Returns the socket.
Options:
:id- address a toast: pushing again with the same id updates it in place (the loading -> success morph):title(required unless updating),:description:duration- ms before auto-dismiss. Defaults to the group'sduration;:loadingtoasts default to sticky (:infinity):action-%{label: "Undo", event: "undo", value: %{}}renders a button that pushes the event to your LiveView:closeable- show the close button (default true):progress- show the timeout progress bar (default true when the toast auto-dismisses)
The toast mount point. Render once in your root layout.
Attributes
id(:string) - one group per layout. Defaults to"pc-toast-group".position(:string) - where the stack lives. Defaults to"bottom-right". Must be one of"top-left","top-center","top-right","bottom-left","bottom-center", or"bottom-right".max(:integer) - visible toasts while collapsed - the rest queue behind and surface as older ones leave. Defaults to3.duration(:integer) - default auto-dismiss in ms. Defaults to5000.flash(:map) - pass @flash to bridge put_flash into toasts automatically (info and error map to their kinds; the flash is cleared once shown). Defaults tonil.- Global attributes are accepted.