From 3648311d664498214ef9ef00752f8b1254641777 Mon Sep 17 00:00:00 2001 From: Gil Barbara Date: Thu, 7 Nov 2024 13:24:30 -0300 Subject: [PATCH] Fix scroll for targets with custom scroll parent or fixed position --- src/components/Overlay.tsx | 8 +++++++- src/modules/dom.ts | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/components/Overlay.tsx b/src/components/Overlay.tsx index 83ba24c0..52e594f4 100644 --- a/src/components/Overlay.tsx +++ b/src/components/Overlay.tsx @@ -63,9 +63,15 @@ export default class JoyrideOverlay extends React.Component } componentDidUpdate(previousProps: OverlayProps) { - const { lifecycle, spotlightClicks } = this.props; + const { disableScrollParentFix, lifecycle, spotlightClicks, target } = this.props; const { changed } = treeChanges(previousProps, this.props); + if (changed('target') || changed('disableScrollParentFix')) { + const element = getElement(target); + + this.scrollParent = getScrollParent(element ?? document.body, disableScrollParentFix, true); + } + if (changed('lifecycle', LIFECYCLE.TOOLTIP)) { this.scrollParent?.addEventListener('scroll', this.handleScroll, { passive: true }); diff --git a/src/modules/dom.ts b/src/modules/dom.ts index de9534dd..f6b1c4ad 100644 --- a/src/modules/dom.ts +++ b/src/modules/dom.ts @@ -204,10 +204,16 @@ export function getElementPosition( const elementRect = getClientRect(element); const parent = getScrollParent(element, skipFix); const hasScrollParent = hasCustomScrollParent(element, skipFix); + const isFixedTarget = hasPosition(element); let parentTop = 0; let top = elementRect?.top ?? 0; - if (parent instanceof HTMLElement) { + if (hasScrollParent && isFixedTarget) { + const offsetTop = element?.offsetTop ?? 0; + const parentScrollTop = (parent as HTMLElement)?.scrollTop ?? 0; + + top = offsetTop - parentScrollTop; + } else if (parent instanceof HTMLElement) { parentTop = parent.scrollTop; if (!hasScrollParent && !hasPosition(element)) {