Skip to content

Commit 3f943a2

Browse files
Merge pull request #2245 from redis/DOC-5824
Fix sidebar auto-scroll to correctly target scrollable container
2 parents 712f0e0 + b83dc88 commit 3f943a2

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

layouts/partials/docs-nav.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,27 @@
204204
const sidebar = document.getElementById('sidebar');
205205
const footer = document.querySelector('footer');
206206

207+
// Scroll sidebar to show active page on load
208+
const activeLink = sidebar.querySelector('a.active');
209+
if (activeLink) {
210+
// Find the scrollable UL container that contains the active link
211+
const scrollContainer = activeLink.closest('ul.overflow-y-auto');
212+
213+
if (scrollContainer) {
214+
// Get the position of the active link relative to its scrollable container
215+
const linkRect = activeLink.getBoundingClientRect();
216+
const containerRect = scrollContainer.getBoundingClientRect();
217+
218+
// Calculate how far the link is from the top of the container
219+
const relativeTop = linkRect.top - containerRect.top + scrollContainer.scrollTop;
220+
221+
// Scroll so the active link appears near the top (with some padding)
222+
const scrollPosition = relativeTop - 20; // 20px padding from top
223+
224+
scrollContainer.scrollTop = Math.max(0, scrollPosition);
225+
}
226+
}
227+
207228
window.addEventListener('scroll', function() {
208229
const scrolledHeight = window.scrollY + window.innerHeight;
209230
if (scrolledHeight >= footer.offsetTop) {

0 commit comments

Comments
 (0)