2023-06-22 10:00:07 +03:00
|
|
|
defmodule PlausibleWeb.Components.Generic do
|
|
|
|
@moduledoc """
|
|
|
|
Generic reusable components
|
|
|
|
"""
|
|
|
|
use Phoenix.Component
|
|
|
|
|
2023-11-16 18:40:50 +03:00
|
|
|
@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"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-08 12:40:07 +03:00
|
|
|
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"""
|
|
|
|
<button
|
|
|
|
type={@type}
|
|
|
|
disabled={@disabled}
|
|
|
|
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) %>
|
|
|
|
</button>
|
|
|
|
"""
|
|
|
|
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) %>
|
|
|
|
</.link>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
2023-10-18 15:01:17 +03:00
|
|
|
attr(:slug, :string, required: true)
|
|
|
|
|
|
|
|
def docs_info(assigns) do
|
|
|
|
~H"""
|
|
|
|
<a href={"https://plausible.io/docs/#{@slug}"} rel="noreferrer" target="_blank">
|
|
|
|
<Heroicons.information_circle class="text-gray-400 w-6 h-6 absolute top-0 right-0 text-gray-400" />
|
|
|
|
</a>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
2023-11-16 18:40:50 +03:00
|
|
|
attr(:title, :any, default: nil)
|
2023-10-18 15:01:17 +03:00
|
|
|
attr(:size, :atom, default: :sm)
|
2023-11-16 18:40:50 +03:00
|
|
|
attr(:theme, :atom, default: :yellow)
|
2023-11-13 19:19:58 +03:00
|
|
|
attr(:dismissable_id, :any, default: nil)
|
|
|
|
attr(:class, :string, default: "")
|
2023-10-18 15:01:17 +03:00
|
|
|
attr(:rest, :global)
|
|
|
|
slot(:inner_block)
|
2023-06-22 10:00:07 +03:00
|
|
|
|
|
|
|
def notice(assigns) do
|
2023-11-16 18:40:50 +03:00
|
|
|
assigns = assign(assigns, :theme, Map.fetch!(@notice_themes, assigns.theme))
|
|
|
|
|
2023-06-22 10:00:07 +03:00
|
|
|
~H"""
|
2023-11-13 19:19:58 +03:00
|
|
|
<div id={@dismissable_id} class={@dismissable_id && "hidden"}>
|
2023-11-16 18:40:50 +03:00
|
|
|
<div class={["rounded-md p-4 relative", @theme.bg, @class]} {@rest}>
|
2023-11-13 19:19:58 +03:00
|
|
|
<button
|
|
|
|
:if={@dismissable_id}
|
2023-11-16 18:40:50 +03:00
|
|
|
class={"absolute right-0 top-0 m-2 #{@theme.title_text}"}
|
2023-11-13 19:19:58 +03:00
|
|
|
onclick={"localStorage['notice_dismissed__#{@dismissable_id}'] = 'true'; document.getElementById('#{@dismissable_id}').classList.add('hidden')"}
|
|
|
|
>
|
|
|
|
<Heroicons.x_mark class="h-4 w-4 hover:stroke-2" />
|
|
|
|
</button>
|
|
|
|
<div class="flex">
|
2023-11-16 18:40:50 +03:00
|
|
|
<div :if={@title} class="flex-shrink-0">
|
2023-11-13 19:19:58 +03:00
|
|
|
<svg
|
2023-11-16 18:40:50 +03:00
|
|
|
class={"h-5 w-5 #{@theme.icon}"}
|
2023-11-13 19:19:58 +03:00
|
|
|
viewBox="0 0 20 20"
|
|
|
|
fill="currentColor"
|
|
|
|
aria-hidden="true"
|
|
|
|
>
|
|
|
|
<path
|
|
|
|
fill-rule="evenodd"
|
|
|
|
d="M8.485 2.495c.673-1.167 2.357-1.167 3.03 0l6.28 10.875c.673 1.167-.17 2.625-1.516 2.625H3.72c-1.347 0-2.189-1.458-1.515-2.625L8.485 2.495zM10 5a.75.75 0 01.75.75v3.5a.75.75 0 01-1.5 0v-3.5A.75.75 0 0110 5zm0 9a1 1 0 100-2 1 1 0 000 2z"
|
|
|
|
clip-rule="evenodd"
|
|
|
|
/>
|
|
|
|
</svg>
|
|
|
|
</div>
|
2023-11-16 18:40:50 +03:00
|
|
|
<div class={["w-full", @title && "ml-3"]}>
|
|
|
|
<h3 :if={@title} class={"text-#{@size} font-medium #{@theme.title_text} mb-2"}>
|
2023-11-13 19:19:58 +03:00
|
|
|
<%= @title %>
|
|
|
|
</h3>
|
2023-11-16 18:40:50 +03:00
|
|
|
<div class={"text-#{@size} #{@theme.body_text}"}>
|
2023-11-13 19:19:58 +03:00
|
|
|
<p>
|
|
|
|
<%= render_slot(@inner_block) %>
|
|
|
|
</p>
|
|
|
|
</div>
|
2023-06-22 10:00:07 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-11-16 18:40:50 +03:00
|
|
|
<script :if={@dismissable_id} data-key={@dismissable_id}>
|
2023-11-13 19:19:58 +03:00
|
|
|
const dismissId = document.currentScript.dataset.key
|
|
|
|
const localStorageKey = `notice_dismissed__${dismissId}`
|
|
|
|
|
|
|
|
if (localStorage[localStorageKey] !== 'true') {
|
|
|
|
document.getElementById(dismissId).classList.remove('hidden')
|
|
|
|
}
|
|
|
|
</script>
|
2023-06-22 10:00:07 +03:00
|
|
|
"""
|
|
|
|
end
|
2023-09-21 13:55:56 +03:00
|
|
|
|
2023-10-10 20:35:17 +03:00
|
|
|
attr :id, :any, default: nil
|
2023-09-21 13:55:56 +03:00
|
|
|
attr :href, :string, required: true
|
2023-10-17 12:01:27 +03:00
|
|
|
attr :new_tab, :boolean, default: false
|
2023-09-21 13:55:56 +03:00
|
|
|
attr :class, :string, default: ""
|
|
|
|
slot :inner_block
|
|
|
|
|
|
|
|
def styled_link(assigns) do
|
2023-10-17 12:01:27 +03:00
|
|
|
~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) %>
|
|
|
|
</.unstyled_link>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
slot :button, required: true do
|
|
|
|
attr :class, :string
|
|
|
|
end
|
|
|
|
|
|
|
|
slot :panel, required: true do
|
|
|
|
attr :class, :string
|
|
|
|
end
|
|
|
|
|
|
|
|
def dropdown(assigns) do
|
|
|
|
~H"""
|
|
|
|
<div class="flex justify-center">
|
|
|
|
<div
|
|
|
|
x-data="dropdown"
|
|
|
|
x-on:keydown.escape.prevent.stop="close($refs.button)"
|
|
|
|
x-on:focusin.window="! $refs.panel.contains($event.target) && close()"
|
|
|
|
x-id="['dropdown-button']"
|
|
|
|
class="relative"
|
|
|
|
>
|
|
|
|
<button
|
|
|
|
x-ref="button"
|
|
|
|
x-on:click="toggle()"
|
|
|
|
x-bind:aria-expanded="open"
|
|
|
|
x-bind:aria-controls="$id('dropdown-button')"
|
|
|
|
type="button"
|
|
|
|
class={List.first(@button).class}
|
|
|
|
>
|
|
|
|
<%= render_slot(List.first(@button)) %>
|
|
|
|
</button>
|
|
|
|
<div
|
|
|
|
x-ref="panel"
|
|
|
|
x-show="open"
|
|
|
|
x-transition.origin.top.left
|
|
|
|
x-on:click.outside="close($refs.button)"
|
|
|
|
x-on:click="onPanelClick"
|
|
|
|
x-bind:id="$id('dropdown-button')"
|
|
|
|
style="display: none;"
|
|
|
|
class={List.first(@panel).class}
|
|
|
|
>
|
|
|
|
<%= render_slot(List.first(@panel)) %>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
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) %>
|
|
|
|
</.unstyled_link>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
attr :href, :string, required: true
|
|
|
|
attr :new_tab, :boolean, default: false
|
|
|
|
attr :class, :string, default: ""
|
2023-10-17 13:16:04 +03:00
|
|
|
attr :id, :any, default: nil
|
2023-11-13 11:08:26 +03:00
|
|
|
attr :rest, :global
|
2023-10-17 12:01:27 +03:00
|
|
|
slot :inner_block
|
|
|
|
|
|
|
|
def unstyled_link(assigns) do
|
2023-09-21 13:55:56 +03:00
|
|
|
if assigns[:new_tab] do
|
|
|
|
assigns = assign(assigns, :icon_class, icon_class(assigns))
|
|
|
|
|
|
|
|
~H"""
|
|
|
|
<.link
|
2023-10-10 20:35:17 +03:00
|
|
|
id={@id}
|
2023-09-21 13:55:56 +03:00
|
|
|
class={[
|
2023-10-17 12:01:27 +03:00
|
|
|
"inline-flex items-center gap-x-0.5",
|
2023-09-21 13:55:56 +03:00
|
|
|
@class
|
|
|
|
]}
|
|
|
|
href={@href}
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
2023-11-13 11:08:26 +03:00
|
|
|
{@rest}
|
2023-09-21 13:55:56 +03:00
|
|
|
>
|
|
|
|
<%= render_slot(@inner_block) %>
|
2023-10-17 12:01:27 +03:00
|
|
|
<Heroicons.arrow_top_right_on_square class={["opacity-60", @icon_class]} />
|
2023-09-21 13:55:56 +03:00
|
|
|
</.link>
|
|
|
|
"""
|
|
|
|
else
|
|
|
|
~H"""
|
2023-10-17 12:01:27 +03:00
|
|
|
<.link class={@class} href={@href}>
|
2023-09-21 13:55:56 +03:00
|
|
|
<%= render_slot(@inner_block) %>
|
|
|
|
</.link>
|
|
|
|
"""
|
|
|
|
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
|
2023-06-22 10:00:07 +03:00
|
|
|
end
|