mirror of
https://github.com/jcreek/LivingDexTracker.git
synced 2026-07-13 02:53:45 +00:00
feat(#48): Add dark mode
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import { writable } from 'svelte/store';
|
||||
|
||||
type Theme = 'pokeball' | 'dark';
|
||||
|
||||
const STORAGE_KEY = 'theme';
|
||||
|
||||
function createThemeStore() {
|
||||
// Get stored theme from localStorage or default to 'pokeball'
|
||||
const storedTheme = (typeof window !== 'undefined' &&
|
||||
localStorage.getItem(STORAGE_KEY)) as Theme | null;
|
||||
const initialTheme: Theme = storedTheme || 'pokeball';
|
||||
|
||||
const { subscribe, set, update } = writable<Theme>(initialTheme);
|
||||
|
||||
return {
|
||||
subscribe,
|
||||
set,
|
||||
toggle: () => update((theme) => (theme === 'pokeball' ? 'dark' : 'pokeball')),
|
||||
init: () => {
|
||||
if (typeof window !== 'undefined') {
|
||||
const stored = localStorage.getItem(STORAGE_KEY) as Theme | null;
|
||||
if (stored) {
|
||||
set(stored);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export const theme = createThemeStore();
|
||||
|
||||
// Helper function to apply theme to HTML element
|
||||
export function applyTheme(themeValue: Theme) {
|
||||
if (typeof document !== 'undefined') {
|
||||
document.documentElement.setAttribute('data-theme', themeValue);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user