Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(ds): keep DS::Menu/Popover panels anchored across Turbo morphs (#2812)

Guillem Arias Fauste07a341325095
diff --git a/app/components/DS/menu.html.erb b/app/components/DS/menu.html.erb
index d4297b901e..6d9114cf08 100644
--- a/app/components/DS/menu.html.erb
+++ b/app/components/DS/menu.html.erb
@@ -5,7 +5,7 @@
<%= button %>
<% end %>
- <div id="<%= menu_id %>" data-DS--menu-target="content" class="px-2 lg:px-0 max-w-full hidden z-50" role="menu">
+ <div id="<%= menu_id %>" data-DS--menu-target="content" class="fixed px-2 lg:px-0 max-w-full hidden z-50" role="menu">
<%= tag.div class: "mx-auto min-w-[200px] shadow-border-lg bg-container rounded-lg", style: ("max-width: #{max_width}" if max_width) do %>
<%= tag.div class: class_names("py-1" => !no_padding, "overflow-y-auto" => max_height), style: ("max-height: #{max_height}" if max_height) do %>
<% items.each do |item| %>
diff --git a/app/components/DS/menu_controller.js b/app/components/DS/menu_controller.js
index fcf50e5939..5615c6e5b8 100644
--- a/app/components/DS/menu_controller.js
+++ b/app/components/DS/menu_controller.js
@@ -24,12 +24,21 @@ export default class extends Controller {
};
connect() {
- this.show = this.showValue;
this.boundUpdate = this.update.bind(this);
this.addEventListeners();
this.startAutoUpdate();
}
+ // Derived from the content element's own class rather than tracked as
+ // separate state. A Turbo morph (e.g. the same-page refresh after this
+ // menu's own "disable account" action) re-renders the content element
+ // closed without going through toggle()/close(), which would otherwise
+ // leave a plain instance property out of sync with the DOM — swallowing
+ // the next click because toggle() would think it still needs to close.
+ get show() {
+ return !this.contentTarget.classList.contains("hidden");
+ }
+
disconnect() {
this.removeEventListeners();
this.stopAutoUpdate();
@@ -103,17 +112,16 @@ export default class extends Controller {
};
toggle = () => {
- this.show = !this.show;
- this.contentTarget.classList.toggle("hidden", !this.show);
- this.buttonTarget.setAttribute("aria-expanded", this.show.toString());
- if (this.show) {
+ const nextShow = !this.show;
+ this.contentTarget.classList.toggle("hidden", !nextShow);
+ this.buttonTarget.setAttribute("aria-expanded", nextShow.toString());
+ if (nextShow) {
this.update();
this.#focusFirstMenuItem();
}
};
close() {
- this.show = false;
this.contentTarget.classList.add("hidden");
this.buttonTarget.setAttribute("aria-expanded", "false");
}
diff --git a/app/components/DS/popover.html.erb b/app/components/DS/popover.html.erb
index 611dd33fe4..a2030628a6 100644
--- a/app/components/DS/popover.html.erb
+++ b/app/components/DS/popover.html.erb
@@ -20,7 +20,7 @@
</button>
<% end %>
- <div id="<%= panel_id %>" data-DS--popover-target="content" class="px-2 lg:px-0 max-w-full hidden z-50">
+ <div id="<%= panel_id %>" data-DS--popover-target="content" class="fixed px-2 lg:px-0 max-w-full hidden z-50">
<%= tag.div class: "mx-auto min-w-[200px] shadow-border-lg bg-container rounded-lg", style: ("max-width: #{max_width}" if max_width) do %>
<%= header %>
diff --git a/app/components/DS/popover_controller.js b/app/components/DS/popover_controller.js
index 2040a04e2f..18a96c03ed 100644
--- a/app/components/DS/popover_controller.js
+++ b/app/components/DS/popover_controller.js
@@ -25,12 +25,21 @@ export default class extends Controller {
};
connect() {
- this.show = this.showValue;
this.boundUpdate = this.update.bind(this);
this.addEventListeners();
this.startAutoUpdate();
}
+ // Derived from the content element's own class rather than tracked as
+ // separate state. A Turbo morph (e.g. a same-page refresh while this
+ // popover is mounted) re-renders the content element closed without
+ // going through toggle()/close(), which would otherwise leave a plain
+ // instance property out of sync with the DOM — swallowing the next
+ // click because toggle() would think it still needs to close.
+ get show() {
+ return !this.contentTarget.classList.contains("hidden");
+ }
+
disconnect() {
this.removeEventListeners();
this.stopAutoUpdate();
@@ -67,17 +76,16 @@ export default class extends Controller {
};
toggle = () => {
- this.show = !this.show;
- this.contentTarget.classList.toggle("hidden", !this.show);
- this.buttonTarget.setAttribute("aria-expanded", this.show.toString());
- if (this.show) {
+ const nextShow = !this.show;
+ this.contentTarget.classList.toggle("hidden", !nextShow);
+ this.buttonTarget.setAttribute("aria-expanded", nextShow.toString());
+ if (nextShow) {
this.update();
this.focusFirstElement();
}
};
close() {
- this.show = false;
this.contentTarget.classList.add("hidden");
this.buttonTarget.setAttribute("aria-expanded", "false");
}
diff --git a/test/system/ds_overlay_morph_test.rb b/test/system/ds_overlay_morph_test.rb
new file mode 100644
index 0000000000..4dcfbd04a1
--- /dev/null
+++ b/test/system/ds_overlay_morph_test.rb
@@ -0,0 +1,65 @@
+require "application_system_test_case"
+
+# Regression coverage for the bug fixed alongside DS::Menu / DS::Popover's
+# `fixed`-in-static-class change: a Turbo morph re-renders an open panel's
+# content element back to its server-rendered (closed) state without going
+# through the controller's own open/close methods. `show` used to be a plain
+# instance property that a morph couldn't touch, so the next click toggled
+# off an already-closed panel — swallowing the click. Deriving `show` from
+# the content element's own `hidden` class (rather than tracked state) means
+# it can't desync from what the DOM actually shows.
+class DsOverlayMorphTest < ApplicationSystemTestCase
+ setup do
+ sign_in @user = users(:family_admin)
+ end
+
+ test "DS::Menu reopens correctly after a morph resets it to hidden" do
+ visit tags_url
+
+ trigger = find("[aria-haspopup='menu']", match: :first)
+ content_id = trigger["aria-controls"]
+
+ trigger.click
+ assert_selector "##{content_id}", visible: true
+ assert_equal "true", trigger["aria-expanded"]
+
+ simulate_morph_reset(content_id)
+
+ trigger.click
+ assert_selector "##{content_id}", visible: true
+ assert_equal "true", trigger["aria-expanded"]
+ end
+
+ test "DS::Popover reopens correctly after a morph resets it to hidden" do
+ visit root_url
+
+ within_testid "user-menu" do
+ trigger = find("[aria-haspopup='dialog']")
+ content_id = trigger["aria-controls"]
+
+ trigger.click
+ assert_selector "##{content_id}", visible: true
+ assert_equal "true", trigger["aria-expanded"]
+
+ simulate_morph_reset(content_id)
+
+ trigger.click
+ assert_selector "##{content_id}", visible: true
+ assert_equal "true", trigger["aria-expanded"]
+ end
+ end
+
+ private
+ # Mimics what idiomorph does to an open panel on a same-page Turbo morph:
+ # the content element's class list is reconciled back to the always-hidden
+ # server-rendered markup, and any inline style the controller applied
+ # (position coordinates) is stripped — all without calling the Stimulus
+ # controller's toggle()/close().
+ def simulate_morph_reset(content_id)
+ page.execute_script(<<~JS, content_id)
+ const el = document.getElementById(arguments[0]);
+ el.classList.add("hidden");
+ el.removeAttribute("style");
+ JS
+ end
+end