Use dropdown component in /sites page (#3572)

* Use dropdown component in sites page

* Add x- attributes to globals

* Update tests

* Prevent default on click

* Fix compile warning

* Add conditional rendering back in

* Remove $id magic
pull/3626/head^2
Uku Taht 3 years ago committed by GitHub
parent 95e7a2b1f6
commit 677b4f2e79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -17,10 +17,4 @@ export default () => ({
this.open = false
focusAfter && focusAfter.focus()
},
onPanelClick(e) {
if (e.target.tagName === 'A') {
this.close()
}
}
})

@ -2,7 +2,7 @@ defmodule PlausibleWeb.Components.Generic do
@moduledoc """
Generic reusable components
"""
use Phoenix.Component
use Phoenix.Component, global_prefixes: ~w(x-)
@notice_themes %{
yellow: %{
@ -68,7 +68,7 @@ defmodule PlausibleWeb.Components.Generic do
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" />
<Heroicons.information_circle class="text-gray-400 w-6 h-6 absolute top-0 right-0" />
</a>
"""
end
@ -161,36 +161,28 @@ defmodule PlausibleWeb.Components.Generic do
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()"
>
<button x-ref="button" x-on:click="toggle()" type="button" class={List.first(@button).class}>
<%= render_slot(List.first(@button)) %>
</button>
<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"
x-ref="panel"
x-show="open"
x-transition:enter="transition ease-out duration-100"
x-transition:enter-start="opacity-0 scale-95"
x-transition:enter-end="opacity-100 scale-100"
x-transition:leave="transition ease-in duration-75"
x-transition:leave-start="opacity-100 scale-100"
x-transition:leave-end="opacity-0 scale-95"
x-on:click.outside="close($refs.button)"
style="display: none;"
class={List.first(@panel).class}
>
<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>
<%= render_slot(List.first(@panel)) %>
</div>
</div>
"""
@ -198,6 +190,7 @@ defmodule PlausibleWeb.Components.Generic do
attr :href, :string, required: true
attr :new_tab, :boolean, default: false
attr :rest, :global
slot :inner_block, required: true
def dropdown_link(assigns) do
@ -205,7 +198,9 @@ defmodule PlausibleWeb.Components.Generic do
<.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"
x-on:click="close()"
class="w-full inline-flex text-gray-700 dark:text-gray-300 px-3.5 py-1.5 hover:bg-gray-100 dark:hover:bg-gray-700 hover:text-gray-900 dark:hover:text-gray-100"
{@rest}
>
<%= render_slot(@inner_block) %>
</.unstyled_link>
@ -227,7 +222,7 @@ defmodule PlausibleWeb.Components.Generic do
<.link
id={@id}
class={[
"inline-flex items-center gap-x-0.5",
"justify-between items-center gap-x-0.5",
@class
]}
href={@href}
@ -241,7 +236,7 @@ defmodule PlausibleWeb.Components.Generic do
"""
else
~H"""
<.link class={@class} href={@href}>
<.link class={@class} href={@href} {@rest}>
<%= render_slot(@inner_block) %>
</.link>
"""

@ -3,7 +3,7 @@ defmodule PlausibleWeb.Live.Sites do
LiveView for sites index.
"""
use Phoenix.LiveView
use Phoenix.LiveView, global_prefixes: ~w(x-)
use PlausibleWeb.Live.Flash
alias Phoenix.LiveView.JS
@ -250,54 +250,23 @@ defmodule PlausibleWeb.Live.Sites do
def ellipsis_menu(assigns) do
~H"""
<div x-data="dropdown">
<a
x-on:click="toggle()"
x-ref="button"
x-bind:aria-expanded="open"
x-bind:aria-controls="$id('dropdown-button')"
class="absolute top-0 right-0 h-10 w-10 rounded-md hover:cursor-pointer text-gray-400 dark:text-gray-600 hover:text-black dark:hover:text-indigo-400"
>
<Heroicons.ellipsis_vertical
class="absolute top-3 right-3 w-4 h-4"
aria-expanded="false"
aria-haspopup="true"
/>
</a>
<div
x-ref="panel"
x-show="open"
x-bind:id="$id('dropdown-button')"
x-on:click.outside="close($refs.button)"
x-on:click="onPanelClick"
x-transition.origin.top.right
x-transition.duration.100ms
class="absolute top-7 right-3 z-10 mt-2 w-40 origin-top-right rounded-md bg-white dark:bg-gray-900 shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none"
style="display: none;"
role="menu"
aria-orientation="vertical"
aria-label={"#{@site.domain} menu button"}
tabindex="-1"
>
<.dropdown>
<:button class="absolute top-0 right-0 h-10 w-10 rounded-md hover:cursor-pointer text-gray-400 dark:text-gray-600 hover:text-black dark:hover:text-indigo-400">
<Heroicons.ellipsis_vertical class="absolute top-3 right-3 w-4 h-4" />
</:button>
<:panel class="absolute top-7 right-3 z-10 mt-2 w-40 rounded-md bg-white dark:bg-gray-900 shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none">
<div class="py-1 text-sm" role="none">
<.unstyled_link
<.dropdown_link
:if={List.first(@site.memberships).role != :viewer}
href={"/#{URI.encode_www_form(@site.domain)}/settings/general"}
class="text-gray-500 flex px-4 py-2 hover:bg-gray-100 hover:text-gray-800 dark:text-gray-500 dark:hover:text-gray-100 dark:hover:bg-indigo-900 cursor-pointer"
role="menuitem"
tabindex="-1"
>
<Heroicons.cog_6_tooth class="mr-3 h-5 w-5" />
<span>Settings</span>
</.unstyled_link>
<button
type="button"
class="w-full text-gray-500 flex px-4 py-2 hover:bg-gray-100 hover:text-gray-800 dark:text-gray-500 dark:hover:text-gray-100 dark:hover:bg-indigo-900 cursor-pointer"
role="menuitem"
tabindex="-1"
x-on:click="close($refs.button)"
</.dropdown_link>
<.dropdown_link
href="#"
x-on:click.prevent
phx-click={
JS.hide(
transition: {"duration-500", "opacity-100", "opacity-0"},
@ -316,10 +285,10 @@ defmodule PlausibleWeb.Live.Sites do
<.icon_pin :if={!@site.pinned_at} class="pt-1 mr-3 h-5 w-5" />
<span :if={!@site.pinned_at}>Pin Site</span>
</button>
</.dropdown_link>
</div>
</div>
</div>
</:panel>
</.dropdown>
"""
end

@ -74,7 +74,7 @@ defmodule PlausibleWeb.Live.SitesTest do
assert text_of_element(
html,
~s/li[data-domain="#{site.domain}"] button/
~s/li[data-domain="#{site.domain}"] a[phx-value-domain]/
) == "Pin Site"
end
@ -83,7 +83,7 @@ defmodule PlausibleWeb.Live.SitesTest do
{:ok, lv, _html} = live(conn, "/sites")
button_selector = ~s/li[data-domain="#{site.domain}"] button/
button_selector = ~s/li[data-domain="#{site.domain}"] a[phx-value-domain]/
html =
lv
@ -114,7 +114,7 @@ defmodule PlausibleWeb.Live.SitesTest do
{:ok, lv, _html} = live(conn, "/sites")
button_selector = ~s/li[data-domain="#{site.domain}"] button/
button_selector = ~s/li[data-domain="#{site.domain}"] a[phx-value-domain]/
html =
lv

Loading…
Cancel
Save