From bb53258286288d907fc66c7351c3f099e0f08910 Mon Sep 17 00:00:00 2001 From: Andy Smith Date: Mon, 18 Aug 2025 11:08:17 -0500 Subject: [PATCH] Remove orientation on dialog close --- app/javascript/controllers/dialog_controller.js | 1 + app/javascript/helpers/orientation_helpers.js | 14 ++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app/javascript/controllers/dialog_controller.js b/app/javascript/controllers/dialog_controller.js index 96483b72c..9341f4c25 100644 --- a/app/javascript/controllers/dialog_controller.js +++ b/app/javascript/controllers/dialog_controller.js @@ -37,6 +37,7 @@ export default class extends Controller { this.dialogTarget.close() this.dialogTarget.setAttribute('aria-hidden', 'true') this.dialogTarget.blur() + orient(this.dialogTarget, false) } closeOnClickOutside({ target }) { diff --git a/app/javascript/helpers/orientation_helpers.js b/app/javascript/helpers/orientation_helpers.js index 6d8e075ec..4e3f1a3a5 100644 --- a/app/javascript/helpers/orientation_helpers.js +++ b/app/javascript/helpers/orientation_helpers.js @@ -1,9 +1,15 @@ const EDGE_THRESHOLD = 16 -export function orient(el) { - el.classList.toggle("orient-left", spaceOnRight(el) < EDGE_THRESHOLD) - el.classList.toggle("orient-right", spaceOnLeft(el) < EDGE_THRESHOLD) - el.classList.toggle("orient-top", spaceOnBottom(el) < EDGE_THRESHOLD) +export function orient(el, orient = true) { + const directions = [ + ["orient-left", spaceOnRight], + ["orient-right", spaceOnLeft], + ["orient-top", spaceOnBottom] + ]; + + directions.forEach(([className, fn]) => + el.classList[orient && fn(el) < EDGE_THRESHOLD ? "add" : "remove"](className) + ); } function spaceOnLeft(el) {