Skip to content

Commit 9606b85

Browse files
committed
Dynamically adjust comment plugin theme
1 parent ffc89f1 commit 9606b85

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

docs/javascripts/extra.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,45 @@ document.addEventListener('DOMContentLoaded', function() {
2020
}
2121
});
2222
});
23+
24+
// Update giscus theme when Material theme changes
25+
function updateGiscusTheme() {
26+
const iframe = document.querySelector('iframe.giscus-frame');
27+
if (!iframe) return;
28+
29+
// Get current theme from Material's data-md-color-scheme attribute
30+
const scheme = document.body.getAttribute('data-md-color-scheme');
31+
const theme = scheme === 'slate' ? 'dark' : 'light';
32+
33+
// Send theme update message to giscus iframe
34+
iframe.contentWindow.postMessage(
35+
{ giscus: { setConfig: { theme: theme } } },
36+
'https://giscus.app'
37+
);
38+
}
39+
40+
// Watch for theme changes using MutationObserver
41+
const observer = new MutationObserver(function(mutations) {
42+
mutations.forEach(function(mutation) {
43+
if (mutation.type === 'attributes' && mutation.attributeName === 'data-md-color-scheme') {
44+
updateGiscusTheme();
45+
}
46+
});
47+
});
48+
49+
// Start observing the body element for attribute changes
50+
observer.observe(document.body, {
51+
attributes: true,
52+
attributeFilter: ['data-md-color-scheme']
53+
});
54+
55+
// Set initial theme when giscus loads
56+
window.addEventListener('message', function(event) {
57+
if (event.origin !== 'https://giscus.app') return;
58+
if (!(typeof event.data === 'object' && event.data.giscus)) return;
59+
60+
// Once giscus is ready, update its theme
61+
updateGiscusTheme();
62+
});
2363
});
2464

0 commit comments

Comments
 (0)