-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathmod.js
26 lines (25 loc) · 1.31 KB
/
mod.js
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
/// <reference lib="dom" />
const prefers = matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"
const html = document.querySelector("html")
html.dataset.colorScheme = prefers
document.querySelectorAll(`body > header .color-scheme`).forEach((element) => {
element.querySelector(`svg.${prefers}`).style.display = "inline-block"
element.addEventListener("click", () => {
toggleColorScheme(html.dataset, element)
})
})
document.querySelectorAll(".example-tabs li.color-scheme").forEach((element) => {
element.querySelector(`svg.${prefers}`).style.display = "inline-block"
element.addEventListener("click", () => {
const example = element.parentNode.nextElementSibling
if (example.tagName === "IFRAME") {
example.contentWindow.document.documentElement.dataset.colorScheme = (example.contentWindow.document.documentElement.dataset.colorScheme ?? prefers) === "light" ? "dark" : "light"
}
toggleColorScheme(example.dataset, element)
})
})
function toggleColorScheme(dataset, element) {
dataset.colorScheme = (dataset.colorScheme ?? prefers) === "light" ? "dark" : "light"
element.querySelector("svg.light").style.display = dataset.colorScheme === "light" ? "inline-block" : "none"
element.querySelector("svg.dark").style.display = dataset.colorScheme === "dark" ? "inline-block" : "none"
}