Skip to content

Commit 771bd05

Browse files
committed
Maintain documentation scroll position when switching tabs (closes #357)
1 parent 3261be0 commit 771bd05

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/shared/renderers/DocumentationRenderer.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,24 @@ export default class DocumentationRenderer implements TabRenderer {
66

77
private stateQueue: string | null = null;
88
private loaded = false;
9+
private scrollPosition = 0;
10+
private shouldResetScroll = false;
911

1012
constructor(content: HTMLElement) {
1113
this.CONTAINER = content.getElementsByClassName("documentation-container")[0] as HTMLElement;
1214
this.IFRAME = this.CONTAINER.firstElementChild as HTMLIFrameElement;
1315
this.IFRAME.addEventListener("load", () => {
1416
this.loaded = true;
1517
});
18+
19+
// Periodic function to record when tab is hidden
20+
let periodic = () => {
21+
if (this.CONTAINER.getBoundingClientRect().height === 0) {
22+
this.shouldResetScroll = true;
23+
}
24+
window.requestAnimationFrame(periodic);
25+
};
26+
window.requestAnimationFrame(periodic);
1627
}
1728

1829
saveState(): unknown {
@@ -31,9 +42,20 @@ export default class DocumentationRenderer implements TabRenderer {
3142
}
3243

3344
render(_: unknown): void {
45+
// Navigate to initial page when loaded
3446
if (this.stateQueue !== null && this.loaded) {
3547
this.IFRAME.contentWindow!.location.hash = this.stateQueue;
3648
this.stateQueue = null;
3749
}
50+
51+
// Update scroll location
52+
if (this.loaded && this.IFRAME.contentWindow !== null) {
53+
if (this.shouldResetScroll) {
54+
this.IFRAME.contentWindow.scrollTo(0, this.scrollPosition);
55+
this.shouldResetScroll = false;
56+
} else {
57+
this.scrollPosition = this.IFRAME.contentWindow.scrollY;
58+
}
59+
}
3860
}
3961
}

0 commit comments

Comments
 (0)