Skip to content

Commit 7f9f94d

Browse files
committed
fix: improve link copying, enhance accessibility, and refine scroll behavior
- Updated link copying to include the pathname for better URL accuracy. - Added aria-selected attributes to list items for improved accessibility. - Enhanced scroll behavior to respect user preferences for reduced motion. - Refined keyboard navigation to prevent actions when the palette is empty.
1 parent 7c2ee3f commit 7f9f94d

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

src/scripts/studio.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ function runQuery() {
203203

204204
async function copyLink() {
205205
const id = currentHash();
206-
const url = `${location.origin}/#${id}`;
206+
const url = `${location.origin}${location.pathname}#${id}`;
207207
try {
208208
await navigator.clipboard.writeText(url);
209209
studioConsole.ok(`copied link to #${id}`);
@@ -221,7 +221,7 @@ function downloadBlob(content: string, filename: string, mime: string) {
221221
document.body.appendChild(a);
222222
a.click();
223223
a.remove();
224-
URL.revokeObjectURL(url);
224+
setTimeout(() => URL.revokeObjectURL(url), 1000);
225225
}
226226

227227
function exportSection() {
@@ -298,6 +298,7 @@ function renderPalette(query: string) {
298298
paletteFiltered.forEach((it, i) => {
299299
const li = document.createElement('li');
300300
li.setAttribute('role', 'option');
301+
li.setAttribute('aria-selected', String(i === 0));
301302
li.className = `flex cursor-pointer items-center justify-between px-4 py-2 ${i === 0 ? 'bg-raised text-bright' : 'text-fg'}`;
302303
const labelSpan = document.createElement('span');
303304
labelSpan.textContent = it.label;
@@ -318,6 +319,7 @@ function setHighlight(i: number) {
318319
if (!list) return;
319320
[...list.children].forEach((li, idx) => {
320321
li.className = `flex cursor-pointer items-center justify-between px-4 py-2 ${idx === i ? 'bg-raised text-bright' : 'text-fg'}`;
322+
(li as HTMLElement).setAttribute('aria-selected', String(idx === i));
321323
});
322324
}
323325

@@ -372,7 +374,8 @@ function init() {
372374
if (action === 'palette') { e.preventDefault(); openPalette(); }
373375
if (action === 'results') {
374376
e.preventDefault();
375-
document.querySelector(`[data-section="${currentHash()}"] .studio-results`)?.scrollTo({ top: 0, behavior: 'smooth' });
377+
const smooth = !window.matchMedia('(prefers-reduced-motion: reduce)').matches;
378+
document.querySelector(`[data-section="${currentHash()}"] .studio-results`)?.scrollTo({ top: 0, behavior: smooth ? 'smooth' : 'auto' });
376379
}
377380
});
378381

@@ -395,8 +398,11 @@ function init() {
395398
if (!root || root.hasAttribute('hidden')) return;
396399
if (e.key === 'Escape') { e.preventDefault(); closePalette(); }
397400
else if (e.key === 'Tab') { e.preventDefault(); (document.querySelector('[data-palette-input]') as HTMLElement | null)?.focus(); }
398-
else if (e.key === 'ArrowDown') { e.preventDefault(); setHighlight(Math.min(paletteHighlight + 1, paletteFiltered.length - 1)); }
399-
else if (e.key === 'ArrowUp') { e.preventDefault(); setHighlight(Math.max(paletteHighlight - 1, 0)); }
401+
else if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
402+
if (paletteFiltered.length === 0) { e.preventDefault(); return; }
403+
if (e.key === 'ArrowDown') { e.preventDefault(); setHighlight(Math.min(paletteHighlight + 1, paletteFiltered.length - 1)); }
404+
else { e.preventDefault(); setHighlight(Math.max(paletteHighlight - 1, 0)); }
405+
}
400406
else if (e.key === 'Enter') { e.preventDefault(); paletteFiltered[paletteHighlight]?.run(); closePalette(); }
401407
});
402408

0 commit comments

Comments
 (0)