Skip to content

Commit 1e706a7

Browse files
authored
feat: scroll api docs sidebar automatically into current viewport (#1288)
1 parent ae98bbf commit 1e706a7

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

apify-docs-theme/static/js/custom.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,45 @@
3232
// load();
3333
// }
3434
// }, 500);
35+
36+
let lastKnownScrollHash = '';
37+
38+
// handles automatic scrolling of the API reference sidebar (redoc)
39+
function scrollSidebarItemIntoView() {
40+
const hash = window.location.hash.substring(1);
41+
42+
if (hash !== lastKnownScrollHash) {
43+
const $li = document.querySelector(`li[data-item-id="${hash}"]`);
44+
45+
if (!$li) {
46+
return;
47+
}
48+
49+
// not visible, click on the parent <li> first
50+
if (!$li.offsetParent) {
51+
$li.parentElement?.parentElement?.click();
52+
}
53+
54+
$li.scrollIntoView({
55+
// smooth would be nice, but it's not working in some case
56+
// behavior: 'smooth',
57+
block: 'nearest',
58+
inline: 'center',
59+
});
60+
lastKnownScrollHash = hash;
61+
}
62+
}
63+
64+
let ticking = false;
65+
66+
document.addEventListener('scroll', () => {
67+
if (!ticking) {
68+
// throttling based on current frame rate
69+
window.requestAnimationFrame(() => {
70+
scrollSidebarItemIntoView();
71+
ticking = false;
72+
});
73+
74+
ticking = true;
75+
}
76+
});

0 commit comments

Comments
 (0)