Set and persist theme preference in localStorage

Implemented logic to store and retrieve theme preferences using localStorage for both logged-in users and guests. This ensures the selected theme is applied immediately on load, preventing theme flash issues. Adjusted initialization to apply the correct theme at app startup.
This commit is contained in:
2026-02-06 18:41:45 +01:00
parent 6434256dfb
commit 070057afb3
3 changed files with 26 additions and 3 deletions

View File

@@ -1,9 +1,17 @@
import React from "react";
import { createRoot } from "react-dom/client";
import ReactDOM from "react-dom/client";
import App from "./App.jsx";
import { applyTheme, DEFAULT_THEME_KEY } from "./styles/themes";
import { registerSW } from "virtual:pwa-register";
createRoot(document.getElementById("root")).render(<App />);
try {
const key = localStorage.getItem("hpTheme:guest") || DEFAULT_THEME_KEY;
applyTheme(key);
} catch {
applyTheme(DEFAULT_THEME_KEY);
}
ReactDOM.createRoot(document.getElementById("root")).render(<App />);
registerSW({ immediate: true });
const updateSW = registerSW({
immediate: true,