From 677b4f2e7963258b5e1a562b1cff782e5c7a1f76 Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Wed, 13 Dec 2023 10:59:20 +0200 Subject: [PATCH] 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 --- assets/js/liveview/dropdown.js | 6 --- lib/plausible_web/components/generic.ex | 61 ++++++++++++------------- lib/plausible_web/live/sites.ex | 61 ++++++------------------- test/plausible_web/live/sites_test.exs | 6 +-- 4 files changed, 46 insertions(+), 88 deletions(-) diff --git a/assets/js/liveview/dropdown.js b/assets/js/liveview/dropdown.js index 490b3217f..18175bb3f 100644 --- a/assets/js/liveview/dropdown.js +++ b/assets/js/liveview/dropdown.js @@ -17,10 +17,4 @@ export default () => ({ this.open = false focusAfter && focusAfter.focus() }, - - onPanelClick(e) { - if (e.target.tagName === 'A') { - this.close() - } - } }) diff --git a/lib/plausible_web/components/generic.ex b/lib/plausible_web/components/generic.ex index e1b35f468..bc595fe60 100644 --- a/lib/plausible_web/components/generic.ex +++ b/lib/plausible_web/components/generic.ex @@ -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""" - + """ end @@ -161,36 +161,28 @@ defmodule PlausibleWeb.Components.Generic do def dropdown(assigns) do ~H""" -
+
+
""" @@ -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) %> @@ -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) %> """ diff --git a/lib/plausible_web/live/sites.ex b/lib/plausible_web/live/sites.ex index c8284788e..bf8d9d8d7 100644 --- a/lib/plausible_web/live/sites.ex +++ b/lib/plausible_web/live/sites.ex @@ -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""" -
- - - - -
+ + """ end diff --git a/test/plausible_web/live/sites_test.exs b/test/plausible_web/live/sites_test.exs index 3cdbfa69d..3c6bdc899 100644 --- a/test/plausible_web/live/sites_test.exs +++ b/test/plausible_web/live/sites_test.exs @@ -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