Skip to content

Commit fb9cee9

Browse files
azat-iobrc-dd
andauthored
perf(a11y): change copy code span to button (#1056)
Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
1 parent 867f305 commit fb9cee9

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

src/client/theme-default/composables/copy-code.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function useCopyCode() {
1111
nextTick(() => {
1212
document
1313
.querySelectorAll<HTMLSpanElement>(
14-
'.vp-doc div[class*="language-"] > span.copy'
14+
'.vp-doc div[class*="language-"] > button.copy'
1515
)
1616
.forEach(handleElement)
1717
})
@@ -67,7 +67,8 @@ async function copyToClipboard(text: string) {
6767
function handleElement(el: HTMLElement) {
6868
el.onclick = () => {
6969
const parent = el.parentElement
70-
const sibling = el.nextElementSibling as HTMLPreElement | null
70+
const sibling = el.nextElementSibling
71+
?.nextElementSibling as HTMLPreElement | null
7172
if (!parent || !sibling) {
7273
return
7374
}
@@ -86,7 +87,8 @@ function handleElement(el: HTMLElement) {
8687
el.classList.add('copied')
8788
setTimeout(() => {
8889
el.classList.remove('copied')
89-
}, 3000)
90+
el.blur()
91+
}, 2000)
9092
})
9193
}
9294
}

src/client/theme-default/styles/components/vp-doc.css

+11-9
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,11 @@
363363
transition: border-color 0.5s, color 0.5s;
364364
}
365365

366-
.vp-doc [class*='language-'] > span.copy {
366+
.vp-doc [class*='language-'] > button.copy {
367367
position: absolute;
368368
top: 8px;
369369
right: 8px;
370-
z-index: 2;
370+
z-index: 3;
371371
display: block;
372372
justify-content: center;
373373
align-items: center;
@@ -384,23 +384,24 @@
384384
transition: opacity 0.4s;
385385
}
386386

387-
.vp-doc [class*='language-']:hover > span.copy {
387+
.vp-doc [class*='language-']:hover > button.copy,
388+
.vp-doc [class*='language-'] > button.copy:focus {
388389
opacity: 1;
389390
}
390391

391-
.vp-doc [class*='language-'] > span.copy:hover {
392+
.vp-doc [class*='language-'] > button.copy:hover {
392393
background-color: var(--vp-code-copy-code-hover-bg);
393394
}
394395

395-
.vp-doc [class*='language-'] > span.copy.copied,
396-
.vp-doc [class*='language-'] > span.copy:hover.copied {
396+
.vp-doc [class*='language-'] > button.copy.copied,
397+
.vp-doc [class*='language-'] > button.copy:hover.copied {
397398
border-radius: 0 4px 4px 0;
398399
background-color: var(--vp-code-copy-code-hover-bg);
399400
background-image: var(--vp-icon-copied);
400401
}
401402

402-
.vp-doc [class*='language-'] > span.copy.copied::before,
403-
.vp-doc [class*='language-'] > span.copy:hover.copied::before {
403+
.vp-doc [class*='language-'] > button.copy.copied::before,
404+
.vp-doc [class*='language-'] > button.copy:hover.copied::before {
404405
position: relative;
405406
left: -65px;
406407
display: block;
@@ -428,7 +429,8 @@
428429
transition: color 0.4s, opacity 0.4s;
429430
}
430431

431-
.vp-doc [class*='language-']:hover > span.lang {
432+
.vp-doc [class*='language-']:hover > button.copy + span.lang,
433+
.vp-doc [class*='language-'] > button.copy:focus + span.lang {
432434
opacity: 0;
433435
}
434436

src/node/markdown/plugins/preWrapper.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export const preWrapperPlugin = (md: MarkdownIt) => {
1515
const [tokens, idx] = args
1616
const lang = tokens[idx].info.trim().replace(/-vue$/, '')
1717
const rawCode = fence(...args)
18-
return `<div class="language-${lang}"><span class="lang">${
18+
return `<div class="language-${lang}"><button class="copy"></button><span class="lang">${
1919
lang === 'vue-html' ? 'template' : lang
20-
}</span><span class="copy"></span>${rawCode}</div>`
20+
}</span>${rawCode}</div>`
2121
}
2222
}

0 commit comments

Comments
 (0)