-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathVersionSwitcher.astro
More file actions
363 lines (308 loc) · 10.4 KB
/
VersionSwitcher.astro
File metadata and controls
363 lines (308 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
---
// Detect environment and branch
const isDev = import.meta.env.DEV;
const isMainBranch = import.meta.env.BASE_URL?.includes('/main/');
// No base prefix — versions live at the domain root (/0.1/, /0.2/, /main/)
const basePrefix = '';
// Extract version from BASE_URL (e.g., "/0.1/" -> "0.1")
const versionMatch = (import.meta.env.BASE_URL || '/').match(/\/(\d+\.\d+)\//);
const currentVersion = versionMatch ? versionMatch[1] : 'unknown';
---
<div class="version-switcher" data-base-prefix={basePrefix}>
{isDev ? (
<!-- Development: Show dev badge -->
<div class="version-badge">
<span class="version-current">dev</span>
</div>
) : isMainBranch ? (
<!-- Main branch: Show main badge -->
<div class="version-badge">
<span class="version-current">main</span>
</div>
) : (
<!-- Production: Show interactive dropdown -->
<>
<button class="version-button" id="version-button" aria-label="Switch documentation version">
<span class="version-current">v{currentVersion}</span>
<svg class="version-chevron" width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M4 6L8 10L12 6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</button>
<div class="version-dropdown" id="version-dropdown" hidden>
<div class="version-dropdown-content">
<div class="version-loading">Loading versions...</div>
</div>
</div>
</>
)}
</div>
<script>
(() => {
// Only run in production (when button exists)
const button = document.getElementById('version-button');
if (!button) return;
const dropdown = document.getElementById('version-dropdown');
const versionSwitcher = document.querySelector('.version-switcher');
let isOpen = false;
let versionsLoaded = false;
// Toggle dropdown
button?.addEventListener('click', async (e) => {
e.stopPropagation();
isOpen = !isOpen;
if (isOpen) {
button.setAttribute('aria-expanded', 'true');
dropdown?.removeAttribute('hidden');
// Load versions on first open
if (!versionsLoaded) {
await loadVersions();
versionsLoaded = true;
}
} else {
button.setAttribute('aria-expanded', 'false');
dropdown?.setAttribute('hidden', '');
}
});
// Close dropdown when clicking outside
document.addEventListener('click', (e) => {
const target = e.target as Node;
if (isOpen && !versionSwitcher?.contains(target)) {
isOpen = false;
button?.setAttribute('aria-expanded', 'false');
dropdown?.setAttribute('hidden', '');
}
});
// Prevent dropdown clicks from closing
dropdown?.addEventListener('click', (e) => {
e.stopPropagation();
});
async function loadVersions() {
const content = dropdown?.querySelector('.version-dropdown-content');
if (!content) return;
try {
// Get base prefix from data attribute (set at build time from workflow env vars)
const basePrefix = versionSwitcher?.getAttribute('data-base-prefix') || '';
const currentPath = window.location.pathname;
// Fetch versions.json from repo root
const fetchUrl = `${basePrefix}/versions.json`;
console.log('[VersionSwitcher] Fetching versions from:', fetchUrl);
const response = await fetch(fetchUrl);
if (!response.ok) {
console.error('[VersionSwitcher] Fetch failed:', response.status, response.statusText);
throw new Error('Failed to fetch versions');
}
const data = await response.json();
const versions = data.versions || [];
console.log('[VersionSwitcher] Loaded versions:', versions);
if (versions.length === 0) {
content.innerHTML = '<div class="version-error">No versions available</div>';
return;
}
// Extract current version from path (e.g., /icp-cli/0.1/ -> "0.1")
const versionPattern = new RegExp(`${basePrefix.replace(/\//g, '\\/')}\\/(\\d+\\.\\d+)\\/`);
const currentVersionMatch = currentPath.match(versionPattern);
const currentVersion = currentVersionMatch ? currentVersionMatch[1] : versions.find(v => v.latest)?.version;
console.log('[VersionSwitcher] Current version:', currentVersion, 'from path:', currentPath);
// Render version list
content.innerHTML = versions.map(v => {
const isCurrent = v.version === currentVersion;
const label = v.latest ? `${v.version} (latest)` : v.version;
const href = `${basePrefix}/${v.version}/`;
return `
<a
href="${href}"
class="version-item ${isCurrent ? 'version-current-item' : ''}"
${isCurrent ? 'aria-current="page"' : ''}
>
<span class="version-label">${label}</span>
${isCurrent ? '<span class="version-check">✓</span>' : ''}
</a>
`;
}).join('');
} catch (error) {
console.error('[VersionSwitcher] Error loading versions:', error);
content.innerHTML = '<div class="version-error">Failed to load versions</div>';
}
}
})();
</script>
<style>
/* Static component styles (scoped) */
.version-switcher {
position: relative;
display: inline-block;
}
.version-button,
.version-badge {
display: flex;
align-items: center;
gap: 0.25rem;
padding: 0.25rem 0.625rem;
background: var(--sl-color-gray-6);
color: var(--sl-color-white);
border: 1px solid var(--sl-color-gray-5);
border-radius: 0.375rem;
font-size: 0.8125rem;
font-weight: 600;
white-space: nowrap;
transition: all 0.2s ease;
}
.version-button {
cursor: pointer;
}
.version-badge {
cursor: default;
}
.version-button:hover {
background: var(--sl-color-gray-5);
border-color: var(--sl-color-gray-4);
transform: translateY(-1px);
}
.version-button:active {
transform: translateY(0);
}
.version-button[aria-expanded="true"] {
background: var(--sl-color-gray-5);
}
.version-chevron {
transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}
.version-button[aria-expanded="true"] .version-chevron {
transform: rotate(180deg);
}
.version-button:hover .version-chevron {
transform: translateY(1px);
}
.version-button[aria-expanded="true"]:hover .version-chevron {
transform: rotate(180deg) translateY(-1px);
}
.version-dropdown {
position: absolute;
top: calc(100% + 0.5rem);
left: 0;
min-width: 12rem;
background: var(--sl-color-bg-nav);
border: 1px solid var(--sl-color-gray-5);
border-radius: 0.5rem;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2), 0 2px 8px rgba(0, 0, 0, 0.1);
z-index: 1000;
animation: dropdown-appear 0.15s ease-out;
}
@keyframes dropdown-appear {
from {
opacity: 0;
transform: translateY(-8px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.version-dropdown[hidden] {
display: none;
}
.version-dropdown-content {
display: flex;
flex-direction: column;
gap: 0.375rem;
padding: 0.625rem;
}
.version-loading,
.version-error,
.version-info {
padding: 0.75rem;
text-align: center;
color: var(--sl-color-gray-3);
font-size: 0.875rem;
}
.version-error {
color: var(--sl-color-red);
}
.version-info {
color: var(--sl-color-gray-2);
font-style: italic;
}
/* Light theme overrides */
:root[data-theme='light'] .version-button,
:root[data-theme='light'] .version-badge {
background: var(--sl-color-gray-5);
border-color: var(--sl-color-gray-4);
}
:root[data-theme='light'] .version-button:hover {
background: var(--sl-color-gray-6);
border-color: var(--sl-color-gray-5);
}
/* Dynamically generated content styles (global) */
/* These must be global because they're created with innerHTML */
:global(.version-dropdown-content .version-item) {
display: flex;
align-items: center;
justify-content: space-between;
padding: 0.4rem 0.75rem;
color: var(--sl-color-white);
text-decoration: none;
border-radius: 0.25rem;
font-size: 0.875rem;
background: transparent;
border-left: 3px solid transparent;
transition: all 0.15s ease;
cursor: pointer;
position: relative;
}
:global(.version-dropdown-content .version-item:hover) {
background: rgba(255, 255, 255, 0.05);
border-left-color: var(--sl-color-gray-4);
text-decoration: none;
}
:global(.version-dropdown-content .version-item:active) {
background: rgba(255, 255, 255, 0.03);
}
/* Current version styling - subtle with left indicator */
:global(.version-dropdown-content .version-current-item) {
background: rgba(255, 255, 255, 0.08);
border-left-color: var(--sl-color-gray-2);
font-weight: 500;
}
:global(.version-dropdown-content .version-current-item:hover) {
background: rgba(255, 255, 255, 0.12);
border-left-color: var(--sl-color-gray-1);
}
/* Version label and check */
:global(.version-dropdown-content .version-label) {
flex: 1;
font-weight: 400;
letter-spacing: 0.01em;
}
:global(.version-dropdown-content .version-current-item .version-label) {
font-weight: 500;
}
:global(.version-dropdown-content .version-check) {
margin-left: 0.5rem;
font-size: 0.875rem;
color: var(--sl-color-gray-2);
display: inline-flex;
align-items: center;
justify-content: center;
}
/* Light theme for dynamic content */
:root[data-theme='light'] :global(.version-dropdown-content .version-item) {
color: var(--sl-color-gray-1);
}
:root[data-theme='light'] :global(.version-dropdown-content .version-item:hover) {
background: var(--sl-color-gray-6);
border-left-color: var(--sl-color-gray-3);
color: var(--sl-color-gray-1);
}
:root[data-theme='light'] :global(.version-dropdown-content .version-current-item) {
background: var(--sl-color-gray-5);
border-left-color: var(--sl-color-gray-2);
color: var(--sl-color-gray-1);
}
:root[data-theme='light'] :global(.version-dropdown-content .version-current-item:hover) {
background: var(--sl-color-gray-4);
border-left-color: var(--sl-color-gray-1);
color: var(--sl-color-gray-1);
}
:root[data-theme='light'] :global(.version-dropdown-content .version-check) {
color: var(--sl-color-gray-2);
}
</style>