-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
codeGroups.ts
43 lines (35 loc) · 1.18 KB
/
codeGroups.ts
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
import { inBrowser, onContentUpdated } from 'vitepress'
export function useCodeGroups() {
if (import.meta.env.DEV) {
onContentUpdated(() => {
document.querySelectorAll('.vp-code-group > .blocks').forEach((el) => {
Array.from(el.children).forEach((child) => {
child.classList.remove('active')
})
el.children[0].classList.add('active')
})
})
}
if (inBrowser) {
window.addEventListener('click', (e) => {
const el = e.target as HTMLInputElement
if (el.matches('.vp-code-group input')) {
// input <- .tabs <- .vp-code-group
const group = el.parentElement?.parentElement
if (!group) return
const i = Array.from(group.querySelectorAll('input')).indexOf(el)
if (i < 0) return
const blocks = group.querySelector('.blocks')
if (!blocks) return
const current = Array.from(blocks.children).find((child) =>
child.classList.contains('active')
)
if (!current) return
const next = blocks.children[i]
if (!next || current === next) return
current.classList.remove('active')
next.classList.add('active')
}
})
}
}