The personal finance app for everyone (by everyone)
Ruby
fix(ui): don't close dialog when a text selection is released outside it (#4722)
alexcastgonzd6794efdf50f
diff --git a/app/components/DS/dialog.rb b/app/components/DS/dialog.rbindex ceb81d6dcf..ea98b0cf3e 100644--- a/app/components/DS/dialog.rb+++ b/app/components/DS/dialog.rb@@ -139,7 +139,7 @@ class DS::Dialog < DesignSystemComponentdata[:DS__dialog_auto_open_value] = auto_opendata[:DS__dialog_reload_on_close_value] = reload_on_closedata[:DS__dialog_disable_click_outside_value] = disable_click_outside- data[:action] = [ "click->DS--dialog#clickOutside", data[:action] ].compact.join(" ")+ data[:action] = [ "pointerdown->DS--dialog#trackPress pointercancel->DS--dialog#clearPress click->DS--dialog#clickOutside", data[:action] ].compact.join(" ")data[:hotkey] = "esc:DS--dialog#close"# Fetched into a frame over the page, the dialog is left out of Turbo's# cached copy of that page, or Back brings it back: stray if it was stilldiff --git a/app/components/DS/dialog_controller.js b/app/components/DS/dialog_controller.jsindex d16aa71425..625d8d5d1c 100644--- a/app/components/DS/dialog_controller.js+++ b/app/components/DS/dialog_controller.js@@ -1,4 +1,5 @@import { Controller } from "@hotwired/stimulus";+import { shouldCloseOnClick } from "utils/dialog_click_outside";const FOCUSABLE_SELECTOR = ["a[href]",@@ -28,6 +29,7 @@ export default class extends Controller {this._frame = this.element.closest("turbo-frame");this._onKeydown = this.#onKeydown.bind(this);this._onClose = this.#onClose.bind(this);+ this._pressTarget = null;this.element.addEventListener("keydown", this._onKeydown);this.element.addEventListener("close", this._onClose);@@ -59,10 +61,24 @@ export default class extends Controller {}}+ // Where the gesture that may end in a click started. Only the primary+ // pointer counts, so a second finger cannot overwrite the first one's origin.+ trackPress(e) {+ if (e.isPrimary === false) return;+ this._pressTarget = e.target;+ }++ clearPress() {+ this._pressTarget = null;+ }+// If the user clicks anywhere outside of the visible content, close the dialogclickOutside(e) {if (this.disableClickOutsideValue) return;- if (!this.contentTarget.contains(e.target)) {+ // A click with no detail comes from the keyboard, not from the press kept here.+ const pressTarget = e.detail === 0 ? null : this._pressTarget;+ this._pressTarget = null;+ if (shouldCloseOnClick(pressTarget, e.target, this.contentTarget)) {this.close();}}diff --git a/app/javascript/utils/dialog_click_outside.mjs b/app/javascript/utils/dialog_click_outside.mjsnew file mode 100644index 0000000000..df8521299f--- /dev/null+++ b/app/javascript/utils/dialog_click_outside.mjs@@ -0,0 +1,8 @@+// A click closes a dialog only when it landed outside the content and the press+// that produced it did not start inside the content. Selecting text inside the+// dialog and releasing the mouse over the backdrop fires a click on the backdrop,+// which must not be treated as a click outside.+export function shouldCloseOnClick(pressTarget, clickTarget, content) {+ if (pressTarget && content.contains(pressTarget)) return false;+ return !content.contains(clickTarget);+}diff --git a/config/importmap.rb b/config/importmap.rbindex e527776be1..8216921bdc 100644--- a/config/importmap.rb+++ b/config/importmap.rb@@ -62,5 +62,6 @@ pin "utils/sankey_preview_analytics", to: "utils/sankey_preview_analytics.mjs"pin "utils/sankey_comparison", to: "utils/sankey_comparison.mjs"pin "utils/sankey_labels", to: "utils/sankey_labels.mjs"+pin "utils/dialog_click_outside", to: "utils/dialog_click_outside.mjs"pin "utils/sankey_align", to: "utils/sankey_align.mjs"pin "utils/sankey_subcategories", to: "utils/sankey_subcategories.mjs"diff --git a/test/javascript/utils/dialog_click_outside_test.mjs b/test/javascript/utils/dialog_click_outside_test.mjsnew file mode 100644index 0000000000..8224b15757--- /dev/null+++ b/test/javascript/utils/dialog_click_outside_test.mjs@@ -0,0 +1,24 @@+import assert from "node:assert/strict";+import { test } from "node:test";+import { shouldCloseOnClick } from "../../../app/javascript/utils/dialog_click_outside.mjs";++// Minimal stand-in for the dialog's content element.+const content = (...inside) => ({ contains: (node) => inside.includes(node) });+const inside = {};+const outside = {};++test("closes when the press and the click both happen outside the content", () => {+ assert.equal(shouldCloseOnClick(outside, outside, content(inside)), true);+});++test("does not close when both happen inside the content", () => {+ assert.equal(shouldCloseOnClick(inside, inside, content(inside)), false);+});++test("does not close when a text selection starts inside and is released outside", () => {+ assert.equal(shouldCloseOnClick(inside, outside, content(inside)), false);+});++test("closes when there was no tracked press (e.g. keyboard-triggered click)", () => {+ assert.equal(shouldCloseOnClick(null, outside, content(inside)), true);+});