Skip to content

Commit

Permalink
Fix scroll for targets with custom scroll parent or fixed position
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbarbara committed Nov 18, 2024
1 parent 9874150 commit 3648311
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/components/Overlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,15 @@ export default class JoyrideOverlay extends React.Component<OverlayProps, State>
}

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 });

Expand Down
8 changes: 7 additions & 1 deletion src/modules/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit 3648311

Please sign in to comment.