diff --git a/app/javascript/helpers/orientation_helpers.js b/app/javascript/helpers/orientation_helpers.js index 4e3f1a3a5..b79d66360 100644 --- a/app/javascript/helpers/orientation_helpers.js +++ b/app/javascript/helpers/orientation_helpers.js @@ -1,15 +1,18 @@ const EDGE_THRESHOLD = 16 export function orient(el, orient = true) { - const directions = [ - ["orient-left", spaceOnRight], - ["orient-right", spaceOnLeft], - ["orient-top", spaceOnBottom] - ]; + el.classList.remove("orient-left", "orient-right") - directions.forEach(([className, fn]) => - el.classList[orient && fn(el) < EDGE_THRESHOLD ? "add" : "remove"](className) - ); + if (!orient) return + + const rightSpace = spaceOnRight(el) + const leftSpace = spaceOnLeft(el) + + if (rightSpace < EDGE_THRESHOLD && rightSpace < leftSpace) { + el.classList.add("orient-left") + } else if (leftSpace < EDGE_THRESHOLD && leftSpace < rightSpace) { + el.classList.add("orient-right") + } } function spaceOnLeft(el) { @@ -19,7 +22,3 @@ function spaceOnLeft(el) { function spaceOnRight(el) { return window.innerWidth - el.getBoundingClientRect().right } - -function spaceOnBottom(el) { - return window.innerHeight - el.getBoundingClientRect().bottom -}