Skip to content

Commit 1df097a

Browse files
committed
persist theme selection when navigating
1 parent 1e517c3 commit 1df097a

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

examples/kendo-react-e-commerce-astro-app/src/components/Header.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from "react";
1+
import React, { useState, useEffect } from "react";
22
import {
33
Menu,
44
MenuSelectEvent,
@@ -20,16 +20,30 @@ interface CustomMenuItemModel extends MenuItemModel {
2020

2121
const Header: React.FC = () => {
2222
const isAdminValue = useStore(isAdmin);
23-
const [theme, setTheme] = useState(
24-
"https://unpkg.com/@progress/[email protected]/dist/default-main.css"
23+
24+
const [theme, setTheme] = useState<string>(() =>
25+
typeof window !== "undefined"
26+
? localStorage.getItem("theme") ||
27+
"https://unpkg.com/@progress/[email protected]/dist/default-main.css"
28+
: "https://unpkg.com/@progress/[email protected]/dist/default-main.css"
2529
);
2630

31+
useEffect(() => {
32+
const themeLink = document.getElementById("theme-link") as HTMLLinkElement;
33+
if (themeLink) {
34+
themeLink.href = theme;
35+
} else {
36+
console.error("Theme <link> tag not found");
37+
}
38+
}, [theme]);
39+
2740
const handleThemeChange = (event: any) => {
2841
const selectedTheme = themeItems.find(
2942
(item) => item.themeName === event.item.themeName
3043
);
3144
if (selectedTheme) {
32-
setTheme(selectedTheme.link);
45+
setTheme(selectedTheme.link);
46+
localStorage.setItem("theme", selectedTheme.link);
3347
}
3448
};
3549

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { atom } from 'nanostores';
2+
3+
export const themeStore = atom<string>(
4+
typeof window !== 'undefined'
5+
? localStorage.getItem('theme') ||
6+
'https://unpkg.com/@progress/[email protected]/dist/default-main.css'
7+
: 'https://unpkg.com/@progress/[email protected]/dist/default-main.css'
8+
);
9+
10+
export const setTheme = (newTheme: string) => {
11+
themeStore.set(newTheme);
12+
if (typeof window !== 'undefined') {
13+
localStorage.setItem('theme', newTheme);
14+
}
15+
};

examples/kendo-react-e-commerce-astro-app/src/layouts/Layout.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
z-index: 1000;
6060
}
6161

62-
.sized-parent{
62+
body.sized-parent {
6363
max-width: 1280px;
6464
margin: auto;
6565
}

0 commit comments

Comments
 (0)