Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Theme Persistence via Local Storage and UI Enhancements for Active Themes #366

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 56 additions & 48 deletions apps/frontend/src/components/themes.tsx
Original file line number Diff line number Diff line change
@@ -1,56 +1,64 @@
import { THEMES_DATA } from "@/constants/themes";
import { useThemeContext } from "@/hooks/useThemes";
import { THEMES_DATA } from '@/constants/themes';
import { useThemeContext } from '@/hooks/useThemes';
import { useState } from 'react';
import { THEME } from '@/context/themeContext';

export function Themes() {
const { updateTheme } = useThemeContext();
const { theme, updateTheme } = useThemeContext();
const [selectedTheme, setSelectedTheme] = useState<THEME>(theme);

const handleThemeClick = (themeName: THEME) => {
setSelectedTheme(themeName);
updateTheme(themeName);
};

return (
<div className="flex-1 bg-bgAuxiliary1 py-12 px-10">
<div className="grid grid-cols-1 gap-8">
{
THEMES_DATA.map(theme => {
return (
<div
key={theme.id}
className="p-4 flex items-start justify-between cursor-pointer"
style={{backgroundColor: theme.background}}
onClick={() => {
updateTheme(theme.name);
}}
>
<div>
<h2 className="text-lg capitalize">{theme.name}</h2>
</div>
<div className="grid grid-cols-2">
<img
src="/bk.png"
className="w-16 h-16"
alt="chess-piece"
style={{backgroundColor: theme["board-dark"]}}
/>
<img
src="/wn.png"
className="w-16 h-16"
alt="chess-piece"
style={{backgroundColor: theme["board-light"]}}
/>
<img
src="/br.png"
className="w-16 h-16"
alt="chess-piece"
style={{backgroundColor: theme["board-light"]}}
/>
<img
src="/wp.png"
className="w-16 h-16"
alt="chess-piece"
style={{backgroundColor: theme["board-dark"]}}
/>
</div>
{THEMES_DATA.map((themeData) => {
const isActive = themeData.name === selectedTheme;
return (
<div
key={themeData.id}
className={`p-4 flex items-start justify-between cursor-pointer border-2 ${
isActive ? 'border-blue-500' : 'border-transparent'
} rounded-md`}
style={{ backgroundColor: themeData.background }}
onClick={() => handleThemeClick(themeData.name)}
>
<div>
<h2 className="text-lg capitalize">{themeData.name}</h2>
</div>
<div className="grid grid-cols-2">
<img
src="/bk.png"
className="w-16 h-16"
alt="chess-piece"
style={{ backgroundColor: themeData['board-dark'] }}
/>
<img
src="/wn.png"
className="w-16 h-16"
alt="chess-piece"
style={{ backgroundColor: themeData['board-light'] }}
/>
<img
src="/br.png"
className="w-16 h-16"
alt="chess-piece"
style={{ backgroundColor: themeData['board-light'] }}
/>
<img
src="/wp.png"
className="w-16 h-16"
alt="chess-piece"
style={{ backgroundColor: themeData['board-dark'] }}
/>
</div>
)
})
}
</div>
);
})}
</div>
</div>
)
}
);
}
48 changes: 19 additions & 29 deletions apps/frontend/src/context/themeContext.tsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,31 @@
import { createContext, useEffect, useState } from "react";
import { createContext, useEffect, useState } from 'react';

export type THEME = "default" | "bubblegum";
export type THEME = 'default' | 'bubblegum';

export type THEME_CONTEXT = {
theme: THEME,
updateTheme: (theme: THEME) => void
}
theme: THEME;
updateTheme: (theme: THEME) => void;
};

const AVAILABLE_THEMES: THEME[] = ["default", "bubblegum"];
const AVAILABLE_THEMES: THEME[] = ['default', 'bubblegum'];

export const ThemeContext = createContext<THEME_CONTEXT | null>(null);

export function ThemesProvider({ children }: { children: React.ReactNode }) {
const [theme, setTheme] = useState<THEME>("default");

function updateTheme(theme: THEME) {
setTheme(theme);
localStorage.setItem("theme", theme);
document.querySelector("html")?.setAttribute("data-theme", theme);
const [theme, setTheme] = useState<THEME>(() => {
const storedTheme = localStorage.getItem('theme') as THEME | null;
return storedTheme && AVAILABLE_THEMES.includes(storedTheme) ? storedTheme : 'default';
});

function updateTheme(newTheme: THEME) {
setTheme(newTheme);
localStorage.setItem('theme', newTheme);
document.querySelector('html')?.setAttribute('data-theme', newTheme);
}

useEffect(() => {
const currentTheme = localStorage.getItem("theme") as THEME | null;

if(currentTheme && AVAILABLE_THEMES.includes(currentTheme)) {
setTheme(currentTheme);
document.querySelector("html")?.setAttribute("data-theme", currentTheme);
}
}, []);

return (
<ThemeContext.Provider value={{
theme,
updateTheme
}}>
{children}
</ThemeContext.Provider>
)
}
document.querySelector('html')?.setAttribute('data-theme', theme);
}, [theme]);

return <ThemeContext.Provider value={{ theme, updateTheme }}>{children}</ThemeContext.Provider>;
}