defmodule SigmaKit.Components.Flash do use Phoenix.LiveComponent import SigmaKit.Components.Js, only: [show: 1, hide: 1, hide: 2] import SigmaKit.Components.Flair, only: [indicator: 1] import SigmaKit.Components.Icons, only: [icon: 1] alias Phoenix.LiveView.JS @doc """ ## Examples <.flash kind={:info} flash={@flash} /> <.flash kind={:info} phx-mounted={show("#flash")}>Welcome Back! """ attr :id, :string, doc: "the optional id of flash container" attr :flash, :map, default: %{}, doc: "the map of flash messages to display" attr :kind, :atom, values: [:info, :error], doc: "used for styling and flash lookup" attr :rest, :global, doc: "the arbitrary HTML attributes to add to the flash container" attr :details, :list, default: nil def flash(assigns) when assigns.details == nil do case Phoenix.Flash.get(assigns.flash, assigns.kind) do details when is_binary(details) -> assign(assigns, details: [message: details]) |> flash() details when is_list(details) -> assign(assigns, details: details) |> flash() nil -> ~H""" """ end end def flash(assigns) do assigns = assign_new(assigns, :id, fn -> "flash-#{assigns.kind}" end) ~H"""
hide("##{@id}")} role="alert" data-autoclose={Keyword.get(@details, :autoclose, 5000)} class={[ "fixed bottom-2 right-2 mr-2 overflow-hidden z-50 shadow-lg bg-white border border-zinc-200 opacity-0 min-w-[400px]" ]} {@rest} >
<.indicator info={@kind == :info} danger={@kind == :error} />
{title}
<.icon :if={Keyword.get(@details, :icon)} name={Keyword.get(@details, :icon)} class={[ "w-8 h-8", @kind == :info && "text-blue-400", @kind == :error && "text-rose-600" ]} /> <.flash_icon :if={!Keyword.get(@details, :icon)} kind={@kind} />

{Keyword.get(@details, :message, "")}

""" end @doc """ Shows the flash group with standard titles and content. ## Examples <.flash_group flash={@flash} /> """ attr :flash, :map, required: true, doc: "the map of flash messages" attr :id, :string, default: "flash-group", doc: "the optional id of flash container" def flash_group(assigns) do ~H"""
<.flash kind={:info} flash={@flash} /> <.flash kind={:error} flash={@flash} />
""" end attr :kind, :atom def flash_icon(%{kind: :error} = assigns) do ~H""" """ end def flash_icon(%{kind: :info} = assigns) do ~H""" """ end end