Last active
October 14, 2016 16:39
-
-
Save stockhuman/f4b26d58f12fd3cdca6446a697bb00f4 to your computer and use it in GitHub Desktop.
Sets and retrieves a key to select user UI color preference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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