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"""
{Keyword.get(@details, :message, "")}