Skip to content

Commit 6688734

Browse files
committed
Implement theme toggle functionality and update Giscus theme handling
1 parent 2c9c674 commit 6688734

2 files changed

Lines changed: 117 additions & 1 deletion

File tree

_includes/giscus.html

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,59 @@
1111
data-reactions-enabled="{{ site.giscus.reactions-enabled }}"
1212
data-emit-metadata="{{ site.giscus.emit-metadata }}"
1313
data-input-position="{{ site.giscus.input-position }}"
14-
data-theme="preferred_color_scheme"
14+
data-theme="dark"
1515
data-lang="{{ site.giscus.lang }}"
1616
crossorigin="anonymous"
1717
async>
1818
</script>
1919
</div>
20+
21+
<script>
22+
// Function to update Giscus theme based on Jekyll theme
23+
function updateGiscusTheme() {
24+
const currentTheme = document.documentElement.getAttribute('data-theme');
25+
const giscusFrame = document.querySelector('iframe.giscus-frame');
26+
27+
if (giscusFrame) {
28+
const giscusTheme = currentTheme === 'dark' ? 'dark' : 'light';
29+
giscusFrame.contentWindow.postMessage({
30+
giscus: {
31+
setConfig: {
32+
theme: giscusTheme
33+
}
34+
}
35+
}, 'https://giscus.app');
36+
console.log('Updated Giscus theme to:', giscusTheme);
37+
}
38+
}
39+
40+
// Set initial theme when Giscus loads
41+
document.addEventListener('DOMContentLoaded', function() {
42+
// Wait for Giscus to load, then set initial theme
43+
setTimeout(updateGiscusTheme, 1000);
44+
45+
// Also try updating theme when iframe loads
46+
const checkForGiscus = setInterval(() => {
47+
if (document.querySelector('iframe.giscus-frame')) {
48+
updateGiscusTheme();
49+
clearInterval(checkForGiscus);
50+
}
51+
}, 500);
52+
});
53+
54+
// Watch for theme changes on the document element
55+
const observer = new MutationObserver(function(mutations) {
56+
mutations.forEach(function(mutation) {
57+
if (mutation.type === 'attributes' && mutation.attributeName === 'data-theme') {
58+
setTimeout(updateGiscusTheme, 100); // Small delay to ensure theme change is complete
59+
}
60+
});
61+
});
62+
63+
// Start observing theme changes
64+
observer.observe(document.documentElement, {
65+
attributes: true,
66+
attributeFilter: ['data-theme']
67+
});
68+
</script>
2069
{% endif %}

_includes/header.html

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,25 @@
1010
{% endif %}
1111
{% endfor %}
1212
</span>
13+
14+
<!-- Theme Toggle -->
15+
<div class="toggleWrapper">
16+
<input type="checkbox" class="dn" id="theme-toggle" onclick="modeSwitcher()" checked />
17+
<label for="theme-toggle" class="toggle">
18+
<span class="toggle__handler">
19+
<span class="crater crater--1"></span>
20+
<span class="crater crater--2"></span>
21+
<span class="crater crater--3"></span>
22+
</span>
23+
<span class="star star--1"></span>
24+
<span class="star star--2"></span>
25+
<span class="star star--3"></span>
26+
<span class="star star--4"></span>
27+
<span class="star star--5"></span>
28+
<span class="star star--6"></span>
29+
</label>
30+
</div>
31+
1332
<br style="clear: both;">
1433
<div style="margin-top: 0.5em; padding-top: 0.75em;">
1534
<a href="/about/" style="display: inline-block; margin-right: 2em; color: #de5684; text-decoration: none; font-size: 1em;">About</a>
@@ -18,3 +37,51 @@
1837
</div>
1938
</div>
2039
</div>
40+
41+
<script type="text/javascript">
42+
const theme = localStorage.getItem('theme');
43+
44+
if (theme === "light") {
45+
document.documentElement.setAttribute('data-theme', 'light');
46+
} else {
47+
document.documentElement.setAttribute('data-theme', 'dark');
48+
}
49+
const userPrefers = getComputedStyle(document.documentElement).getPropertyValue('content');
50+
51+
function activateDarkTheme() {
52+
document.getElementById('theme-toggle').checked = true;
53+
document.documentElement.setAttribute('data-theme', 'dark');
54+
document.documentElement.classList.add('theme--dark');
55+
document.documentElement.classList.remove('theme--light');
56+
document.getElementById("theme-toggle").className = 'light';
57+
window.localStorage.setItem('theme', 'dark');
58+
}
59+
60+
function activateLightTheme() {
61+
document.getElementById('theme-toggle').checked = false;
62+
document.documentElement.setAttribute('data-theme', 'light');
63+
document.documentElement.classList.add('theme--light');
64+
document.documentElement.classList.remove('theme--dark');
65+
document.getElementById("theme-toggle").className = 'dark';
66+
window.localStorage.setItem('theme', 'light');
67+
}
68+
69+
if (theme === "dark") {
70+
activateDarkTheme();
71+
} else if (theme === "light") {
72+
activateLightTheme();
73+
} else if (userPrefers === "light") {
74+
activateDarkTheme();
75+
} else {
76+
activateDarkTheme();
77+
}
78+
79+
function modeSwitcher() {
80+
let currentMode = document.documentElement.getAttribute('data-theme');
81+
if (currentMode === "dark") {
82+
activateLightTheme();
83+
} else {
84+
activateDarkTheme();
85+
}
86+
}
87+
</script>

0 commit comments

Comments
 (0)