mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2025-04-27 02:38:27 -04:00
132 lines
5.8 KiB
HTML
132 lines
5.8 KiB
HTML
<!doctype html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
<title>Welcome to the dark side 🌓</title>
|
|
<style>
|
|
/*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */
|
|
html{line-height:1.15;-webkit-text-size-adjust:100%}body{margin:0}main{display:block}h1{font-size:2em;margin:.67em 0}hr{box-sizing:content-box;height:0;overflow:visible}pre{font-family:monospace,monospace;font-size:1em}a{background-color:transparent}abbr[title]{border-bottom:none;text-decoration:underline;text-decoration:underline dotted}b,strong{font-weight:bolder}code,kbd,samp{font-family:monospace,monospace;font-size:1em}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}img{border-style:none}button,input,optgroup,select,textarea{font-family:inherit;font-size:100%;line-height:1.15;margin:0}button,input{overflow:visible}button,select{text-transform:none}[type=button]::-moz-focus-inner,[type=reset]::-moz-focus-inner,[type=submit]::-moz-focus-inner,button::-moz-focus-inner{border-style:none;padding:0}[type=button]:-moz-focusring,[type=reset]:-moz-focusring,[type=submit]:-moz-focusring,button:-moz-focusring{outline:1px dotted ButtonText}fieldset{padding:.35em .75em .625em}legend{box-sizing:border-box;color:inherit;display:table;max-width:100%;padding:0;white-space:normal}progress{vertical-align:baseline}textarea{overflow:auto}[type=checkbox],[type=radio]{box-sizing:border-box;padding:0}[type=number]::-webkit-inner-spin-button,[type=number]::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}[type=search]::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}details{display:block}summary{display:list-item}template{display:none}[hidden]{display:none}
|
|
|
|
/* non-color rules that apply globally */
|
|
body {
|
|
font-family: system-ui, -apple-system, sans-serif;
|
|
text-align: center;
|
|
}
|
|
a {
|
|
text-decoration: none;
|
|
}
|
|
h1 {
|
|
font-size: 1.8em;
|
|
}
|
|
.dark-mode-toggle {
|
|
cursor: pointer;
|
|
padding: 1em;
|
|
|
|
/* hide toggle until we're sure user has JS enabled */
|
|
visibility: hidden;
|
|
}
|
|
|
|
/* theme-specific rules -- you probably only want color-related stuff here. */
|
|
/* SCSS makes this a whole lot easier by allowing nesting, but is not required. */
|
|
body.light {
|
|
background-color: #fff;
|
|
color: #222;
|
|
}
|
|
body.light a {
|
|
color: #06f;
|
|
}
|
|
body.dark {
|
|
background-color: #222;
|
|
color: #fff;
|
|
}
|
|
body.dark a {
|
|
color: #fe0;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body class="light">
|
|
<h1>Welcome to the dark side 🌓</h1>
|
|
|
|
<button class="dark-mode-toggle">💡 Click to see the light... or not.</button>
|
|
|
|
<p><a href="https://github.com/jakejarvis/dark-mode-example" target="_blank" rel="noopener">View the source code</a> or <a href="https://jarv.is/notes/dark-mode/" target="_blank" rel="noopener">read the post</a>.</p>
|
|
|
|
<script>
|
|
/*! Dark mode switcheroo | MIT License | jrvs.io/darkmode */
|
|
|
|
(function () {
|
|
var classes = window.document.body.classList;
|
|
var storage = sessionStorage || localStorage; // modified for example
|
|
|
|
// check for preset `dark_mode_pref` preference in local storage
|
|
var pref_key = 'theme_example-for-dark-mode-post'; // modified for example
|
|
var pref = storage.getItem(pref_key);
|
|
|
|
// change CSS via these <body> classes:
|
|
var dark = 'dark';
|
|
var light = 'light';
|
|
|
|
// which class is <body> set to initially?
|
|
var default_theme = light;
|
|
|
|
// use an element with class `dark-mode-toggle` to trigger swap when clicked
|
|
var toggle = window.document.querySelector('.dark-mode-toggle');
|
|
|
|
// keep track of current state no matter how we got there
|
|
var active = (default_theme === dark);
|
|
|
|
// receives a class name and switches <body> to it
|
|
var activateTheme = function (theme) {
|
|
classes.remove(dark, light);
|
|
classes.add(theme);
|
|
active = (theme === dark);
|
|
};
|
|
|
|
// if user already explicitly toggled in the past, restore their preference
|
|
if (pref === dark) activateTheme(dark);
|
|
if (pref === light) activateTheme(light);
|
|
|
|
// user has never clicked the button, so go by their OS preference until/if they do so
|
|
if (!pref) {
|
|
// returns media query selector syntax
|
|
var prefers = function (theme) {
|
|
return '(prefers-color-scheme: ' + theme + ')';
|
|
};
|
|
|
|
// check for OS dark/light mode preference and switch accordingly
|
|
// default to `default_theme` set above if unsupported
|
|
if (window.matchMedia(prefers(dark)).matches)
|
|
activateTheme(dark);
|
|
else if (window.matchMedia(prefers(light)).matches)
|
|
activateTheme(light);
|
|
else
|
|
activateTheme(default_theme);
|
|
|
|
// real-time switching if supported by OS/browser
|
|
window.matchMedia(prefers(dark)).addListener(function (e) { if (e.matches) activateTheme(dark); });
|
|
window.matchMedia(prefers(light)).addListener(function (e) { if (e.matches) activateTheme(light); });
|
|
}
|
|
|
|
// don't freak out if page happens not to have a toggle
|
|
if (toggle) {
|
|
// toggle re-appears now that we know user has JS enabled
|
|
toggle.style.visibility = 'visible';
|
|
|
|
// handle toggle click
|
|
toggle.addEventListener('click', function () {
|
|
// switch to the opposite theme & save preference in local storage
|
|
if (active) {
|
|
activateTheme(light);
|
|
storage.setItem(pref_key, light);
|
|
} else {
|
|
activateTheme(dark);
|
|
storage.setItem(pref_key, dark);
|
|
}
|
|
}, true);
|
|
}
|
|
})();
|
|
</script>
|
|
</body>
|
|
</html>
|