-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathcodeModal.ts
51 lines (41 loc) · 1.16 KB
/
codeModal.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
44
45
46
47
48
49
50
51
import { inBrowser } from 'vitepress'
export function useCodeModal() {
if (inBrowser) {
window.addEventListener('click', (e) => {
const el = e.target as HTMLElement
if (el.matches('div[class*="language-"] > button.modal')) {
//remove focus from button
el.blur()
const parent = el.parentElement
const sibling = el.nextElementSibling
if (!parent || !sibling) {
return
}
sibling.classList.add('open')
}
if (
el.matches('div[class*="language-"] div.modal-container button.close')
) {
const parent = el.parentElement?.parentElement
if (!parent) {
return
}
parent.classList.remove('open')
}
if (el.matches('div[class*="language-"] > div.modal-container')) {
el.classList.remove('open')
}
})
window.addEventListener('keydown', (ev) => {
if (ev.key == 'Escape') {
let modal = window.document.querySelector(
'div[class*="language-"] > div.modal-container.open'
)
if (!modal) {
return
}
modal.classList.remove('open')
}
})
}
}