From 4bf34887dd3c68ba4d114413cfb1c06da9e67607 Mon Sep 17 00:00:00 2001 From: Anton Vikulov Date: Thu, 22 Feb 2024 15:17:02 +0500 Subject: [PATCH] Reset scroll with scrollTop changes --- src/runtime/TabsController.ts | 12 ++++++++---- src/runtime/utils.ts | 4 ++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/runtime/TabsController.ts b/src/runtime/TabsController.ts index 161f519..5233344 100644 --- a/src/runtime/TabsController.ts +++ b/src/runtime/TabsController.ts @@ -98,11 +98,11 @@ export class TabsController { const updatedTabs = this.updateHTML({group, key}); if (updatedTabs > 0) { + this.fireSelectTabEvent({group, key}, targetTab?.dataset.diplodocId); + if (previousTargetOffset) { this.resetScroll(targetTab, scrollableParent, previousTargetOffset); } - - this.fireSelectTabEvent({group, key}, targetTab?.dataset.diplodocId); } } @@ -155,9 +155,13 @@ export class TabsController { previousTargetOffset: ElementOffset, ) { const targetOffset = getOffsetByScrollableParent(target, scrollableParent); + const topDelta = targetOffset.top - previousTargetOffset.top; + const leftDelta = targetOffset.left - previousTargetOffset.left; + const scrollTopDelta = targetOffset.scrollTop - previousTargetOffset.scrollTop; + const scrollLeftDelta = targetOffset.scrollLeft - previousTargetOffset.scrollLeft; scrollableParent.scrollTo( - targetOffset.left + scrollableParent.scrollLeft - previousTargetOffset.left, - targetOffset.top + scrollableParent.scrollTop - previousTargetOffset.top, + scrollableParent.scrollLeft + leftDelta - scrollLeftDelta, + scrollableParent.scrollTop + topDelta - scrollTopDelta, ); } diff --git a/src/runtime/utils.ts b/src/runtime/utils.ts index bb190c7..8ed673e 100644 --- a/src/runtime/utils.ts +++ b/src/runtime/utils.ts @@ -19,6 +19,8 @@ export const getClosestScrollableParent = (element: HTMLElement): HTMLElement | export interface ElementOffset { top: number; left: number; + scrollTop: number; + scrollLeft: number; } export const getOffsetByScrollableParent = ( @@ -30,5 +32,7 @@ export const getOffsetByScrollableParent = ( return { top: elementBounds.top - scrollableParentBounds.top, left: elementBounds.left - scrollableParentBounds.left, + scrollTop: scrollableParent.scrollTop, + scrollLeft: scrollableParent.scrollLeft, }; };