"""
end
@doc type: :component
@doc """
Renders a div that creates a toast notification when a server error occurs.
This component should be placed in your layout and will automatically
create a toast when Phoenix LiveView detects a server-side connection error.
## Examples
<.toast_server_error
title="Something went wrong!"
description="Attempting to reconnect"
type={:error}
duration={:infinity}
/>
"""
attr(:id, :string, default: "server-error-toast")
attr(:title, :string, required: true)
attr(:description, :string, default: nil)
attr(:type, :atom, default: :error, values: [:info, :success, :error])
attr(:duration, :any, default: :infinity)
def toast_server_error(assigns) do
type_str =
case assigns.type do
:info -> "info"
:success -> "success"
:error -> "error"
_ -> "error"
end
duration_str = if assigns.duration == :infinity, do: "Infinity", else: assigns.duration
assigns =
assigns
|> assign(:type_str, type_str)
|> assign(:duration_str, duration_str)
~H"""
"""
end
@doc type: :component
@doc """
Renders a div that creates a toast notification when the connection is restored.
This component should be placed in your layout and will automatically
create a toast when Phoenix LiveView detects that the connection has been restored.
## Examples
<.toast_connected
title="Connection restored"
description="You're back online"
type={:success}
/>
"""
attr(:id, :string, default: "connected-toast")
attr(:title, :string, required: true)
attr(:description, :string, default: nil)
attr(:type, :atom, default: :success, values: [:info, :success, :error])
attr(:duration, :any, default: 5000)
def toast_connected(assigns) do
type_str =
case assigns.type do
:info -> "info"
:success -> "success"
:error -> "error"
_ -> "success"
end
duration_str = if assigns.duration == :infinity, do: "Infinity", else: assigns.duration
assigns =
assigns
|> assign(:type_str, type_str)
|> assign(:duration_str, duration_str)
~H"""
"""
end
@doc type: :component
@doc """
Renders a div that creates a toast notification when the connection is lost.
This component should be placed in your layout and will automatically
create a toast when Phoenix LiveView detects that the connection has been lost.
## Examples
<.toast_disconnected
title="Connection lost"
description="Attempting to reconnect"
type={:warning}
duration={:infinity}
/>
"""
attr(:id, :string, default: "disconnected-toast")
attr(:title, :string, required: true)
attr(:description, :string, default: nil)
attr(:type, :atom, default: :info, values: [:info, :success, :error])
attr(:duration, :any, default: :infinity)
def toast_disconnected(assigns) do
type_str =
case assigns.type do
:info -> "info"
:success -> "success"
:error -> "error"
_ -> "info"
end
duration_str = if assigns.duration == :infinity, do: "Infinity", else: assigns.duration
assigns =
assigns
|> assign(:type_str, type_str)
|> assign(:duration_str, duration_str)
~H"""
"""
end
@doc type: :api
@doc """
Creates a toast notification programmatically (client-side).
This function returns a JS command that can be used in event handlers.
## Examples
def handle_event("save", _params, socket) do
# ... save logic ...
{:noreply, push_event(socket, "toast-create", %{
title: "Saved!",
description: "Your changes have been saved.",
type: "success"
})}
end
Or use the JS command version:
"""
def create_toast(title, description \\ nil, type \\ :info, opts \\ []) do
duration = Keyword.get(opts, :duration, 5000)
duration_str = if duration == :infinity, do: "Infinity", else: duration
type_str =
case type do
:info -> "info"
:success -> "success"
:error -> "error"
_ -> "info"
end
Phoenix.LiveView.JS.dispatch("toast:create",
to: ".toast-group-js",
detail: %{
title: title,
description: description,
type: type_str,
duration: duration_str
}
)
end
@doc type: :api
@doc """
Server-side function to push a toast event to the client.
Use this in your LiveView event handlers.
## Examples
def handle_event("save", _params, socket) do
# ... save logic ...
{:noreply, push_toast(socket, "Saved!", "Your changes have been saved.", :success)}
end
"""
def push_toast(socket, title, description \\ nil, type \\ :info, duration \\ 5000) do
duration_str = if duration == :infinity, do: "Infinity", else: duration
type_str =
case type do
:info -> "info"
:success -> "success"
:error -> "error"
_ -> "info"
end
Phoenix.LiveView.push_event(socket, "toast-create", %{
title: title,
description: description,
type: type_str,
duration: duration_str
})
end
end