From af943d2ab71527ec0c244c8032fb1094b6522ce3 Mon Sep 17 00:00:00 2001 From: Ray Ohmori Date: Tue, 5 Oct 2021 15:47:17 -0400 Subject: [PATCH 1/3] store zero element as a map of elements for each separate body --- src/js/utils/bounds.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/js/utils/bounds.js b/src/js/utils/bounds.js index ec305427..3429dbc2 100644 --- a/src/js/utils/bounds.js +++ b/src/js/utils/bounds.js @@ -3,7 +3,7 @@ import { extend, uniqueId } from './general'; import { isUndefined } from './type-check'; const zeroPosCache = {}; -let zeroElement = null; +const zeroElements = new Map(); export function getBounds(body, el) { let doc; @@ -143,10 +143,11 @@ export function getVisibleBounds(body, target) { } export function removeUtilElements(body) { - if (zeroElement) { - body.removeChild(zeroElement); - } - zeroElement = null; + if (zeroElements.has(body)) { + body.removeChild(zeroElements.get(body)); + } + + zeroElements.delete(body); } /** @@ -187,7 +188,7 @@ function _getOrigin(body) { // jitter as the user scrolls that messes with our ability to detect if two positions // are equivilant or not. We place an element at the top left of the page that will // get the same jitter, so we can cancel the two out. - let node = zeroElement; + let node = zeroElements.get(body); if (!node || !body.contains(node)) { node = document.createElement('div'); node.setAttribute('data-tether-id', uniqueId()); @@ -199,7 +200,7 @@ function _getOrigin(body) { body.appendChild(node); - zeroElement = node; + zeroElements.set(body, node); } const id = node.getAttribute('data-tether-id'); From 2fee4c8dcc35825809efc071a44a702f7efd2e06 Mon Sep 17 00:00:00 2001 From: Ray Ohmori Date: Tue, 5 Oct 2021 16:03:35 -0400 Subject: [PATCH 2/3] fix spacing --- src/js/utils/bounds.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/js/utils/bounds.js b/src/js/utils/bounds.js index 3429dbc2..e4722a85 100644 --- a/src/js/utils/bounds.js +++ b/src/js/utils/bounds.js @@ -143,11 +143,11 @@ export function getVisibleBounds(body, target) { } export function removeUtilElements(body) { - if (zeroElements.has(body)) { - body.removeChild(zeroElements.get(body)); - } + if (zeroElements.has(body)) { + body.removeChild(zeroElements.get(body)); + } - zeroElements.delete(body); + zeroElements.delete(body); } /** From ca2e9054cb158e45752ec32d83fc15d55039093d Mon Sep 17 00:00:00 2001 From: Ray Ohmori Date: Tue, 5 Oct 2021 16:54:01 -0400 Subject: [PATCH 3/3] add a unit test for calculating bounds --- test/unit/utils/bounds.spec.js | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 test/unit/utils/bounds.spec.js diff --git a/test/unit/utils/bounds.spec.js b/test/unit/utils/bounds.spec.js new file mode 100644 index 00000000..24974753 --- /dev/null +++ b/test/unit/utils/bounds.spec.js @@ -0,0 +1,37 @@ +import { getBounds } from '../../../src/js/utils/bounds'; + +describe('Utils - bounds', () => { + describe('get bounds', () => { + let bodyElement; + + beforeEach(() => { + bodyElement = document.createElement('div'); + document.body.appendChild(bodyElement); + }); + + it('should get body offset', () => { + let bounds; + const element = document.createElement('div'); + element.style.position = 'absolute'; + element.style.top = '0'; + element.style.left = '0'; + bodyElement.appendChild(element); + bounds = getBounds(bodyElement, element); + + expect(bounds.top).toEqual(0); + expect(bounds.left).toEqual(0); + + bodyElement.style.position = 'absolute'; + bodyElement.style.top = '30px'; + bodyElement.style.left = '30px'; + bounds = getBounds(bodyElement, element); + expect(bounds.top, 'correct top bounds with body offset').toEqual(0); + expect(bounds.left, 'correct left bounds with body offset').toEqual(0); + }); + + afterEach(() => { + document.body.removeChild(bodyElement); + bodyElement = null; + }); + }); +});