defmodule BerrypodWeb.CoreComponents do @moduledoc """ Provides core UI components. At first glance, this module may seem daunting, but its goal is to provide core building blocks for your application, such as tables, forms, and inputs. The components consist mostly of markup and are well-documented with doc strings and declarative assigns. You may customize and style them in any way you want, based on your application growth and needs. Styled with custom admin CSS (`assets/css/admin/components.css`). * [Heroicons](https://heroicons.com) - see `icon/1` for usage. * [Phoenix.Component](https://hexdocs.pm/phoenix_live_view/Phoenix.Component.html) - the component system used by Phoenix. Some components, such as `<.link>` and `<.form>`, are defined there. """ use Phoenix.Component use Gettext, backend: BerrypodWeb.Gettext alias Phoenix.LiveView.JS @doc """ Renders flash notices. ## 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 :title, :string, default: nil 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" slot :inner_block, doc: "the optional inner block that renders the flash message" def flash(assigns) do assigns = assign_new(assigns, :id, fn -> "flash-#{assigns.kind}" end) ~H"""
hide("##{@id}")} role="alert" class="admin-toast" {@rest} >
<.icon :if={@kind == :info} name="hero-information-circle" class="size-5 shrink-0" /> <.icon :if={@kind == :error} name="hero-exclamation-circle" class="size-5 shrink-0" />

{@title}

{msg}

""" end @doc """ Renders a button with navigation support. ## Examples <.button>Send! <.button phx-click="go" variant="primary">Send! <.button navigate={~p"/"}>Home """ attr :rest, :global, include: ~w(href navigate patch method download name value disabled) attr :class, :string attr :variant, :string, values: ~w(primary outline) slot :inner_block, required: true def button(%{rest: rest} = assigns) do variants = %{ "primary" => "admin-btn-primary", "outline" => "admin-btn-outline", nil => "admin-btn-primary admin-btn-soft" } variant_class = Map.fetch!(variants, assigns[:variant]) assigns = assign(assigns, :class, [ "admin-btn", variant_class, assigns[:class] ]) if rest[:href] || rest[:navigate] || rest[:patch] do ~H""" <.link class={@class} {@rest}> {render_slot(@inner_block)} """ else ~H""" """ end end @doc """ Renders an input with label and error messages. A `Phoenix.HTML.FormField` may be passed as argument, which is used to retrieve the input name, id, and values. Otherwise all attributes may be passed explicitly. ## Types This function accepts all HTML input types, considering that: * You may also set `type="select"` to render a ` {@label} <.error :for={msg <- @errors}>{msg}
""" end def input(%{type: "select"} = assigns) do assigns = assign(assigns, :error_id, assigns.id && "#{assigns.id}-error") ~H"""
<.error :for={msg <- @errors} id={@error_id}>{msg}
""" end def input(%{type: "textarea"} = assigns) do assigns = assign(assigns, :error_id, assigns.id && "#{assigns.id}-error") ~H"""
<.error :for={msg <- @errors} id={@error_id}>{msg}
""" end # All other inputs text, datetime-local, url, password, etc. are handled here... def input(assigns) do assigns = assign(assigns, :error_id, assigns.id && "#{assigns.id}-error") ~H"""
<.error :for={msg <- @errors} id={@error_id}>{msg}
""" end # Helper used by inputs to generate form errors attr :id, :string, default: nil slot :inner_block, required: true defp error(assigns) do ~H""" """ end @doc """ Renders a header with title. """ slot :inner_block, required: true slot :subtitle slot :actions def header(assigns) do ~H"""

{render_slot(@inner_block)}

{render_slot(@subtitle)}

{render_slot(@actions)}
""" end @doc """ Renders a table with generic styling. ## Examples <.table id="users" rows={@users}> <:col :let={user} label="id">{user.id} <:col :let={user} label="username">{user.username} """ attr :id, :string, required: true attr :rows, :list, required: true attr :row_id, :any, default: nil, doc: "the function for generating the row id" attr :row_click, :any, default: nil, doc: "the function for handling phx-click on each row" attr :row_item, :any, default: &Function.identity/1, doc: "the function for mapping each row before calling the :col and :action slots" slot :col, required: true do attr :label, :string end slot :action, doc: "the slot for showing user actions in the last table column" def table(assigns) do assigns = with %{rows: %Phoenix.LiveView.LiveStream{}} <- assigns do assign(assigns, row_id: assigns.row_id || fn {id, _item} -> id end) end ~H"""
{col[:label]} {gettext("Actions")}
{render_slot(col, @row_item.(row))}
<%= for action <- @action do %> {render_slot(action, @row_item.(row))} <% end %>
""" end @doc """ Renders a data list. ## Examples <.list> <:item title="Title">{@post.title} <:item title="Views">{@post.views} """ slot :item, required: true do attr :title, :string, required: true end def list(assigns) do ~H""" """ end @doc """ Renders a [Heroicon](https://heroicons.com). Heroicons come in three styles – outline, solid, and mini. By default, the outline style is used, but solid and mini may be applied by using the `-solid` and `-mini` suffix. You can customize the size and colors of the icons by setting width, height, and background color classes. Icons are extracted from the `deps/heroicons` directory and bundled into `admin/icons.css` by `mix generate_admin_icons`. ## Examples <.icon name="hero-x-mark" /> <.icon name="hero-arrow-path" class="ml-1 size-3 motion-safe:animate-spin" /> """ attr :name, :string, required: true attr :class, :string, default: "size-4" def icon(%{name: "hero-" <> _} = assigns) do ~H""" """ end @doc """ Renders a link to an external site with proper security attributes, an external-link icon, and screen reader context. Set `icon={false}` when the link already contains its own visual indicator. """ attr :href, :string, required: true attr :class, :string, default: nil attr :icon, :boolean, default: true attr :rest, :global slot :inner_block, required: true def external_link(assigns) do ~H""" {render_slot(@inner_block)} <.icon :if={@icon} name="hero-arrow-top-right-on-square" class="external-link-icon" /> (opens in new tab) """ end ## JS Commands def show(js \\ %JS{}, selector) do JS.show(js, to: selector, time: 300, transition: {"transition-all ease-out duration-300", "opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95", "opacity-100 translate-y-0 sm:scale-100"} ) end def hide(js \\ %JS{}, selector) do JS.hide(js, to: selector, time: 200, transition: {"transition-all ease-in duration-200", "opacity-100 translate-y-0 sm:scale-100", "opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"} ) end @doc """ Translates an error message using gettext. """ def translate_error({msg, opts}) do # When using gettext, we typically pass the strings we want # to translate as a static argument: # # # Translate the number of files with plural rules # dngettext("errors", "1 file", "%{count} files", count) # # However the error messages in our forms and APIs are generated # dynamically, so we need to translate them by calling Gettext # with our gettext backend as first argument. Translations are # available in the errors.po file (as we use the "errors" domain). if count = opts[:count] do Gettext.dngettext(BerrypodWeb.Gettext, "errors", msg, msg, count, opts) else Gettext.dgettext(BerrypodWeb.Gettext, "errors", msg, opts) end end @doc """ Translates the errors for a field from a keyword list of errors. """ def translate_errors(errors, field) when is_list(errors) do for {^field, {msg, opts}} <- errors, do: translate_error({msg, opts}) end @doc """ Renders a modal dialog. Uses daisyUI's modal component with proper accessibility. ## Examples <.modal id="confirm-modal"> Are you sure? <:actions> """ attr :id, :string, required: true attr :show, :boolean, default: false attr :on_cancel, JS, default: %JS{} slot :inner_block, required: true slot :actions def modal(assigns) do ~H"""
{render_slot(@inner_block)}
{render_slot(@actions)}
""" end @doc """ Renders a radio card group — a set of selectable cards backed by radio inputs. Each option is a map with `:value` and `:name`, plus optional `:description`, `:tags`, `:url`, `:badge`, and `:disabled` keys. The `display` attr controls card content layout: - `:tags` (default) — name + tag pills + short description - `:description` — name + description text + link ## Examples <.card_radio_group name="email[adapter]" value={@selected} legend="Email provider" options={[ %{value: "postmark", name: "Postmark", description: "Fast email.", tags: ["Transactional", "US"]}, %{value: "smtp", name: "SMTP", description: "Any SMTP server.", tags: ["Any type"]} ]} /> """ attr :name, :string, required: true attr :value, :string, default: nil attr :legend, :string, required: true attr :options, :list, required: true attr :disabled, :boolean, default: false attr :display, :atom, default: :tags, values: [:description, :tags] def card_radio_group(assigns) do ~H"""
{@legend}
""" end attr :option, :map, required: true attr :display, :atom, required: true defp card_radio_content(%{display: :tags} = assigns) do ~H""" {tag} {@option.description} {@option.badge} """ end defp card_radio_content(assigns) do ~H""" {@option.description} {@option.badge} <.external_link :if={@option[:url]} href={@option.url} icon={false} class="card-radio-link" onclick="event.stopPropagation();" aria-label={@option.name} > {@option.name} ↗ """ end def show_modal(js \\ %JS{}, id) when is_binary(id) do js |> JS.exec("showModal()", to: "##{id}") end def hide_modal(js \\ %JS{}, id) when is_binary(id) do js |> JS.exec("close()", to: "##{id}") |> JS.pop_focus() end # ── Pagination ─────────────────────────────────────────────────── @doc """ Renders pagination controls for admin lists. Hidden when there's only one page. Shows "Showing X-Y of Z" on the left, page number buttons with ellipsis on the right. ## Attributes * `page` - Required. A `%Berrypod.Pagination{}` struct. * `patch` - Required. Base URL path for pagination links (e.g. "/admin/products"). * `params` - Extra query params to preserve (e.g. %{"tab" => "broken"}). Default `%{}`. """ attr :page, Berrypod.Pagination, required: true attr :patch, :string, required: true attr :params, :map, default: %{} def admin_pagination(assigns) do assigns = assigns |> assign(:showing, Berrypod.Pagination.showing_text(assigns.page)) |> assign(:numbers, Berrypod.Pagination.page_numbers(assigns.page)) ~H""" """ end defp admin_page_url(base, page, params) do query = if page > 1, do: Map.put(params, "page", to_string(page)), else: params if query == %{}, do: base, else: base <> "?" <> URI.encode_query(query) end end