Skip to content

Commit 5b1a044

Browse files
committed
proper theme toggle for giscus
1 parent 51acd55 commit 5b1a044

2 files changed

Lines changed: 107 additions & 5 deletions

File tree

static/js/themetoggle.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
function setTheme(mode) {
2+
localStorage.setItem("theme-storage", mode);
3+
}
4+
5+
function changeGiscusTheme() {
6+
const theme = getSavedTheme();
7+
function sendMessage(message) {
8+
const iframe = document.querySelector("iframe.giscus-frame");
9+
if (!iframe) return;
10+
iframe.contentWindow.postMessage({ giscus: message }, "https://giscus.app");
11+
}
12+
sendMessage({
13+
setConfig: {
14+
theme,
15+
},
16+
});
17+
}
18+
19+
function toggleTheme() {
20+
if (localStorage.getItem("theme-storage") === "light") {
21+
setTheme("dark");
22+
updateItemToggleTheme();
23+
changeGiscusTheme();
24+
} else if (localStorage.getItem("theme-storage") === "dark") {
25+
setTheme("light");
26+
updateItemToggleTheme();
27+
changeGiscusTheme();
28+
}
29+
}
30+
31+
function updateItemToggleTheme() {
32+
let mode = getSavedTheme();
33+
34+
const darkModeStyle = document.getElementById("darkModeStyle");
35+
if (darkModeStyle) {
36+
darkModeStyle.disabled = mode === "light";
37+
}
38+
39+
const sunIcon = document.getElementById("sun-icon");
40+
const moonIcon = document.getElementById("moon-icon");
41+
if (sunIcon && moonIcon) {
42+
sunIcon.style.display = (mode === "dark") ? "block" : "none";
43+
moonIcon.style.display = (mode === "light") ? "block" : "none";
44+
}
45+
46+
let htmlElement = document.querySelector("html");
47+
if (mode === "dark") {
48+
htmlElement.classList.remove("light");
49+
htmlElement.classList.add("dark");
50+
} else if (mode === "light") {
51+
htmlElement.classList.remove("dark");
52+
htmlElement.classList.add("light");
53+
}
54+
}
55+
56+
function getSavedTheme() {
57+
let currentTheme = localStorage.getItem("theme-storage");
58+
if (!currentTheme) {
59+
if (
60+
window.matchMedia &&
61+
window.matchMedia("(prefers-color-scheme: dark)").matches
62+
) {
63+
currentTheme = "dark";
64+
} else {
65+
currentTheme = "light";
66+
}
67+
}
68+
69+
return currentTheme;
70+
}
71+
72+
// Update the toggle theme on page load
73+
updateItemToggleTheme();

templates/_giscus_script.html

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
1-
<script src="https://giscus.app/client.js" data-repo="pacokwon/pacokwon.github.io"
2-
data-repo-id="MDEwOlJlcG9zaXRvcnkzMDE5NjEzMTE=" data-category="Comments" data-category-id="DIC_kwDOEf-QX84CugNx"
3-
data-mapping="pathname" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0"
4-
data-input-position="bottom" data-theme="gruvbox_dark" data-lang="en" crossorigin="anonymous" async>
5-
</script>
1+
<script>
2+
let currentTheme = localStorage.getItem("theme-storage");
3+
if (!currentTheme) {
4+
if (
5+
window.matchMedia &&
6+
window.matchMedia("(prefers-color-scheme: dark)").matches
7+
) {
8+
currentTheme = "dark";
9+
} else {
10+
currentTheme = "light";
11+
}
12+
}
13+
14+
let giscusAttributes = {
15+
"src": "https://giscus.app/client.js",
16+
"data-repo": "pacokwon/pacokwon.github.io",
17+
"data-repo-id": "MDEwOlJlcG9zaXRvcnkzMDE5NjEzMTE=",
18+
"data-category": "Comments",
19+
"data-category-id": "DIC_kwDOEf-QX84CugNx",
20+
"data-mapping": "pathname",
21+
"data-strict": "0",
22+
"data-reactions-enabled": "1",
23+
"data-emit-metadata": "0",
24+
"data-input-position": "bottom",
25+
"data-theme": currentTheme,
26+
"data-lang": "en",
27+
"crossorigin": "anonymous",
28+
"async": "",
29+
};
30+
31+
let giscusScript = document.createElement("script");
32+
Object.entries(giscusAttributes).forEach(([key, value]) => giscusScript.setAttribute(key, value));
33+
document.body.appendChild(giscusScript);
34+
</script>

0 commit comments

Comments
 (0)