-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdarkmode.js
33 lines (29 loc) · 875 Bytes
/
darkmode.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
27
28
29
30
31
32
33
const html = document.querySelector("html")
const checkbox = document.querySelector("input[name=theme]")
const getStyle = (element, style) =>
window
.getComputedStyle(element)
.getPropertyValue(style)
const initialColors = {
bg :getStyle(html, "--bg"),
bgPanel : getStyle(html, "--bg-panel"),
colorHeadings: getStyle(html, "--color-headings"),
coloText: getStyle(html, "--color-text"),
}
const darkMode = {
bg:"#333333",
bgPanel:"#434343",
colorHeadings:"#3664FF",
colorText:"#858585"
}
const transformKey = key =>
"--" + key.replace(/([A-Z])/, "-$1").toLowerCase()
const changeColors = (colors) => {
Object.keys(colors).map(key =>
html.style.setProperty(transformKey(key), colors[key])
)
}
checkbox.addEventListener("change", ({target}) =>{
target.checked ? changeColors(darkMode) : changeColors(initialColors)
}
)