defmodule AprsmeWeb.CoreComponents do @moduledoc """ Provides core UI components. The components in this module use Tailwind CSS, a utility-first CSS framework. See the [Tailwind CSS documentation](https://tailwindcss.com) to learn how to customize the generated components in this module. Icons are provided by [heroicons](https://heroicons.com), vendored in priv/heroicons/. """ use Phoenix.Component use Gettext, backend: AprsmeWeb.Gettext alias Phoenix.HTML.Form alias Phoenix.LiveView.JS @doc """ Renders a Heroicon SVG inline from priv/heroicons/. SVGs are loaded from trusted vendored heroicons directory and are safe to render as raw HTML. Usage: <.icon name="arrow-left" outline={true} class="h-5 w-5" /> """ attr :name, :string, required: true attr :outline, :boolean, default: true attr :class, :string, default: nil def icon(%{name: name, outline: outline, class: class} = assigns) do style = if outline, do: "outline", else: "solid" path = Path.join([ :aprsme |> :code.priv_dir() |> to_string(), "heroicons/24", style, name <> ".svg" ]) svg = case File.read(path) do {:ok, contents} -> # Insert class attribute if not present # SVG is from trusted vendored heroicons, safe to modify and render Regex.replace(~r/]*?)>/, contents, fn _, attrs -> add_class_to_svg_tag(attrs, class) end) _ -> "" end assigns = assign(assigns, :svg, Phoenix.HTML.raw(svg)) ~H""" {@svg} """ end @doc """ Renders a modal. ## Examples <.modal id="confirm-modal"> Are you sure? <:confirm>OK <:cancel>Cancel JS commands may be passed to the `:on_cancel` and `on_confirm` attributes for the caller to react to each button press, for example: <.modal id="confirm" on_confirm={JS.push("delete")} on_cancel={JS.navigate(~p"/posts")}> Are you sure you? <:confirm>OK <:cancel>Cancel """ attr :id, :string, required: true attr :show, :boolean, default: false attr :on_cancel, JS, default: %JS{} attr :on_confirm, JS, default: %JS{} slot :inner_block, required: true slot :title slot :subtitle slot :confirm slot :cancel def modal(assigns) do ~H"""