-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
persist theme selection when navigating
- Loading branch information
1 parent
1e517c3
commit 1df097a
Showing
3 changed files
with
34 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import React, { useState } from "react"; | ||
import React, { useState, useEffect } from "react"; | ||
import { | ||
Menu, | ||
MenuSelectEvent, | ||
|
@@ -20,16 +20,30 @@ interface CustomMenuItemModel extends MenuItemModel { | |
|
||
const Header: React.FC = () => { | ||
const isAdminValue = useStore(isAdmin); | ||
const [theme, setTheme] = useState( | ||
"https://unpkg.com/@progress/[email protected]/dist/default-main.css" | ||
|
||
const [theme, setTheme] = useState<string>(() => | ||
typeof window !== "undefined" | ||
? localStorage.getItem("theme") || | ||
"https://unpkg.com/@progress/[email protected]/dist/default-main.css" | ||
: "https://unpkg.com/@progress/[email protected]/dist/default-main.css" | ||
); | ||
|
||
useEffect(() => { | ||
const themeLink = document.getElementById("theme-link") as HTMLLinkElement; | ||
if (themeLink) { | ||
themeLink.href = theme; | ||
} else { | ||
console.error("Theme <link> tag not found"); | ||
} | ||
}, [theme]); | ||
|
||
const handleThemeChange = (event: any) => { | ||
const selectedTheme = themeItems.find( | ||
(item) => item.themeName === event.item.themeName | ||
); | ||
if (selectedTheme) { | ||
setTheme(selectedTheme.link); | ||
setTheme(selectedTheme.link); | ||
localStorage.setItem("theme", selectedTheme.link); | ||
} | ||
}; | ||
|
||
|
15 changes: 15 additions & 0 deletions
15
examples/kendo-react-e-commerce-astro-app/src/helpers/themeStore.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { atom } from 'nanostores'; | ||
|
||
export const themeStore = atom<string>( | ||
typeof window !== 'undefined' | ||
? localStorage.getItem('theme') || | ||
'https://unpkg.com/@progress/[email protected]/dist/default-main.css' | ||
: 'https://unpkg.com/@progress/[email protected]/dist/default-main.css' | ||
); | ||
|
||
export const setTheme = (newTheme: string) => { | ||
themeStore.set(newTheme); | ||
if (typeof window !== 'undefined') { | ||
localStorage.setItem('theme', newTheme); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,7 +59,7 @@ | |
z-index: 1000; | ||
} | ||
|
||
.sized-parent{ | ||
body.sized-parent { | ||
max-width: 1280px; | ||
margin: auto; | ||
} | ||
|