diff options
author | TinWoodman92 <chrhodgden@gmail.com> | 2023-12-15 18:55:05 -0600 |
---|---|---|
committer | TinWoodman92 <chrhodgden@gmail.com> | 2023-12-15 18:55:05 -0600 |
commit | 6ffde20364ce3b56c0f4bacdc440a646a8af4cc0 (patch) | |
tree | 5dcb426305ebd2d294fab50b24dc6656486cbbd1 /app.js | |
parent | cdaf4b1fd9b2213669b0d8bf06207efefedb6f17 (diff) |
added dark mode toggle
Diffstat (limited to 'app.js')
-rw-r--r-- | app.js | 27 |
1 files changed, 26 insertions, 1 deletions
@@ -1 +1,26 @@ -console.log('Hello from app.js!');
\ No newline at end of file +console.log('Hello from app.js!'); + +const toggleTheme = function() { + document.documentElement.style.setProperty(this.id, Number(this.checked)); + localStorage.setItem(this.id, Number(this.checked)); +}; + +const initInput = function(styleVariable=null) { + let initValue = localStorage.getItem(styleVariable); + if (initValue) { + initValue = Number(initValue); + document.documentElement.style.setProperty(styleVariable, initValue); + } else { + initValue = getComputedStyle(document.body).getPropertyValue(styleVariable); + }; + + const inputObject = document.getElementById(styleVariable); + if (inputObject) { + inputObject.addEventListener('change', toggleTheme); + if (inputObject.type == 'checkbox') { + inputObject.checked = initValue + }; + }; +}; + +initInput('--dark-theme-check'); |