Skip to content

Commit

Permalink
Merge pull request #406 from pocivaneuh/dark-mode-improvements
Browse files Browse the repository at this point in the history
Dark mode improvements
  • Loading branch information
sylhare authored Nov 9, 2023
2 parents 6c9d166 + 381f74d commit f531e82
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
5 changes: 3 additions & 2 deletions assets/js/main.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions assets/js/partials/dark-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ const themeButton = {
'dark': '<i class="fas fa-adjust fa-rotate-180" aria-hidden="true"></i>'
}

const currentTheme = () => sessionStorage.getItem('theme')
const currentTheme = () => localStorage.getItem('theme')

function setMode(theme) {
document.documentElement.setAttribute('data-theme', theme)
sessionStorage.setItem('theme', theme)
localStorage.setItem('theme', theme)
const toggle = document.getElementById('theme-toggle')
if (toggle) {
toggle.innerHTML = themeButton[theme]
Expand All @@ -30,13 +30,14 @@ function themeToggle() {
}
}

window.onload = function bootstrapTheme() {
function bootstrapTheme() {
if (isAutoTheme) {
if (!currentTheme()) {
// Load browser's preference
let browserPrefersDark = window.matchMedia('(prefers-color-scheme: dark)');
if (browserPrefersDark.matches) localStorage.setItem('theme', 'dark');
browserPrefersDark.addEventListener('change', () => {
if (browserPrefersDark.matches) sessionStorage.setItem('theme', 'dark')
if (browserPrefersDark.matches) localStorage.setItem('theme', 'dark')
});
}

Expand All @@ -45,3 +46,8 @@ window.onload = function bootstrapTheme() {
setMode(sessionPrefers ? sessionPrefers : 'light')
}
}

// Init
(function () {
bootstrapTheme();
})()

0 comments on commit f531e82

Please sign in to comment.