Keep TOC scroll position when expanding a group (#4678)

This commit is contained in:
Peter White
2026-10-13 09:25:16 +02:00
committed by GitHub
parent 8bc381d852
commit 9d62cb4bad
2 changed files with 14 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"gitbook": patch
---
Fix table of contents scrolling back to the current page when expanding another group.
@@ -100,11 +100,19 @@ export function ScrollContainer(props: ScrollContainerProps) {
}
let frame = 0;
let previousActiveItems = new Set(container.querySelectorAll(active));
// Active items can mount only after a collapsed group expands.
const observer = new MutationObserver(() => {
cancelAnimationFrame(frame);
frame = requestAnimationFrame(() => {
for (const activeItem of container.querySelectorAll(active)) {
const activeItems = new Set(container.querySelectorAll(active));
const newActiveItems = [...activeItems].filter(
(item) => !previousActiveItems.has(item)
);
previousActiveItems = activeItems;
// Only follow newly active items so unrelated changes (e.g. expanding another
// group) don't pull the user back to the current page.
for (const activeItem of newActiveItems) {
if (!isElementFullyVisibleInContainer(activeItem, container)) {
scrollToElementInContainer(activeItem, container, 'smooth');
return;