1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-04-27 06:38:30 -04:00

dynamically switch between utteranc.es's light/dark themes (#118)

This commit is contained in:
Jake Jarvis 2020-04-26 10:49:45 -04:00
parent 3200509ffd
commit c986676379
Signed by: jake
GPG Key ID: 2B0C9CF251E69A39

View File

@ -12,15 +12,38 @@ let pref = localStorage.getItem('dark_mode_pref');
// keep track of current state, light by default
let activated = false;
// dynamically switch utteranc.es's theme
const setUtterances = function(t) {
// if utteranc.es iframe hasn't loaded yet
const script = document.querySelector('script[src="https://utteranc.es/client.js"]');
if (script) script.setAttribute('data-theme', t);
// if utteranc.es iframe has already loaded
const frame = document.querySelector('iframe.utterances-frame');
if (frame) {
// https://github.com/utterance/utterances/blob/4d9823c6c4f9a58365f06e2aa76c51b8cf5d5478/src/configuration-component.ts#L160
frame.contentWindow.postMessage({
type: 'set-theme',
theme: t
}, 'https://utteranc.es');
}
};
const activateDarkMode = function() {
document.body.classList.remove('light');
document.body.classList.add('dark');
setUtterances('github-dark');
activated = true;
};
const activateLightMode = function() {
document.body.classList.remove('dark');
document.body.classList.add('light');
setUtterances('github-light');
activated = false;
};