Skip to content

Commit

Permalink
persist theme selection when navigating
Browse files Browse the repository at this point in the history
  • Loading branch information
filipKovachev committed Dec 1, 2024
1 parent 1e517c3 commit 1df097a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
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,
Expand All @@ -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);
}
};

Expand Down
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);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
z-index: 1000;
}

.sized-parent{
body.sized-parent {
max-width: 1280px;
margin: auto;
}
Expand Down

0 comments on commit 1df097a

Please sign in to comment.