Skip to content

Commit

Permalink
feat(dark-mode): keep preference for next opening using local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
soullivaneuh committed Nov 7, 2023
1 parent 85ca6f1 commit 381f74d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion assets/js/main.min.js

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

8 changes: 4 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 @@ -35,9 +35,9 @@ function bootstrapTheme() {
if (!currentTheme()) {
// Load browser's preference
let browserPrefersDark = window.matchMedia('(prefers-color-scheme: dark)');
if (browserPrefersDark.matches) sessionStorage.setItem('theme', '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 Down

0 comments on commit 381f74d

Please sign in to comment.