From e5bef41c5497a2b79d11f719f21ec81beb7d14e1 Mon Sep 17 00:00:00 2001 From: Adrien Maston Date: Tue, 27 Jan 2026 11:21:15 +0100 Subject: [PATCH] Refactor orientation helper to express intent more clearly --- app/javascript/helpers/orientation_helpers.js | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/app/javascript/helpers/orientation_helpers.js b/app/javascript/helpers/orientation_helpers.js index c63da2fca..f823de09a 100644 --- a/app/javascript/helpers/orientation_helpers.js +++ b/app/javascript/helpers/orientation_helpers.js @@ -8,18 +8,28 @@ export function orient({ target, anchor = null, reset = false }) { const targetGeometry = geometry(target) const anchorGeometry = geometry(anchor) + const shouldOrientLeft = targetGeometry.spaceOnRight < EDGE_THRESHOLD && targetGeometry.spaceOnRight < targetGeometry.spaceOnLeft + const shouldOrientRight = targetGeometry.spaceOnLeft < EDGE_THRESHOLD && targetGeometry.spaceOnLeft < targetGeometry.spaceOnRight - if (targetGeometry.spaceOnRight < EDGE_THRESHOLD && targetGeometry.spaceOnRight < targetGeometry.spaceOnLeft) { - const offset = Math.min(0, anchorGeometry.spaceOnLeft + anchorGeometry.width - targetGeometry.width) * -1 - target.classList.add("orient-left") - target.style.setProperty("--orient-offset", `${offset}px`) - } else if (targetGeometry.spaceOnLeft < EDGE_THRESHOLD && targetGeometry.spaceOnLeft < targetGeometry.spaceOnRight) { - const offset = Math.max(0, anchorGeometry.spaceOnLeft + targetGeometry.width - window.innerWidth) * -1 - target.classList.add("orient-right") - target.style.setProperty("--orient-offset", `${offset}px`) + if (shouldOrientLeft) { + orientLeft({ el: target, targetGeometry, anchorGeometry }) + } else if (shouldOrientRight) { + orientRight({ el: target, targetGeometry, anchorGeometry }) } } +function orientLeft({ el, targetGeometry, anchorGeometry }) { + const offset = Math.min(0, anchorGeometry.spaceOnLeft + anchorGeometry.width - targetGeometry.width) * -1 + el.classList.add("orient-left") + el.style.setProperty("--orient-offset", `${offset}px`) +} + +function orientRight({ el, targetGeometry, anchorGeometry }) { + const offset = Math.max(0, anchorGeometry.spaceOnLeft + targetGeometry.width - window.innerWidth) * -1 + el.classList.add("orient-right") + el.style.setProperty("--orient-offset", `${offset}px`) +} + function geometry(el) { const rect = el.getBoundingClientRect() return {