feat(ui): add light theme toggle
This commit is contained in:
parent
bbc81cea0d
commit
cdb269e505
17 changed files with 415 additions and 24 deletions
54
src/lib/ui/theme.ts
Normal file
54
src/lib/ui/theme.ts
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
export const UI_THEME_STORAGE_KEY = "dashboard-ui-theme";
|
||||
|
||||
export type UiTheme = "dark" | "light";
|
||||
|
||||
type ReadableThemeStorage =
|
||||
| { getItem(key: string): string | null }
|
||||
| { get(key: string): string | undefined };
|
||||
|
||||
type WritableThemeStorage =
|
||||
| { setItem(key: string, value: string): void }
|
||||
| { set(key: string, value: string): unknown };
|
||||
|
||||
export function isUiTheme(value: unknown): value is UiTheme {
|
||||
return value === "dark" || value === "light";
|
||||
}
|
||||
|
||||
export function getNextUiTheme(theme: UiTheme): UiTheme {
|
||||
return theme === "dark" ? "light" : "dark";
|
||||
}
|
||||
|
||||
export function resolveInitialUiTheme(
|
||||
storage?: ReadableThemeStorage | null,
|
||||
fallback: UiTheme = "dark",
|
||||
): UiTheme {
|
||||
const stored = readStoredTheme(storage);
|
||||
|
||||
return isUiTheme(stored) ? stored : fallback;
|
||||
}
|
||||
|
||||
export function persistUiTheme(
|
||||
theme: UiTheme,
|
||||
storage?: WritableThemeStorage | null,
|
||||
): void {
|
||||
if (!storage) return;
|
||||
|
||||
if ("setItem" in storage) {
|
||||
storage.setItem(UI_THEME_STORAGE_KEY, theme);
|
||||
return;
|
||||
}
|
||||
|
||||
storage.set(UI_THEME_STORAGE_KEY, theme);
|
||||
}
|
||||
|
||||
function readStoredTheme(
|
||||
storage?: ReadableThemeStorage | null,
|
||||
): string | undefined {
|
||||
if (!storage) return undefined;
|
||||
|
||||
if ("getItem" in storage) {
|
||||
return storage.getItem(UI_THEME_STORAGE_KEY) ?? undefined;
|
||||
}
|
||||
|
||||
return storage.get(UI_THEME_STORAGE_KEY);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue