Last active
March 8, 2022 01:50
-
-
Save saiwolf/6e766e78690e00163ec555be78132d16 to your computer and use it in GitHub Desktop.
TS Dark Mode Theme
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
interface ITheme { | |
mode: 'dark' | 'light' | 'hiContrast'; | |
}; | |
const defaultTheme: ITheme = { | |
mode: 'light', | |
}; | |
if (!localStorage.getItem('theme')) { | |
const themeJSON = JSON.stringify(defaultTheme); | |
localStorage.setItem('theme', themeJSON); | |
} | |
function setTheme(themeOptions: ITheme) { | |
switch (themeOptions.mode) { | |
case 'dark': | |
document.body.classList.remove('light', 'hiContrast'); | |
document.body.classList.add('dark'); | |
break; | |
case 'light': | |
document.body.classList.remove('dark', 'hiContrast'); | |
document.body.classList.add('light'); | |
break; | |
case 'hiContrast': | |
document.body.classList.remove('dark', 'light'); | |
document.body.classList.add('hiContrast'); | |
break; | |
default: | |
document.body.classList.remove('dark', 'light', 'hiContrast'); | |
}; | |
console.log(document.body.classList.toString()); | |
} | |
const themeOptions: ITheme = JSON.parse(localStorage.getItem('theme')!); | |
setTheme(themeOptions); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Playground Link