From eaa70202309009a0edd8dbc90294c8e8e8601dac Mon Sep 17 00:00:00 2001 From: Adrian Gruntkowski Date: Mon, 15 Jan 2024 11:39:30 +0100 Subject: [PATCH] Latency-friendly LV modal implementation and Goals form refactor (#3649) * Make modal for goal settings trigger without BE roundtrip * Turn goal form into a live component and extract modal into a wrapper * Further extract modal component and handle reset action * Make ComboBox selection callback more flexible * Add rudimentary loading state to dialog * Make form unaware of being put inside a modal * Make modal a live component and completely reset contents on open server-side * Try to avoid race condition * Fix race condition * Remove unnecessary conditional on socket assigns * Add typespecs and fix formatting * Make goals form high latency friendly * Fix tests to account for goal settings form becoming live component * Fix goal settings form live component declaration * Add documentation for modal * Fix small build test * Fix typo Co-authored-by: hq1 * Revert no longer necessary test changes from 46f65d9 * Fix and clean up modal styling * Keep focus on dialog when open and show only spinner on backdrop when loading * Adjust corners and shadows and implement open/close transitions * Lock body scroll when modal is open * Make modal top-aligned again to avoid jumping around on variable content height --------- Co-authored-by: hq1 --- assets/js/app.js | 10 + assets/js/liveview/combo-box.js | 4 + .../live/funnel_settings/form.ex | 5 + lib/plausible_web/components/generic.ex | 2 + .../live/components/combo_box.ex | 25 +- lib/plausible_web/live/components/modal.ex | 227 ++++++++++++++++++ lib/plausible_web/live/goal_settings.ex | 41 ++-- lib/plausible_web/live/goal_settings/form.ex | 176 +++++++------- lib/plausible_web/live/goal_settings/list.ex | 10 +- lib/plausible_web/live/props_settings/form.ex | 4 - .../live/goal_settings/form_test.exs | 86 +++---- .../plausible_web/live/goal_settings_test.exs | 14 +- 12 files changed, 422 insertions(+), 182 deletions(-) create mode 100644 lib/plausible_web/live/components/modal.ex diff --git a/assets/js/app.js b/assets/js/app.js index 3e344c2aa..eaad0400b 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -10,6 +10,16 @@ Alpine.data('dropdown', dropdown) Alpine.data('comboBox', comboBox) Alpine.start() +if (document.querySelectorAll('[data-modal]').length > 0) { + window.addEventListener(`phx:close-modal`, (e) => { + document + .getElementById(e.detail.id) + .dispatchEvent( + new CustomEvent('close-modal', { bubbles: true, detail: e.detail.id }) + ) + }) +} + const triggers = document.querySelectorAll('[data-dropdown-trigger]') for (const trigger of triggers) { diff --git a/assets/js/liveview/combo-box.js b/assets/js/liveview/combo-box.js index 59006b767..7849e6db6 100644 --- a/assets/js/liveview/combo-box.js +++ b/assets/js/liveview/combo-box.js @@ -5,6 +5,7 @@ export default (id) => ({ isOpen: false, id: id, focus: null, + selectionInProgress: false, setFocus(f) { this.focus = f; }, @@ -13,6 +14,9 @@ export default (id) => ({ this.setFocus(this.leastFocusableIndex()) } }, + trackSubmitValueChange() { + this.selectionInProgress = false; + }, open() { this.initFocus() this.isOpen = true diff --git a/extra/lib/plausible_web/live/funnel_settings/form.ex b/extra/lib/plausible_web/live/funnel_settings/form.ex index d2b53ebf5..e3f5248a5 100644 --- a/extra/lib/plausible_web/live/funnel_settings/form.ex +++ b/extra/lib/plausible_web/live/funnel_settings/form.ex @@ -84,6 +84,11 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do submit_name={"funnel[steps][#{step_idx}][goal_id]"} module={PlausibleWeb.Live.Components.ComboBox} suggest_fun={&PlausibleWeb.Live.Components.ComboBox.StaticSearch.suggest/2} + on_selection_made={ + fn value, by_id -> + send(self(), {:selection_made, %{submit_value: value, by: by_id}}) + end + } id={"step-#{step_idx}"} options={reject_already_selected("step-#{step_idx}", @goals, @selections_made)} /> diff --git a/lib/plausible_web/components/generic.ex b/lib/plausible_web/components/generic.ex index 4a207c1c4..8a72d0f28 100644 --- a/lib/plausible_web/components/generic.ex +++ b/lib/plausible_web/components/generic.ex @@ -250,6 +250,7 @@ defmodule PlausibleWeb.Components.Generic do end attr :class, :any, default: "" + attr :rest, :global def spinner(assigns) do ~H""" @@ -258,6 +259,7 @@ defmodule PlausibleWeb.Components.Generic do xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" + {@rest} > diff --git a/lib/plausible_web/live/components/combo_box.ex b/lib/plausible_web/live/components/combo_box.ex index e1e238bac..4af94347f 100644 --- a/lib/plausible_web/live/components/combo_box.ex +++ b/lib/plausible_web/live/components/combo_box.ex @@ -36,7 +36,7 @@ defmodule PlausibleWeb.Live.Components.ComboBox do ComboBox own test suite, so there is no need for additional verification. """ - use Phoenix.LiveComponent + use Phoenix.LiveComponent, global_prefixes: ~w(x-) alias Phoenix.LiveView.JS @default_suggestions_limit 15 @@ -69,6 +69,7 @@ defmodule PlausibleWeb.Live.Components.ComboBox do attr(:creatable, :boolean, default: false) attr(:errors, :list, default: []) attr(:async, :boolean, default: Mix.env() != :test) + attr(:on_selection_made, :any) def render(assigns) do assigns = @@ -103,15 +104,22 @@ defmodule PlausibleWeb.Live.Components.ComboBox do phx-target={@myself} phx-debounce={200} value={@display_value} - class="border-none py-1 px-1 p-0 w-full inline-block rounded-md focus:outline-none focus:ring-0 text-sm" + class="[&.phx-change-loading+svg.spinner]:block border-none py-1 px-1 p-0 w-full inline-block rounded-md focus:outline-none focus:ring-0 text-sm" style="background-color: inherit;" required={@required} /> +