Skip to content

Commit 78fa7d8

Browse files
authored
Merge pull request #22 from 0ME9A/themeFix
Theme toggle fix v2
2 parents 9e78f73 + 0bc707d commit 78fa7d8

File tree

7 files changed

+41
-60
lines changed

7 files changed

+41
-60
lines changed

src/RTK/slices/themeSlice.tsx

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/RTK/store.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ import { configureStore } from "@reduxjs/toolkit";
33
import { githubApi } from "./RTKQuery/devQuery";
44

55
// import devsHistorySlice from "./slices/devsHistorySlice";
6-
import themeSlice from "./slices/themeSlice";
76

87
const reducer = {
9-
theme: themeSlice,
108
[githubApi.reducerPath]: githubApi.reducer,
119
// devsHistory: devsHistorySlice,
1210
};

src/app/ClientLayout.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
import { Provider } from "react-redux";
33
import { store } from "@/RTK/store";
44
import { ReactNode } from "react";
5+
import useTheme from "@/hooks/useTheme";
56

6-
import Theme from "@/components/InitialState";
77

88
export const ClientLayout = ({ children }: { children: ReactNode }) => {
9+
useTheme();
10+
911
return (
1012
<Provider store={store}>
11-
<Theme />
1213
<main className="container mx-auto">{children}</main>
1314
</Provider>
1415
);

src/components/InitialState.tsx

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/components/dev/DevProfileSSR.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import { NotFound } from "../errors/NotFound";
33
import { devFace } from "@/types/devFace";
44
import { useEffect } from "react";
5-
65
import setDevToLocalstorage from "@/utils/setDevToLocalstorage";
76
import isValidGitHubUserId from "@/utils/isValidGitHubUserId";
87
import Searchbox from "../Searchbox";

src/components/menu/Menu.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
"use client";
2-
import { useDispatch, useSelector } from "react-redux";
32
import { MdOutlineLightMode } from "react-icons/md";
4-
import { setTheme } from "@/RTK/slices/themeSlice";
53
import { FiMoon } from "react-icons/fi";
6-
import { RootState } from "@/RTK/store";
7-
import { theme } from "@/utils/theme";
8-
4+
import useTheme from "@/hooks/useTheme";
95
import Image from "next/image";
106
import Link from "next/link";
117

128
const Menu = () => {
13-
const { dark } = useSelector((state: RootState) => state.theme);
14-
const dispatch = useDispatch();
9+
const [theme, setTheme] = useTheme()
1510

1611
return (
1712
<nav className="w-full flex items-center justify-between p-1">
@@ -30,11 +25,11 @@ const Menu = () => {
3025
<button
3126
type="button"
3227
title="Switch theme"
33-
onClick={() => dispatch(setTheme(theme()))}
28+
onClick={setTheme}
3429
className="flex items-center justify-center gap-2 px-2 py-1 border-transparent border-2 hover:border-black dark:hover:border-white rounded-full"
3530
>
36-
<span className="text-xs">{!dark ? "Dark" : "Light"}</span>
37-
{!dark ? <FiMoon /> : <MdOutlineLightMode />}
31+
<span className="text-xs">{theme === "dark" ? "Light" : "Dark"}</span>
32+
{theme === "dark" ? <MdOutlineLightMode /> : <FiMoon />}
3833
</button>
3934
</nav>
4035
);

src/hooks/useTheme.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { useEffect, useState } from "react";
2+
3+
function useTheme(): ["dark" | "light", () => void] {
4+
const [themeVal, setThemeVal] = useState<"dark" | "light">("dark");
5+
6+
useEffect(() => {
7+
const savedMode = localStorage.getItem("theme");
8+
if (savedMode === "dark") {
9+
document.documentElement.classList.add("dark");
10+
setThemeVal("dark");
11+
} else {
12+
document.documentElement.classList.remove("dark");
13+
setThemeVal("light");
14+
}
15+
}, []);
16+
17+
const setTheme = () => {
18+
const savedMode = localStorage.getItem("theme");
19+
if (savedMode === "dark") {
20+
document.documentElement.classList.remove("dark");
21+
localStorage.setItem("theme", "light");
22+
setThemeVal("light");
23+
} else {
24+
document.documentElement.classList.add("dark");
25+
localStorage.setItem("theme", "dark");
26+
setThemeVal("dark");
27+
}
28+
};
29+
30+
return [themeVal, setTheme];
31+
}
32+
33+
export default useTheme;

0 commit comments

Comments
 (0)