-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathInstallSelector.astro
More file actions
479 lines (416 loc) · 13.9 KB
/
Copy pathInstallSelector.astro
File metadata and controls
479 lines (416 loc) · 13.9 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
---
import { codeToTokens } from 'shiki';
interface InstallOption {
label: string;
command: string;
}
interface Props {
options: InstallOption[];
defaultIndex?: number;
}
const { options, defaultIndex = 0 } = Astro.props;
/**
* Highlights a shell command using Shiki and returns inline HTML.
*/
async function highlightCommand(cmd: string): Promise<string> {
const { tokens } = await codeToTokens(cmd, {
lang: 'bash',
theme: 'github-dark',
});
// Convert tokens to inline HTML spans (single line command)
return tokens[0]
.map(token => {
const escaped = token.content
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
return `<span style="color: ${token.color}">${escaped}</span>`;
})
.join('');
}
// Pre-highlight all options server-side for client-side switching
const highlightedOptions = await Promise.all(
options.map(async (opt) => ({
label: opt.label,
command: opt.command,
highlighted: await highlightCommand(opt.command),
}))
);
const defaultOption = highlightedOptions[defaultIndex];
---
<div class="install-selector">
<div class="install-box">
<button class="dropdown-trigger" aria-expanded="false" aria-haspopup="listbox">
<span class="dropdown-label">{defaultOption.label}</span>
<svg class="chevron" xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="m6 9 6 6 6-6"/>
</svg>
</button>
<div class="command-area" style={{marginTop: 0}}>
<code class="command-text" style={{backgroundColor: 'transparent'}} data-command={defaultOption.command} set:html={defaultOption.highlighted} />
<button class="copy-btn" data-command={defaultOption.command} aria-label="Copy to clipboard">
<svg class="copy-icon" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<rect width="14" height="14" x="8" y="8" rx="2" ry="2"/>
<path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/>
</svg>
<svg class="check-icon" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M20 6 9 17l-5-5"/>
</svg>
</button>
</div>
<ul class="dropdown-menu" role="listbox" aria-label="Installation method">
{highlightedOptions.map((opt, index) => (
<li>
<button
class={`dropdown-option ${index === defaultIndex ? 'selected' : ''}`}
data-label={opt.label}
data-command={opt.command}
data-highlighted={opt.highlighted}
role="option"
aria-selected={index === defaultIndex ? 'true' : 'false'}
>
{opt.label}
</button>
</li>
))}
</ul>
</div>
</div>
<style>
.install-selector {
position: relative;
width: fit-content;
}
/* Unified container with shared border */
.install-box {
display: flex;
align-items: stretch;
position: relative;
background: rgba(255, 255, 255, 0.05);
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 9999px;
}
.dropdown-trigger {
display: flex;
align-items: center;
gap: 0.5rem;
background: rgba(255, 255, 255, 0.04);
border: none;
border-radius: 9999px 0 0 9999px;
padding: 0.6rem 1rem;
font-family: inherit;
font-size: 0.9rem;
font-weight: 500;
color: rgba(255, 255, 255, 0.7);
cursor: pointer;
transition: background-color 0.2s ease;
white-space: nowrap;
position: relative;
z-index: 1;
}
.dropdown-trigger:hover {
background: rgba(255, 255, 255, 0.08);
}
.chevron {
transition: transform 0.2s ease;
opacity: 0.6;
}
.install-box.open .chevron {
transform: rotate(180deg);
}
.command-area {
display: flex;
align-items: center;
gap: 0.75rem;
background: transparent;
border: none;
padding: 0.6rem 1rem;
font-family: 'JetBrains Mono', monospace;
font-size: 0.875rem;
line-height: 1;
}
.command-text {
white-space: nowrap;
background: transparent;
min-width: 47ch; /* Fixed width for longest command (curl) */
}
/* Remove any background from Shiki-generated spans */
.command-text :global(span) {
background: transparent !important;
}
.copy-btn {
background: none;
border: none;
color: rgba(255, 255, 255, 0.4);
cursor: pointer;
padding: 0;
transition: color 0.2s ease;
flex-shrink: 0;
width: 16px;
height: 16px;
display: grid;
place-items: center;
}
.copy-btn:hover {
color: #fff;
}
.copy-btn .copy-icon,
.copy-btn .check-icon {
grid-area: 1 / 1;
pointer-events: none;
transition: opacity 0.15s ease;
}
.copy-btn .check-icon {
opacity: 0;
color: #22c55e;
transform: translateY(-6px);
}
.copy-btn.copied .copy-icon {
opacity: 0;
}
.copy-btn.copied .check-icon {
opacity: 1;
}
.dropdown-menu {
position: absolute;
top: 90%;
left: 0;
background: #1a1a1f;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 0.75rem;
padding: 0.5rem !important;
list-style: none;
margin: 0;
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
opacity: 0;
visibility: hidden;
transform: translateY(-8px);
transition: all 0.2s ease;
z-index: 100;
min-width: 120px;
}
.install-box.open .dropdown-menu {
opacity: 1;
visibility: visible;
transform: translateY(0);
}
.dropdown-menu li {
margin: 0;
padding: 0;
}
.dropdown-option {
width: 100%;
margin-bottom: 0 !important;
display: block;
padding: 0.5rem 0.75rem;
background: none;
border: none;
border-radius: 0.5rem;
font-family: inherit;
font-size: 0.875rem;
color: rgba(255, 255, 255, 0.8);
cursor: pointer;
text-align: left;
transition: background-color 0.15s ease;
}
.dropdown-option:hover {
background: rgba(255, 255, 255, 0.08);
}
.dropdown-option.selected {
background: rgba(255, 255, 255, 0.1);
font-weight: 500;
}
.dropdown-option:focus {
outline: none;
background: rgba(255, 255, 255, 0.12);
}
.dropdown-option:focus-visible {
outline: 2px solid rgba(255, 255, 255, 0.4);
outline-offset: -2px;
}
</style>
<script is:inline>
(function() {
function initInstallSelector() {
document.querySelectorAll('.install-selector').forEach(function(selector) {
const installBox = selector.querySelector('.install-box');
const trigger = selector.querySelector('.dropdown-trigger');
const label = selector.querySelector('.dropdown-label');
const commandText = selector.querySelector('.command-text');
const copyBtn = selector.querySelector('.copy-btn');
const options = selector.querySelectorAll('.dropdown-option');
const optionsArray = Array.from(options);
if (!trigger || !installBox) return;
/**
* Selects an option: updates UI, closes dropdown, returns focus to trigger.
*/
function selectOption(option) {
const newLabel = option.getAttribute('data-label');
const newCommand = option.getAttribute('data-command');
const newHighlighted = option.getAttribute('data-highlighted');
if (label && newLabel) label.textContent = newLabel;
if (commandText && newHighlighted) {
commandText.innerHTML = newHighlighted;
}
if (commandText && newCommand) {
commandText.setAttribute('data-command', newCommand);
}
if (copyBtn && newCommand) {
copyBtn.setAttribute('data-command', newCommand);
}
// Update selected state
options.forEach(function(opt) {
opt.classList.remove('selected');
opt.setAttribute('aria-selected', 'false');
});
option.classList.add('selected');
option.setAttribute('aria-selected', 'true');
// Close dropdown and return focus
installBox.classList.remove('open');
trigger.setAttribute('aria-expanded', 'false');
trigger.focus();
}
/**
* Opens the dropdown and focuses the selected or first option.
*/
function openDropdown() {
// Close all other dropdowns first
document.querySelectorAll('.install-box.open').forEach(function(box) {
if (box !== installBox) box.classList.remove('open');
});
installBox.classList.add('open');
trigger.setAttribute('aria-expanded', 'true');
// Focus the currently selected option, or first option
const selectedOption = selector.querySelector('.dropdown-option.selected') || options[0];
if (selectedOption) selectedOption.focus();
}
/**
* Closes the dropdown and returns focus to trigger.
*/
function closeDropdown() {
installBox.classList.remove('open');
trigger.setAttribute('aria-expanded', 'false');
trigger.focus();
}
// Toggle dropdown on click
trigger.addEventListener('click', function(e) {
e.stopPropagation();
const isOpen = installBox.classList.contains('open');
if (isOpen) {
closeDropdown();
} else {
openDropdown();
}
});
// Keyboard navigation on trigger
trigger.addEventListener('keydown', function(e) {
const isOpen = installBox.classList.contains('open');
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
e.preventDefault();
if (!isOpen) {
openDropdown();
} else {
// Focus first or last option based on direction
const targetOption = e.key === 'ArrowDown' ? options[0] : options[options.length - 1];
if (targetOption) targetOption.focus();
}
}
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
if (!isOpen) {
openDropdown();
}
}
if (e.key === 'Home' && isOpen) {
e.preventDefault();
if (options[0]) options[0].focus();
}
if (e.key === 'End' && isOpen) {
e.preventDefault();
if (options[options.length - 1]) options[options.length - 1].focus();
}
});
// Handle option click and keyboard navigation
options.forEach(function(option, index) {
option.addEventListener('click', function() {
selectOption(option);
});
option.addEventListener('keydown', function(e) {
if (e.key === 'ArrowDown') {
e.preventDefault();
const nextIndex = index < optionsArray.length - 1 ? index + 1 : 0;
optionsArray[nextIndex].focus();
}
if (e.key === 'ArrowUp') {
e.preventDefault();
const prevIndex = index > 0 ? index - 1 : optionsArray.length - 1;
optionsArray[prevIndex].focus();
}
if (e.key === 'Home') {
e.preventDefault();
options[0].focus();
}
if (e.key === 'End') {
e.preventDefault();
options[options.length - 1].focus();
}
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
selectOption(option);
}
if (e.key === 'Escape') {
e.preventDefault();
closeDropdown();
}
if (e.key === 'Tab') {
// Close dropdown when tabbing away
closeDropdown();
}
});
});
// Copy functionality
if (copyBtn) {
copyBtn.addEventListener('click', async function() {
const command = copyBtn.getAttribute('data-command');
if (command) {
await navigator.clipboard.writeText(command);
copyBtn.classList.add('copied');
setTimeout(function() { copyBtn.classList.remove('copied'); }, 2000);
}
});
}
});
// Close dropdown when clicking outside
document.addEventListener('click', function(e) {
if (!e.target.closest('.install-selector')) {
document.querySelectorAll('.install-box.open').forEach(function(box) {
box.classList.remove('open');
const trigger = box.querySelector('.dropdown-trigger');
if (trigger) trigger.setAttribute('aria-expanded', 'false');
});
}
});
// Close on escape (global handler for when focus is elsewhere)
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape') {
document.querySelectorAll('.install-box.open').forEach(function(box) {
box.classList.remove('open');
const trigger = box.querySelector('.dropdown-trigger');
if (trigger) {
trigger.setAttribute('aria-expanded', 'false');
trigger.focus();
}
});
}
});
}
// Run when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initInstallSelector);
} else {
initInstallSelector();
}
// Re-run after Astro page transitions
document.addEventListener('astro:after-swap', initInstallSelector);
})();
</script>