diff --git a/src/assets/js/color-scheme.js b/src/assets/js/color-scheme.js new file mode 100644 index 0000000000..004c766805 --- /dev/null +++ b/src/assets/js/color-scheme.js @@ -0,0 +1,76 @@ +// Setup darkreader for CORS +DarkReader.setFetchMethod(url => { + let headers = new Headers(); + headers.append('Access-Control-Allow-Origin', '*'); + + return window.fetch(url, { + headers, + mode: 'no-cors', + }); +}); + +function darkModeSelected() { + let darkModeSelected = localStorage.getItem('darkMode'); + + if (darkModeSelected === 'false') { + return false; + } + if (darkModeSelected === 'true') { + return true; + } + return darkModeSelected; +} + +function darkModeEnabled() { + if (darkModeSelected() === true) { + return true; + } + if (darkModeSelected() === null && darkModePreferred === true) { + return true; + } + return false; +} + +// set color mode and icons on page load +function checkDarkMode() { + if (darkModeEnabled()) { + DarkReader.enable(); + } else { + DarkReader.disable(); + } + toggleIcon(); +} + +function toggleDarkMode() { + if (darkModeEnabled()) { + disableDarkMode(); + } else { + enableDarkMode(); + } +} + +function enableDarkMode() { + DarkReader.enable(); + localStorage.setItem('darkMode', 'true'); + toggleIcon(); +} + +function disableDarkMode() { + DarkReader.disable(); + localStorage.setItem('darkMode', 'false'); + toggleIcon(); +} + +function toggleIcon() { + if (iconDarkMode.classList.contains('fa-sun')) { + // Change to fa-moon + iconDarkMode.classList.remove('fa-sun'); + iconDarkMode.classList.add('fa-moon'); + iconDarkMode.style.color = 'blue'; + } else { + // Change to fa-sun + iconDarkMode.classList.remove('fa-moon'); + iconDarkMode.classList.add('fa-sun'); + iconDarkMode.style.color = 'yellow'; + } +} diff --git a/src/jekyll/_includes/footer.html b/src/jekyll/_includes/footer.html index c7d3b54dc0..c0fb03f741 100644 --- a/src/jekyll/_includes/footer.html +++ b/src/jekyll/_includes/footer.html @@ -36,4 +36,21 @@ + + + diff --git a/src/jekyll/_includes/head.html b/src/jekyll/_includes/head.html index e226df8cbf..5b6f4b0c10 100644 --- a/src/jekyll/_includes/head.html +++ b/src/jekyll/_includes/head.html @@ -21,6 +21,13 @@ + + + + + + +