|
| 1 | +/** |
| 2 | + * Navigation Scroll Position Fix |
| 3 | + * Preserves sidebar scroll position when clicking navigation items |
| 4 | + */ |
| 5 | + |
| 6 | +(function() { |
| 7 | + 'use strict'; |
| 8 | + |
| 9 | + const STORAGE_KEY = 'nav-scroll-position'; |
| 10 | + const NAV_SELECTORS = [ |
| 11 | + '.md-sidebar--primary .md-sidebar__scrollwrap', |
| 12 | + '.md-nav--primary', |
| 13 | + 'nav[data-md-component="sidebar"]', |
| 14 | + '.md-sidebar.md-sidebar--primary', |
| 15 | + '[data-sidebar="content"]', |
| 16 | + '[data-slot="sidebar-content"]', |
| 17 | + '[data-slot="sidebar-wrapper"] [data-slot="sidebar"] [data-slot="sidebar-content"]' |
| 18 | + ]; |
| 19 | + |
| 20 | + function getNavElement() { |
| 21 | + for (const selector of NAV_SELECTORS) { |
| 22 | + const elem = document.querySelector(selector); |
| 23 | + if (elem) { |
| 24 | + return elem; |
| 25 | + } |
| 26 | + } |
| 27 | + return null; |
| 28 | + } |
| 29 | + |
| 30 | + function saveScrollPosition() { |
| 31 | + const navElement = getNavElement(); |
| 32 | + if (navElement) { |
| 33 | + const scrollPosition = navElement.scrollTop; |
| 34 | + sessionStorage.setItem(STORAGE_KEY, scrollPosition.toString()); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + function restoreScrollPosition(immediate = false) { |
| 39 | + const navElement = getNavElement(); |
| 40 | + const savedPosition = sessionStorage.getItem(STORAGE_KEY); |
| 41 | + |
| 42 | + if (navElement && savedPosition) { |
| 43 | + const scrollPos = parseInt(savedPosition, 10); |
| 44 | + |
| 45 | + if (immediate) { |
| 46 | + // Restore immediately without delay to prevent flashing |
| 47 | + navElement.scrollTop = scrollPos; |
| 48 | + } else { |
| 49 | + // Use requestAnimationFrame for smooth restoration |
| 50 | + requestAnimationFrame(() => { |
| 51 | + navElement.scrollTop = scrollPos; |
| 52 | + }); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + let lastActiveLink = null; |
| 58 | + let lastNavigationTime = 0; |
| 59 | + |
| 60 | + function onNavClick(event) { |
| 61 | + const target = event.currentTarget; |
| 62 | + lastActiveLink = target; |
| 63 | + const now = Date.now(); |
| 64 | + lastNavigationTime = now; |
| 65 | + saveScrollPosition(); |
| 66 | + } |
| 67 | + |
| 68 | + function getNavLinks() { |
| 69 | + return document.querySelectorAll('.md-nav__link, [data-sidebar="menu-button"], [data-slot="sidebar-menu-button"] a, [data-slot="sidebar-menu-button"], [data-sidebar="menu-button"]'); |
| 70 | + } |
| 71 | + |
| 72 | + function attachScrollSaver() { |
| 73 | + const navLinks = getNavLinks(); |
| 74 | + |
| 75 | + navLinks.forEach(link => { |
| 76 | + link.removeEventListener('click', onNavClick); |
| 77 | + link.addEventListener('click', onNavClick); |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + // Initialize on page load |
| 82 | + function init(immediate = false) { |
| 83 | + // Restore scroll position |
| 84 | + restoreScrollPosition(immediate); |
| 85 | + |
| 86 | + // Attach scroll savers to navigation links |
| 87 | + attachScrollSaver(); |
| 88 | + |
| 89 | + // Also save on page unload |
| 90 | + window.removeEventListener('beforeunload', saveScrollPosition); |
| 91 | + window.addEventListener('beforeunload', saveScrollPosition); |
| 92 | + |
| 93 | + // Handle dynamic content loading (for MkDocs instant loading) |
| 94 | + const observer = new MutationObserver((mutations) => { |
| 95 | + mutations.forEach((mutation) => { |
| 96 | + if (mutation.addedNodes.length) { |
| 97 | + attachScrollSaver(); |
| 98 | + } |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + // Observe the navigation for changes |
| 103 | + const nav = document.querySelector('.md-sidebar--primary'); |
| 104 | + const navWrapper = document.querySelector('[data-slot="sidebar-wrapper"]'); |
| 105 | + if (navWrapper) { |
| 106 | + observer.observe(navWrapper, { |
| 107 | + childList: true, |
| 108 | + subtree: true |
| 109 | + }); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Restore scroll position immediately on script load to prevent flashing |
| 114 | + // This runs before the page is fully rendered |
| 115 | + (function earlyRestore() { |
| 116 | + const navElement = getNavElement(); |
| 117 | + const savedPosition = sessionStorage.getItem(STORAGE_KEY); |
| 118 | + if (navElement && savedPosition) { |
| 119 | + navElement.scrollTop = parseInt(savedPosition, 10); |
| 120 | + } |
| 121 | + })(); |
| 122 | + |
| 123 | + // Run when DOM is ready |
| 124 | + if (document.readyState === 'loading') { |
| 125 | + document.addEventListener('DOMContentLoaded', () => init(true)); |
| 126 | + } else { |
| 127 | + init(true); |
| 128 | + } |
| 129 | + |
| 130 | + // Handle instant loading in MkDocs Material theme |
| 131 | + document.addEventListener('DOMContentSwitch', () => { |
| 132 | + // Restore immediately to prevent flash |
| 133 | + restoreScrollPosition(true); |
| 134 | + // Then initialize event handlers |
| 135 | + setTimeout(() => { |
| 136 | + attachScrollSaver(); |
| 137 | + }, 50); |
| 138 | + }); |
| 139 | + |
| 140 | + document.addEventListener('navigation', () => { |
| 141 | + // Restore immediately on navigation |
| 142 | + restoreScrollPosition(true); |
| 143 | + // Then reinitialize |
| 144 | + setTimeout(() => { |
| 145 | + attachScrollSaver(); |
| 146 | + }, 50); |
| 147 | + }); |
| 148 | + |
| 149 | +})(); |
| 150 | + |
0 commit comments