From d8f5c4951487b45aa07dd3a993aeb967a8013faa Mon Sep 17 00:00:00 2001 From: Stephan Osthold <1461136+StephanOsthold@users.noreply.github.com> Date: Thu, 18 Aug 2022 11:28:30 +0200 Subject: [PATCH 1/2] fix: not traversing dom when getting container size --- src/utils/get-parent-container-size.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/get-parent-container-size.js b/src/utils/get-parent-container-size.js index 6e75a80..2132309 100644 --- a/src/utils/get-parent-container-size.js +++ b/src/utils/get-parent-container-size.js @@ -5,9 +5,9 @@ export const getParentContainerSize = (img, type = 'width') => { do { parentNode = parentNode && parentNode.parentNode; - size = typeof parentNode.getBoundingClientRect === 'function' ? parentNode.getBoundingClientRect()[type] : window.innerWidth; + size = parentNode && typeof parentNode.getBoundingClientRect === 'function' ? parentNode.getBoundingClientRect()[type] : window.innerWidth; maxCount = maxCount + 1; - } while (parentNode && !size && maxCount > 5) + } while (parentNode && !size && maxCount <= 5) const leftPadding = (size && parentNode && parentNode.nodeType === 1) ? parseInt(window.getComputedStyle(parentNode).paddingLeft) : 0; const rightPadding = (size && parentNode && parentNode.nodeType === 1) ? parseInt(window.getComputedStyle(parentNode).paddingRight) : 0; From f559f0080ef72b20ce3ebc9f262217f650dab064 Mon Sep 17 00:00:00 2001 From: Stephan Osthold <1461136+StephanOsthold@users.noreply.github.com> Date: Thu, 18 Aug 2022 11:32:51 +0200 Subject: [PATCH 2/2] fix: getParentContainerSize mixing width and height When failing to determine the size, the fallback only made sense for the width. Also the left- / right-paddings are not to be subtracted from the height. It may be advisable to subtract the top- / bottom-paddings, however this is out of the scope of this fix. --- src/utils/get-parent-container-size.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/utils/get-parent-container-size.js b/src/utils/get-parent-container-size.js index 2132309..bb32177 100644 --- a/src/utils/get-parent-container-size.js +++ b/src/utils/get-parent-container-size.js @@ -5,16 +5,21 @@ export const getParentContainerSize = (img, type = 'width') => { do { parentNode = parentNode && parentNode.parentNode; - size = parentNode && typeof parentNode.getBoundingClientRect === 'function' ? parentNode.getBoundingClientRect()[type] : window.innerWidth; + size = parentNode && typeof parentNode.getBoundingClientRect === 'function' ? parentNode.getBoundingClientRect()[type] : 0; maxCount = maxCount + 1; } while (parentNode && !size && maxCount <= 5) - const leftPadding = (size && parentNode && parentNode.nodeType === 1) ? parseInt(window.getComputedStyle(parentNode).paddingLeft) : 0; - const rightPadding = (size && parentNode && parentNode.nodeType === 1) ? parseInt(window.getComputedStyle(parentNode).paddingRight) : 0; + if (type === 'width') { + if (!size) { + size = window.innerWidth; + } else if (parentNode.nodeType === 1) { + const computedStyle = window.getComputedStyle(parentNode); + const leftPadding = parseInt(computedStyle.paddingLeft) || 0; + const rightPadding = parseInt(computedStyle.paddingRight) || 0; - if (!size) { - size = window.innerWidth; + size -= leftPadding + rightPadding; + } } - return size + (size ? (-leftPadding - rightPadding) : 0); + return size; };