Skip to content

Instantly share code, notes, and snippets.

@saiwolf
Last active March 8, 2022 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saiwolf/6e766e78690e00163ec555be78132d16 to your computer and use it in GitHub Desktop.
Save saiwolf/6e766e78690e00163ec555be78132d16 to your computer and use it in GitHub Desktop.
TS Dark Mode Theme
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);
@saiwolf
Copy link
Author

saiwolf commented Mar 8, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment