|
| 1 | +import ExecutionEnvironment from "@docusaurus/ExecutionEnvironment"; |
| 2 | + |
| 3 | +let floatingHeader = null; |
| 4 | +let cleanup = null; |
| 5 | + |
| 6 | +function createFloatingHeader(originalHeader) { |
| 7 | + if (floatingHeader) return floatingHeader; |
| 8 | + |
| 9 | + // Get the original table for width measurements |
| 10 | + const originalTable = originalHeader.closest("table"); |
| 11 | + |
| 12 | + // Clone the original header |
| 13 | + const headerClone = originalHeader.cloneNode(true); |
| 14 | + |
| 15 | + // Create floating container |
| 16 | + floatingHeader = document.createElement("div"); |
| 17 | + |
| 18 | + // Check if we're on a wide screen where sidebar is visible |
| 19 | + const isWideScreen = window.innerWidth >= 997; |
| 20 | + |
| 21 | + // Get the actual sidebar width from CSS variables |
| 22 | + let sidebarWidth = "0px"; |
| 23 | + if (isWideScreen) { |
| 24 | + const computedStyle = getComputedStyle(document.documentElement); |
| 25 | + const docSidebarWidth = computedStyle.getPropertyValue( |
| 26 | + "--doc-sidebar-width" |
| 27 | + ); |
| 28 | + sidebarWidth = docSidebarWidth || "300px"; // fallback to 300px |
| 29 | + } |
| 30 | + |
| 31 | + floatingHeader.style.cssText = ` |
| 32 | + position: fixed; |
| 33 | + top: 60px; |
| 34 | + left: ${sidebarWidth}; |
| 35 | + right: 0; |
| 36 | + z-index: 1000; |
| 37 | + background: var(--ifm-background-color); |
| 38 | + box-shadow: 0 2px 4px rgba(0,0,0,0.1); |
| 39 | + display: none; |
| 40 | + overflow-x: auto; |
| 41 | + padding: 0 20px; |
| 42 | + `; |
| 43 | + |
| 44 | + // Create table wrapper for the floating header |
| 45 | + const floatingTable = document.createElement("table"); |
| 46 | + floatingTable.className = "simplified-pricing-table"; |
| 47 | + floatingTable.style.cssText = ` |
| 48 | + margin: 0 auto; |
| 49 | + width: 100%; |
| 50 | + max-width: 1200px; |
| 51 | + table-layout: fixed; |
| 52 | + border-collapse: collapse; |
| 53 | + `; |
| 54 | + |
| 55 | + floatingTable.appendChild(headerClone); |
| 56 | + floatingHeader.appendChild(floatingTable); |
| 57 | + document.body.appendChild(floatingHeader); |
| 58 | + |
| 59 | + // Copy column widths from original table to floating table |
| 60 | + const originalCells = originalHeader.querySelectorAll("th"); |
| 61 | + const floatingCells = headerClone.querySelectorAll("th"); |
| 62 | + |
| 63 | + // Get the computed width of the original table |
| 64 | + const originalTableRect = originalTable.getBoundingClientRect(); |
| 65 | + floatingTable.style.width = originalTableRect.width + "px"; |
| 66 | + |
| 67 | + originalCells.forEach((originalCell, index) => { |
| 68 | + if (floatingCells[index]) { |
| 69 | + const cellRect = originalCell.getBoundingClientRect(); |
| 70 | + floatingCells[index].style.width = cellRect.width + "px"; |
| 71 | + floatingCells[index].style.minWidth = cellRect.width + "px"; |
| 72 | + floatingCells[index].style.maxWidth = cellRect.width + "px"; |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + return floatingHeader; |
| 77 | +} |
| 78 | + |
| 79 | +function handleScroll(originalHeader) { |
| 80 | + if (!originalHeader) return; |
| 81 | + |
| 82 | + const headerRect = originalHeader.getBoundingClientRect(); |
| 83 | + const isHeaderVisible = headerRect.bottom > 60; // Account for navbar |
| 84 | + |
| 85 | + if (!isHeaderVisible) { |
| 86 | + // Original header is out of view, show floating header |
| 87 | + const floating = createFloatingHeader(originalHeader); |
| 88 | + floating.style.display = "block"; |
| 89 | + } else { |
| 90 | + // Original header is visible, hide floating header |
| 91 | + if (floatingHeader) { |
| 92 | + floatingHeader.style.display = "none"; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +function initFloatingHeader() { |
| 98 | + // Clean up any existing setup |
| 99 | + if (cleanup) cleanup(); |
| 100 | + |
| 101 | + const table = document.getElementById("pricing-table"); |
| 102 | + const originalHeader = document.getElementById("table-header"); |
| 103 | + |
| 104 | + if (!table || !originalHeader) return; |
| 105 | + |
| 106 | + const scrollHandler = () => handleScroll(originalHeader); |
| 107 | + |
| 108 | + // Use passive event listeners for better performance |
| 109 | + window.addEventListener("scroll", scrollHandler, { passive: true }); |
| 110 | + |
| 111 | + // Handle window resize to update column widths |
| 112 | + const resizeHandler = () => { |
| 113 | + if (floatingHeader) { |
| 114 | + // Remove and recreate floating header with new measurements |
| 115 | + if (floatingHeader.parentNode) { |
| 116 | + floatingHeader.parentNode.removeChild(floatingHeader); |
| 117 | + } |
| 118 | + floatingHeader = null; |
| 119 | + } |
| 120 | + }; |
| 121 | + window.addEventListener("resize", resizeHandler, { passive: true }); |
| 122 | + |
| 123 | + // Cleanup function |
| 124 | + cleanup = () => { |
| 125 | + window.removeEventListener("scroll", scrollHandler); |
| 126 | + window.removeEventListener("resize", resizeHandler); |
| 127 | + if (floatingHeader && floatingHeader.parentNode) { |
| 128 | + floatingHeader.parentNode.removeChild(floatingHeader); |
| 129 | + floatingHeader = null; |
| 130 | + } |
| 131 | + }; |
| 132 | +} |
| 133 | + |
| 134 | +if (ExecutionEnvironment.canUseDOM) { |
| 135 | + // Initialize on route changes |
| 136 | + if (typeof window !== "undefined") { |
| 137 | + // Safari might need a longer delay |
| 138 | + const isSafari = |
| 139 | + navigator.userAgent.includes("Safari") && |
| 140 | + !navigator.userAgent.includes("Chrome"); |
| 141 | + const delay = isSafari ? 300 : 100; |
| 142 | + |
| 143 | + // Initial setup |
| 144 | + setTimeout(initFloatingHeader, delay); |
| 145 | + |
| 146 | + // Setup for route changes |
| 147 | + const originalPushState = history.pushState; |
| 148 | + const originalReplaceState = history.replaceState; |
| 149 | + |
| 150 | + const onRouteChange = () => { |
| 151 | + setTimeout(initFloatingHeader, delay); |
| 152 | + }; |
| 153 | + |
| 154 | + history.pushState = function (...args) { |
| 155 | + originalPushState.apply(this, args); |
| 156 | + onRouteChange(); |
| 157 | + }; |
| 158 | + |
| 159 | + history.replaceState = function (...args) { |
| 160 | + originalReplaceState.apply(this, args); |
| 161 | + onRouteChange(); |
| 162 | + }; |
| 163 | + |
| 164 | + window.addEventListener("popstate", onRouteChange); |
| 165 | + } |
| 166 | +} |
0 commit comments