mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2025-10-29 00:15:47 -04:00
wrap dark mode stuff in an anonymous function like a big boy coder
This commit is contained in:
@@ -2,19 +2,22 @@
|
|||||||
|
|
||||||
// inspired by https://codepen.io/kevinpowell/pen/EMdjOV
|
// inspired by https://codepen.io/kevinpowell/pen/EMdjOV
|
||||||
|
|
||||||
// lightbulb toggle re-appears now that we know user has JS enabled
|
(function() {
|
||||||
const toggle = document.querySelector('button#dark-mode-toggle');
|
'use strict';
|
||||||
toggle.style.visibility = 'visible';
|
|
||||||
|
|
||||||
// check for preset `dark_mode_pref` in localStorage
|
// lightbulb toggle re-appears now that we know user has JS enabled
|
||||||
let pref = localStorage.getItem('dark_mode_pref');
|
const toggle = document.querySelector('button#dark-mode-toggle');
|
||||||
|
toggle.style.visibility = 'visible';
|
||||||
|
|
||||||
// keep track of current state (light by default)
|
// check for preset `dark_mode_pref` in localStorage
|
||||||
let activated = false;
|
let pref = localStorage.getItem('dark_mode_pref');
|
||||||
|
|
||||||
// dynamically switch utteranc.es's theme
|
// keep track of current state (light by default)
|
||||||
let utterancesInit = false;
|
let activated = false;
|
||||||
const setUtterances = function() {
|
|
||||||
|
// dynamically switch utteranc.es's theme
|
||||||
|
let utterancesInit = false;
|
||||||
|
const setUtterances = function() {
|
||||||
let theme = activated ? 'github-dark' : 'github-light';
|
let theme = activated ? 'github-dark' : 'github-light';
|
||||||
const container = document.querySelector('div#comments');
|
const container = document.querySelector('div#comments');
|
||||||
|
|
||||||
@@ -47,32 +50,32 @@ const setUtterances = function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const activateDarkMode = function() {
|
const activateDarkMode = function() {
|
||||||
document.body.classList.remove('light');
|
document.body.classList.remove('light');
|
||||||
document.body.classList.add('dark');
|
document.body.classList.add('dark');
|
||||||
|
|
||||||
activated = true;
|
activated = true;
|
||||||
|
|
||||||
setUtterances();
|
setUtterances();
|
||||||
};
|
};
|
||||||
|
|
||||||
const activateLightMode = function() {
|
const activateLightMode = function() {
|
||||||
document.body.classList.remove('dark');
|
document.body.classList.remove('dark');
|
||||||
document.body.classList.add('light');
|
document.body.classList.add('light');
|
||||||
|
|
||||||
activated = false;
|
activated = false;
|
||||||
|
|
||||||
setUtterances();
|
setUtterances();
|
||||||
};
|
};
|
||||||
|
|
||||||
// if user already explicitly toggled in the past, restore their preference.
|
// if user already explicitly toggled in the past, restore their preference.
|
||||||
if (pref === 'true') activateDarkMode();
|
if (pref === 'true') activateDarkMode();
|
||||||
if (pref === 'false') activateLightMode();
|
if (pref === 'false') activateLightMode();
|
||||||
|
|
||||||
// user has never clicked the button, so go by their OS preference until/if they do so
|
// user has never clicked the button, so go by their OS preference until/if they do so
|
||||||
if (!pref) {
|
if (!pref) {
|
||||||
// check for OS dark mode setting and switch accordingly
|
// check for OS dark mode setting and switch accordingly
|
||||||
// https://gist.github.com/Gioni06/eb5b28343bcf5793a70f6703004cf333#file-darkmode-js-L47
|
// https://gist.github.com/Gioni06/eb5b28343bcf5793a70f6703004cf333#file-darkmode-js-L47
|
||||||
if (window.matchMedia('(prefers-color-scheme: dark)').matches)
|
if (window.matchMedia('(prefers-color-scheme: dark)').matches)
|
||||||
@@ -85,10 +88,10 @@ if (!pref) {
|
|||||||
// right now these keep listening even if pref is set.
|
// right now these keep listening even if pref is set.
|
||||||
window.matchMedia('(prefers-color-scheme: dark)').addListener(function(e) { if (e.matches) activateDarkMode(); });
|
window.matchMedia('(prefers-color-scheme: dark)').addListener(function(e) { if (e.matches) activateDarkMode(); });
|
||||||
window.matchMedia('(prefers-color-scheme: light)').addListener(function(e) { if (e.matches) activateLightMode(); });
|
window.matchMedia('(prefers-color-scheme: light)').addListener(function(e) { if (e.matches) activateLightMode(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle lightbulb click
|
// handle lightbulb click
|
||||||
toggle.addEventListener('click', function() {
|
toggle.addEventListener('click', function() {
|
||||||
// switch to the opposite theme & save preference in local storage
|
// switch to the opposite theme & save preference in local storage
|
||||||
if (!activated) {
|
if (!activated) {
|
||||||
activateDarkMode();
|
activateDarkMode();
|
||||||
@@ -97,4 +100,5 @@ toggle.addEventListener('click', function() {
|
|||||||
activateLightMode();
|
activateLightMode();
|
||||||
localStorage.setItem('dark_mode_pref', 'false');
|
localStorage.setItem('dark_mode_pref', 'false');
|
||||||
}
|
}
|
||||||
}, true);
|
}, true);
|
||||||
|
})();
|
||||||
|
|||||||
6
assets/vendor/emoji/emoji.js
vendored
6
assets/vendor/emoji/emoji.js
vendored
@@ -7,7 +7,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var emoji = (function (
|
(function (
|
||||||
|
|
||||||
// WARNING: this file is generated automatically via
|
// WARNING: this file is generated automatically via
|
||||||
// `node scripts/build.js`
|
// `node scripts/build.js`
|
||||||
@@ -570,6 +570,4 @@ var emoji = (function (
|
|||||||
return r.join(sep || '-');
|
return r.join(sep || '-');
|
||||||
}
|
}
|
||||||
|
|
||||||
}());
|
}()).parse(document.body);
|
||||||
|
|
||||||
emoji.parse(document.body);
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
{{ block "main" . }}{{ end }}
|
{{ block "main" . }}{{ end }}
|
||||||
</div>
|
</div>
|
||||||
{{ partialCached "page/footer" . }}
|
{{ partialCached "page/footer" . }}
|
||||||
|
{{ partial "scripts/_bundle" . }}
|
||||||
{{ partial "scripts/shortcodes" . }}
|
{{ partial "scripts/shortcodes" . }}
|
||||||
{{ if eq hugo.Environment "production" }}
|
{{ if eq hugo.Environment "production" }}
|
||||||
{{ partialCached "scripts/simple_analytics" . }}
|
{{ partialCached "scripts/simple_analytics" . }}
|
||||||
|
|||||||
@@ -8,5 +8,4 @@
|
|||||||
{{ partialCached "head/feeds" . -}}
|
{{ partialCached "head/feeds" . -}}
|
||||||
{{ partialCached "head/webmention" . -}}
|
{{ partialCached "head/webmention" . -}}
|
||||||
{{ partial "head/canonical" . -}}
|
{{ partial "head/canonical" . -}}
|
||||||
{{ partial "scripts/_bundle" . }}
|
|
||||||
{{ partial "head/schema" . -}}
|
{{ partial "head/schema" . -}}
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
<script async defer src="https://s.jarv.is/app.js" data-skip-dnt="true"></script>
|
<script async defer src="https://s.jarv.is/latest.js" data-skip-dnt="true"></script>
|
||||||
<noscript><img src="https://s.jarv.is/image.gif" alt=""></noscript>
|
<noscript><img src="https://s.jarv.is/image.gif" alt=""></noscript>
|
||||||
|
|||||||
Reference in New Issue
Block a user