Init rebase to Svelte and TS
This commit is contained in:
17
src/lib/stores/theme.ts
Normal file
17
src/lib/stores/theme.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { writable } from 'svelte/store';
|
||||
|
||||
function getInitalTheme(): string {
|
||||
if (typeof window === 'undefined') return 'dark';
|
||||
|
||||
const savedTheme: string | null = localStorage.getItem('theme');
|
||||
if (savedTheme === 'dark' || savedTheme === 'light') return savedTheme;
|
||||
|
||||
const sysPrefTheme = window.matchMedia('(prefers-color-scheme: dark)').matches;
|
||||
return sysPrefTheme ? 'dark' : 'light';
|
||||
}
|
||||
|
||||
export const theme = writable(getInitalTheme());
|
||||
|
||||
theme.subscribe((value) => {
|
||||
if (typeof window !== 'undefined') localStorage.setItem('theme', value);
|
||||
});
|
||||
Reference in New Issue
Block a user