Skip to content

Instantly share code, notes, and snippets.

@stockhuman
Last active October 14, 2016 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stockhuman/f4b26d58f12fd3cdca6446a697bb00f4 to your computer and use it in GitHub Desktop.
Save stockhuman/f4b26d58f12fd3cdca6446a697bb00f4 to your computer and use it in GitHub Desktop.
Sets and retrieves a key to select user UI color preference
/* Arthem Co.
* Let "doot" be a reserved private variable.
*
* Michael Hemingway 2016
*/
(function () {
'use strict';
// HELPER FUNCTIONS
var $ = function (id) {
return document.getElementById(id);
};
function setStorage() {
var a = localStorage.getItem('dark-ui'),
body = document.getElementsByTagName('body')[0],
toggle = document.getElementById("dark-ui-toggle");
if (a === "dark") {
body.className += (' dark-ui');
toggle.checked = true;
} else {
body.classList.remove('dark-ui');
toggle.checked = false;
}
}
// read key and apply/remove class with setStorage method.
document.addEventListener("DOMContentLoaded", function (event) {
$('dark-ui-toggle').addEventListener("click", function () {
var b = $('dark-ui-toggle'),
s = b.checked;
if (s !== true) {
b.checked = false;
localStorage.setItem('dark-ui', 'light');
} else {
b.checked = true;
localStorage.setItem('dark-ui', 'dark');
}
setStorage();
});
setStorage();
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment