Add theme get & set functions.
This commit is contained in:
parent
98034ae621
commit
25c45d9d01
|
@ -9,11 +9,11 @@ import {Router} from 'preact-router';
|
||||||
import HomePage from './pages/home.js';
|
import HomePage from './pages/home.js';
|
||||||
import NotFoundPage from './pages/not-found.js';
|
import NotFoundPage from './pages/not-found.js';
|
||||||
import ReleasePage from './pages/release.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 = getThemeByCssClass(
|
||||||
const activeTheme =
|
window.localStorage.getItem('theme') ?? '',
|
||||||
themes.find((theme) => theme.cssClass === savedTheme) ?? themes[0];
|
);
|
||||||
|
|
||||||
document.body.classList.value = activeTheme.cssClass;
|
document.body.classList.value = activeTheme.cssClass;
|
||||||
|
|
||||||
|
|
|
@ -17,3 +17,12 @@ export const themes: Theme[] = [
|
||||||
];
|
];
|
||||||
|
|
||||||
export const themeContext = createContext<Theme>(themes[0]);
|
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);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue