Skip to content

Commit

Permalink
fixed the theme provider
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDokT0r committed Oct 20, 2024
1 parent e7a85f8 commit 28e3bef
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 30 deletions.
10 changes: 5 additions & 5 deletions services/frontend/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { Roboto } from "next/font/google";
import localFont from "next/font/local";
import { AppRouterCacheProvider } from "@mui/material-nextjs/v14-appRouter";
import "./globals.css";
import { CssBaseline, ThemeProvider } from "@mui/material";
import { darkTheme } from "@/utils/theme";
import { CssBaseline } from "@mui/material";
import TopAppBar from "@/components/top-app-bar/top-app-bar";
import { ThemeLayoutProvider } from "@/stores/theme-store";

const geistSans = localFont({
src: "./fonts/GeistVF.woff",
Expand All @@ -26,7 +26,7 @@ const roboto = Roboto({
});

export const metadata: Metadata = {
title: "Create Next App",
title: "Game Tracker",
description: "Generated by create next app",
};

Expand All @@ -41,11 +41,11 @@ export default function RootLayout({
className={`${geistSans.variable} ${geistMono.variable} ${roboto.variable} antialiased`}
>
<AppRouterCacheProvider>
<ThemeProvider theme={darkTheme}>
<ThemeLayoutProvider>
<CssBaseline />
<TopAppBar />
{children}
</ThemeProvider>
</ThemeLayoutProvider>
</AppRouterCacheProvider>
</body>
</html>
Expand Down
30 changes: 30 additions & 0 deletions services/frontend/src/components/theme/theme-provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use client";

import useTheme from "@/stores/theme-store";
import { ThemeProvider } from "@emotion/react";

export const darkTheme = createTheme({
palette: {
mode: "dark",
},
});

export const lightTheme = createTheme({
palette: {
mode: "light",
},
});

export default function ThemeLayoutProvider({
children,
}: {
children: React.ReactNode;
}) {
const { theme } = useTheme();

return (
<ThemeProvider theme={theme === "dark" ? lightTheme : darkTheme}>
{children}
</ThemeProvider>
);
}
22 changes: 18 additions & 4 deletions services/frontend/src/stores/theme-store.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use client";
import { createTheme } from "@mui/material";
import { createTheme, ThemeProvider } from "@mui/material";
import { create } from "zustand";

type AppTheme = "light" | "dark";
Expand All @@ -13,9 +13,9 @@ interface ThemeStoreProps {
const STORAGE_KEY = "app_theme";

const getInitialTheme = (): AppTheme => {
const storageTheme = localStorage.getItem(STORAGE_KEY) as AppTheme | null;
const storageTheme = window.localStorage.getItem(STORAGE_KEY) as AppTheme | null;
if (!storageTheme) {
localStorage.setItem(STORAGE_KEY, "dark");
window.localStorage.setItem(STORAGE_KEY, "dark");
return "dark";
}

Expand All @@ -26,7 +26,7 @@ const useTheme = create<ThemeStoreProps>((set, get) => ({
theme: getInitialTheme(),
setTheme: (newTheme) => {
set({ theme: newTheme });
localStorage.setItem(STORAGE_KEY, newTheme);
window.localStorage.setItem(STORAGE_KEY, newTheme);
},
switchTheme: () => {
if (get().theme === "dark") {
Expand All @@ -50,3 +50,17 @@ export const lightTheme = createTheme({
mode: "light",
},
});

export function ThemeLayoutProvider({
children,
}: {
children: React.ReactNode;
}) {
const { theme } = useTheme();

return (
<ThemeProvider theme={theme === "dark" ? lightTheme : darkTheme}>
{children}
</ThemeProvider>
);
}
21 changes: 0 additions & 21 deletions services/frontend/src/utils/theme.ts

This file was deleted.

0 comments on commit 28e3bef

Please sign in to comment.