|
| 1 | +const SIDEBAR_SCROLL_TOP_KEY = "sidebarScrollTop"; |
| 2 | + |
| 3 | +export function ensureActiveTabInView() { |
| 4 | + const sidebar = document.querySelector("nav[aria-label='Docs sidebar']"); |
| 5 | + |
| 6 | + if (sidebar) { |
| 7 | + // Hide the scrollbar for better UX |
| 8 | + // @ts-ignore |
| 9 | + sidebar.style = ` |
| 10 | + -ms-overflow-style: none; /* IE and Edge */ |
| 11 | + scrollbar-width: none; /* Firefox */ |
| 12 | + &::-webkit-scrollbar { |
| 13 | + display: none; /* Chrome, Safari and Opera */ |
| 14 | + } |
| 15 | + `; |
| 16 | + const lastScrollTop = sessionStorage.getItem(SIDEBAR_SCROLL_TOP_KEY); |
| 17 | + if (lastScrollTop !== null) { |
| 18 | + sidebar.scrollTop = parseInt(lastScrollTop, 10); |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + const activeItems = document.querySelectorAll(".menu__link--active"); |
| 23 | + |
| 24 | + if (!activeItems || activeItems.length === 0) { |
| 25 | + if (sidebar) { |
| 26 | + sessionStorage.setItem( |
| 27 | + SIDEBAR_SCROLL_TOP_KEY, |
| 28 | + sidebar.scrollTop.toString() |
| 29 | + ); |
| 30 | + } |
| 31 | + return; |
| 32 | + } |
| 33 | + const item = activeItems[activeItems.length - 1]; |
| 34 | + |
| 35 | + let isItemVisibleAfterRestore = false; |
| 36 | + if (sidebar) { |
| 37 | + const itemRect = item.getBoundingClientRect(); |
| 38 | + const sidebarRect = sidebar.getBoundingClientRect(); |
| 39 | + // Check if item is visible within the sidebar's viewport |
| 40 | + isItemVisibleAfterRestore = |
| 41 | + itemRect.top >= sidebarRect.top && itemRect.bottom <= sidebarRect.bottom; |
| 42 | + } else { |
| 43 | + const bounding = item.getBoundingClientRect(); |
| 44 | + isItemVisibleAfterRestore = |
| 45 | + bounding.top >= 0 && |
| 46 | + bounding.bottom <= |
| 47 | + (window.innerHeight || document.documentElement.clientHeight); |
| 48 | + } |
| 49 | + |
| 50 | + // Not visible after restoring scroll, so scroll into view. |
| 51 | + if (!isItemVisibleAfterRestore) { |
| 52 | + item.scrollIntoView({ block: "nearest", inline: "nearest" }); |
| 53 | + document.body.scrollTop = document.documentElement.scrollTop = 0; |
| 54 | + } |
| 55 | + |
| 56 | + if (sidebar) { |
| 57 | + sessionStorage.setItem( |
| 58 | + SIDEBAR_SCROLL_TOP_KEY, |
| 59 | + sidebar.scrollTop.toString() |
| 60 | + ); |
| 61 | + // Restore the scrollbar style |
| 62 | + // @ts-ignore |
| 63 | + sidebar.style = undefined; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +export function observeDocumentChanges() { |
| 68 | + const observer = new MutationObserver((mutationsList) => { |
| 69 | + for (const mutation of mutationsList) { |
| 70 | + if ( |
| 71 | + mutation.type === "childList" || |
| 72 | + (mutation.type === "attributes" && mutation.attributeName === "class") |
| 73 | + ) { |
| 74 | + ensureActiveTabInView(); |
| 75 | + } |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + const targetNode = document.getElementById("__docusaurus"); |
| 80 | + if (targetNode) { |
| 81 | + const config = { attributes: true, childList: true, subtree: true }; |
| 82 | + observer.observe(targetNode, config); |
| 83 | + } |
| 84 | + return () => { |
| 85 | + observer.disconnect(); |
| 86 | + }; |
| 87 | +} |
| 88 | + |
| 89 | +export function subscribeSidebarScroll() { |
| 90 | + const sidebar = document.querySelector("nav[aria-label='Docs sidebar']"); |
| 91 | + if (!sidebar) { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + const handleScroll = () => { |
| 96 | + sessionStorage.setItem( |
| 97 | + SIDEBAR_SCROLL_TOP_KEY, |
| 98 | + sidebar.scrollTop.toString() |
| 99 | + ); |
| 100 | + }; |
| 101 | + |
| 102 | + sidebar.addEventListener("scroll", handleScroll); |
| 103 | + return () => { |
| 104 | + sidebar.removeEventListener("scroll", handleScroll); |
| 105 | + }; |
| 106 | +} |
0 commit comments