-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Theme moved into own js file as of performance issues
- Loading branch information
1 parent
c1554d2
commit 844d85b
Showing
3 changed files
with
38 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const selectTheme = document.getElementById('theme-select') | ||
// set dark theme depending on OS settings | ||
function setTheme(theme) { | ||
if (!selectTheme) { | ||
console.log('Themes are currently not working with single file version.'); | ||
return; | ||
} | ||
if (theme == 'os') { | ||
let prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)'); | ||
if (prefersDarkScheme.matches) { | ||
theme = 'dark'; | ||
} else { | ||
theme = 'light'; | ||
} | ||
} | ||
document.documentElement.style['color-scheme'] = theme | ||
document.querySelector('html').setAttribute('data-theme', theme) | ||
} | ||
// activate selected theme | ||
let theme = 'os'; | ||
const localStorageTheme = localStorage.getItem('theme'); | ||
if (localStorageTheme) { | ||
theme = localStorageTheme; | ||
} | ||
if (theme == 'light') { | ||
selectTheme.selectedIndex = 2; | ||
} else if (theme == 'dark') { | ||
selectTheme.selectedIndex = 1; | ||
} | ||
setTheme(theme); | ||
// add handler to theme selction element | ||
if (selectTheme) { | ||
selectTheme.addEventListener ('change', function () { | ||
localStorage.setItem('theme', selectTheme.value); | ||
setTheme(selectTheme.value); | ||
}); | ||
} |