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 @@ + + + + + + + {% if page.title %} diff --git a/src/jekyll/_includes/header.html b/src/jekyll/_includes/header.html index 76d5046a73..eb568f53e8 100644 --- a/src/jekyll/_includes/header.html +++ b/src/jekyll/_includes/header.html @@ -9,6 +9,13 @@ <nav class="header-nav"> <ul class="main-nav"> + <li class="nav-item dark-mode-icon"> + <span> + <a class="dark-mode-button" style="text-decoration: none; cursor: pointer;"> + <i id="icon-dark-mode" class="fa fa-sun"></i> + </a> + </span> + </li> <li class="nav-item"> <a href="{{ site.baseurl }}/"> <span>{{ site.t[page.lang].nav.home }}</span> diff --git a/src/sass/_header.scss b/src/sass/_header.scss index 3fa04e966c..c7333ab1cf 100644 --- a/src/sass/_header.scss +++ b/src/sass/_header.scss @@ -34,7 +34,7 @@ margin: 0; padding: 0; list-style: none; - font-size: 14px; + font-size: 16px; .nav-item { display: inline-block; padding: 0 8px; @@ -44,6 +44,9 @@ display: block; } } + .dark-mode-icon { + font-size: 20px; + } } .submenu {