-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (50 loc) · 1.93 KB
/
index.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const hueInput = document.querySelector("input#hue-input");
hueInput?.addEventListener("input", (event) => {
document.documentElement.style.setProperty(
"--palette-hue",
event.target.value
);
});
const darkModeButton = document.querySelector("button#dark-mode-toggle");
darkModeButton?.addEventListener("click", function () {
const darkClassName = "dark";
const rootElement = document.documentElement;
const isDarkModeActive = rootElement.classList.contains(darkClassName);
if (isDarkModeActive) {
// if the dark theme is active, we remove the class from the root element
rootElement.classList.remove(darkClassName);
return;
}
// otherwise we activate dark mode by adding the class to the root element
rootElement.classList.add(darkClassName);
});
const cssButton = document.querySelector("button#css-toggle");
cssButton?.addEventListener("click", function () {
const stylesheet = document.styleSheets.item(2);
const isStyleSheetDisabled = stylesheet.disabled;
// if the stylesheet is already disabled, no need to confirm action
if (isStyleSheetDisabled) {
stylesheet.disabled = false;
return;
}
// if the styles are enabled, confirm the action with the user
const confirmed = window.confirm(
"The page layout will change. Are you sure you want to disable CSS?"
);
if (confirmed) {
stylesheet.disabled = true;
}
});
const fontButton = document.querySelector("button#font-toggle");
fontButton?.addEventListener("click", function () {
const foutClassName = "fonts-loaded";
const rootElement = document.documentElement;
const isLoadedFontActive = rootElement.classList.contains(foutClassName);
if (isLoadedFontActive) {
// if the loaded font is enabled, we remove the class from the root element
rootElement.classList.remove(foutClassName);
return;
}
// otherwise we activate dark mode by adding the class to the root element
rootElement.classList.add(foutClassName);
});