defmodule PlausibleWeb.Components.Generic do
@moduledoc """
Generic reusable components
"""
use Phoenix.Component
@notice_themes %{
yellow: %{
bg: "bg-yellow-50 dark:bg-yellow-100",
icon: "text-yellow-400",
title_text: "text-yellow-800 dark:text-yellow-900",
body_text: "text-yellow-700 dark:text-yellow-800"
},
red: %{
bg: "bg-red-100",
icon: "text-red-700",
title_text: "text-red-800 dark:text-red-900",
body_text: "text-red-700 dark:text-red-800"
}
}
attr(:type, :string, default: "button")
attr(:class, :string, default: "")
attr(:disabled, :boolean, default: false)
attr(:rest, :global)
slot(:inner_block)
def button(assigns) do
~H"""
"""
end
attr(:href, :string, required: true)
attr(:class, :string, default: "")
attr(:rest, :global)
slot(:inner_block)
def button_link(assigns) do
~H"""
<.link
href={@href}
class={[
"inline-flex items-center justify-center gap-x-2 rounded-md bg-indigo-600 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-700 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 disabled:bg-gray-400",
@class
]}
{@rest}
>
<%= render_slot(@inner_block) %>
"""
end
attr(:slug, :string, required: true)
def docs_info(assigns) do
~H"""
"""
end
attr(:title, :any, default: nil)
attr(:size, :atom, default: :sm)
attr(:theme, :atom, default: :yellow)
attr(:dismissable_id, :any, default: nil)
attr(:class, :string, default: "")
attr(:rest, :global)
slot(:inner_block)
def notice(assigns) do
assigns = assign(assigns, :theme, Map.fetch!(@notice_themes, assigns.theme))
~H"""
<%= @title %>
<%= render_slot(@inner_block) %>
"""
end
attr :id, :any, default: nil
attr :href, :string, required: true
attr :new_tab, :boolean, default: false
attr :class, :string, default: ""
slot :inner_block
def styled_link(assigns) do
~H"""
<.unstyled_link
new_tab={@new_tab}
href={@href}
class="text-indigo-600 hover:text-indigo-700 dark:text-indigo-500 dark:hover:text-indigo-600"
>
<%= render_slot(@inner_block) %>
"""
end
slot :button, required: true do
attr :class, :string
end
slot :panel, required: true do
attr :class, :string
end
def dropdown(assigns) do
~H"""
<%= render_slot(List.first(@panel)) %>
"""
end
attr :href, :string, required: true
attr :new_tab, :boolean, default: false
slot :inner_block, required: true
def dropdown_link(assigns) do
~H"""
<.unstyled_link
new_tab={@new_tab}
href={@href}
class="w-full justify-between text-gray-700 dark:text-gray-300 block px-3.5 py-1.5 hover:bg-gray-100 dark:hover:bg-gray-700 hover:text-gray-900 dark:hover:text-gray-100"
>
<%= render_slot(@inner_block) %>
"""
end
attr :href, :string, required: true
attr :new_tab, :boolean, default: false
attr :class, :string, default: ""
attr :id, :any, default: nil
attr :rest, :global
slot :inner_block
def unstyled_link(assigns) do
if assigns[:new_tab] do
assigns = assign(assigns, :icon_class, icon_class(assigns))
~H"""
<.link
id={@id}
class={[
"inline-flex items-center gap-x-0.5",
@class
]}
href={@href}
target="_blank"
rel="noopener noreferrer"
{@rest}
>
<%= render_slot(@inner_block) %>
"""
else
~H"""
<.link class={@class} href={@href}>
<%= render_slot(@inner_block) %>
"""
end
end
defp icon_class(link_assigns) do
if String.contains?(link_assigns[:class], "text-sm") do
["w-3 h-3"]
else
["w-4 h-4"]
end
end
end