server rendering improvements
This commit is contained in:
@@ -3,13 +3,28 @@ import { get, writable, type Writable } from "svelte/store";
|
||||
|
||||
type Theme = "dark" | "light"
|
||||
|
||||
export const theme: Writable<Theme> = writable(browser && localStorage.getItem("theme") as Theme || "light" );
|
||||
export const theme: Writable<Theme> = writable(getDefaultTheme());
|
||||
|
||||
function getDefaultTheme(): Theme {
|
||||
if (browser) {
|
||||
if (localStorage.getItem("theme")) {
|
||||
return localStorage.getItem("theme") as Theme;
|
||||
} else if (document.documentElement.classList.contains("light")) {
|
||||
return "light"
|
||||
} else if (document.documentElement.classList.contains("dark")) {
|
||||
return "dark";
|
||||
}
|
||||
}
|
||||
|
||||
return "light";
|
||||
|
||||
}
|
||||
|
||||
export function toggleTheme() {
|
||||
const currentTheme = get(theme);
|
||||
const newTheme = currentTheme === "light" ? "dark" : "light";
|
||||
document.documentElement.classList.remove(currentTheme)
|
||||
document.documentElement.classList.add(newTheme)
|
||||
document.documentElement.classList.add(newTheme)
|
||||
theme.set(newTheme)
|
||||
localStorage.setItem("theme", newTheme);
|
||||
}
|
||||
Reference in New Issue
Block a user