bugfix: the whole theming structure overhauled. light/dark mode icon added.
This commit is contained in:
30
frontend/src/lib/theme.ts
Normal file
30
frontend/src/lib/theme.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
export const THEME_STORAGE_KEY = 'dyolink-theme';
|
||||
|
||||
export type ThemeMode = 'light' | 'dark';
|
||||
|
||||
export function getStoredTheme(): ThemeMode {
|
||||
if (typeof window === 'undefined') return 'dark';
|
||||
try {
|
||||
const v = localStorage.getItem(THEME_STORAGE_KEY);
|
||||
if (v === 'light' || v === 'dark') return v;
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
return 'dark';
|
||||
}
|
||||
|
||||
export function applyTheme(mode: ThemeMode) {
|
||||
if (typeof document === 'undefined') return;
|
||||
document.documentElement.setAttribute('data-theme', mode);
|
||||
try {
|
||||
localStorage.setItem(THEME_STORAGE_KEY, mode);
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
export function toggleTheme(): ThemeMode {
|
||||
const next: ThemeMode = getStoredTheme() === 'dark' ? 'light' : 'dark';
|
||||
applyTheme(next);
|
||||
return next;
|
||||
}
|
||||
Reference in New Issue
Block a user