Skip to content

Commit

Permalink
Reset scroll with scrollTop changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Feverqwe committed Feb 22, 2024
1 parent da0d14c commit 23b194e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/runtime/TabsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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,
);
}

Expand Down
4 changes: 4 additions & 0 deletions src/runtime/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export const getClosestScrollableParent = (element: HTMLElement): HTMLElement |
export interface ElementOffset {
top: number;
left: number;
scrollTop: number;
scrollLeft: number;
}

export const getOffsetByScrollableParent = (
Expand All @@ -30,5 +32,7 @@ export const getOffsetByScrollableParent = (
return {
top: elementBounds.top - scrollableParentBounds.top,
left: elementBounds.left - scrollableParentBounds.left,
scrollTop: scrollableParent.scrollTop,
scrollLeft: scrollableParent.scrollLeft,
};
};

0 comments on commit 23b194e

Please sign in to comment.