Skip to content

Commit a8038d0

Browse files
committed
Make dark/light mode change with website theme.
1 parent 96c9702 commit a8038d0

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

layouts/partials/comments.html

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,52 @@
1-
<script src="https://utteranc.es/client.js"
2-
repo="ramsayleung/comment"
3-
issue-term="title"
4-
theme="github-light"
5-
crossorigin="anonymous"
6-
async>
1+
<section>
2+
<h2>Comments</h2>
3+
<div id="comments-utteranc"></div>
4+
</section>
5+
6+
<script type="text/javascript">
7+
8+
function getCurrentTheme() {
9+
return document.documentElement.getAttribute('data-theme') || document.body.classList.contains('dark') ? 'dark' : 'light';
10+
}
11+
12+
function loadUtterances(darkMode=false) {
13+
const giscusContainer = document.getElementById("comments-utteranc");
14+
if (giscusContainer !== null) {
15+
giscusContainer.innerHTML = ''
16+
const giscusScript = document.createElement("script");
17+
giscusScript.setAttribute("id", "utteranc");
18+
giscusScript.setAttribute("src", "https://utteranc.es/client.js");
19+
giscusScript.setAttribute("data-repo", "ramsayleung/comment");
20+
giscusScript.setAttribute("data-category", "Announcements");
21+
giscusScript.setAttribute("data-mapping", "pathname");
22+
giscusScript.setAttribute("data-reactions-enabled", "1");
23+
giscusScript.setAttribute("data-emit-metadata", "0");
24+
giscusScript.setAttribute("data-theme", darkMode ? "github-dark" : "github-light");
25+
giscusScript.setAttribute("data-issue-term", "title");
26+
giscusScript.setAttribute("crossorigin", "anonymous");
27+
giscusScript.setAttribute("async", "true");
28+
giscusContainer.appendChild(giscusScript);
29+
}
30+
}
31+
32+
const isDarkMode = getCurrentTheme() === 'dark';
33+
loadUtterances(isDarkMode);
34+
35+
// Watch for theme changes
36+
const observer = new MutationObserver((mutations) => {
37+
mutations.forEach((mutation) => {
38+
if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
39+
const isDarkMode = getCurrentTheme() === 'dark';
40+
loadUtterances(isDarkMode);
41+
console.log(`changing theme`);
42+
}
43+
});
44+
});
45+
46+
// Start observing the body element for class changes
47+
observer.observe(document.body, {
48+
attributes: true,
49+
attributeFilter: ['class']
50+
});
51+
752
</script>

layouts/shortcodes/heatmap.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,8 @@
6464
<script type="text/javascript">
6565
var myChart;
6666

67-
// Function to get current theme
6867
function getCurrentTheme() {
69-
return document.body.classList.contains('dark') ? 'dark' : 'light';
68+
return document.documentElement.getAttribute('data-theme') || document.body.classList.contains('dark') ? 'dark' : 'light';
7069
}
7170

7271
function getThemeColors() {

0 commit comments

Comments
 (0)