defmodule ChesstrainerWeb.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.
The foundation for styling is Tailwind CSS, a utility-first CSS framework.
Semantic colors such as `base-100`, `base-content` and `primary` are defined
in `assets/css/app.css` and switch with the `data-theme` attribute. Here are
useful references:
* [Tailwind CSS](https://tailwindcss.com) - the foundational framework
we build on. You will use it for layout, sizing, flexbox, grid, and
spacing.
* [Heroicons](https://heroicons.com) - see `icon/1` for usage.
* [Phoenix.Component](https://phoenix-live-view.hexdocs.pm/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: ChesstrainerWeb.Gettext
alias Phoenix.LiveView.JS
@input_class "w-full rounded-md border border-base-300 bg-base-100 px-3 py-2 text-sm text-base-content placeholder:text-base-content/50 focus:border-primary focus:outline-none focus:ring-2 focus:ring-primary/30"
@input_error_class "border-error focus:border-error focus:ring-error/30"
@label_class "mb-1 block text-sm font-medium text-base-content"
@checkbox_class "size-4 rounded border-base-300 accent-primary"
defp input_class, do: @input_class
defp input_error_class, do: @input_error_class
defp label_class, do: @label_class
defp checkbox_class, do: @checkbox_class
@table_class "w-full border-collapse text-left text-sm [&_th]:border-b [&_th]:border-base-200 [&_th]:px-4 [&_th]:py-3 [&_th]:font-semibold [&_td]:border-b [&_td]:border-base-200 [&_td]:px-4 [&_td]:py-3 [&_tbody_tr:nth-child(odd)]:bg-base-200/50"
@doc """
Returns the shared data-table classes, for tables that render their own
``/`
` markup.
"""
def table_class, do: @table_class
@doc """
Renders flash notices.
## Examples
<.flash kind={:info} flash={@flash} />
<.flash
id="welcome-back"
kind={:info}
phx-mounted={show("#welcome-back") |> JS.remove_attribute("hidden")}
hidden
>
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"""
"""
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, :any
attr :variant, :string, values: ~w(primary secondary ghost danger)
slot :inner_block, required: true
def button(%{rest: rest} = assigns) do
variants = %{
"primary" => "bg-primary text-primary-content hover:bg-primary/90",
"secondary" => "border border-base-300 bg-base-100 text-base-content hover:bg-base-200",
"ghost" => "text-base-content hover:bg-base-200",
"danger" => "bg-error text-error-content hover:bg-error/90",
nil => "bg-primary/15 text-primary hover:bg-primary/25"
}
assigns =
assign(assigns, :class, [
"inline-flex items-center justify-center gap-2 rounded-md px-4 py-2 text-sm font-semibold transition-colors focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary disabled:cursor-not-allowed disabled:opacity-60",
Map.fetch!(variants, assigns[:variant]),
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 `