1
Fork 0

Add theme get & set functions.

This commit is contained in:
Bauke 2022-01-14 20:17:52 +01:00
parent 98034ae621
commit 25c45d9d01
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
2 changed files with 13 additions and 4 deletions

View File

@ -9,11 +9,11 @@ import {Router} from 'preact-router';
import HomePage from './pages/home.js';
import NotFoundPage from './pages/not-found.js';
import ReleasePage from './pages/release.js';
import {themeContext, themes} from './utilities/themes.js';
import {getThemeByCssClass, themeContext} from './utilities/themes.js';
const savedTheme = window.localStorage.getItem('theme');
const activeTheme =
themes.find((theme) => theme.cssClass === savedTheme) ?? themes[0];
const activeTheme = getThemeByCssClass(
window.localStorage.getItem('theme') ?? '',
);
document.body.classList.value = activeTheme.cssClass;

View File

@ -17,3 +17,12 @@ export const themes: Theme[] = [
];
export const themeContext = createContext<Theme>(themes[0]);
export function getThemeByCssClass(cssClass: string): Theme {
return themes.find((theme) => theme.cssClass === cssClass) ?? themes[0];
}
export function setTheme(theme: Theme): void {
document.body.classList.value = theme.cssClass;
window.localStorage.setItem('theme', theme.cssClass);
}