File tree Expand file tree Collapse file tree 7 files changed +41
-60
lines changed Expand file tree Collapse file tree 7 files changed +41
-60
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -3,10 +3,8 @@ import { configureStore } from "@reduxjs/toolkit";
3
3
import { githubApi } from "./RTKQuery/devQuery" ;
4
4
5
5
// import devsHistorySlice from "./slices/devsHistorySlice";
6
- import themeSlice from "./slices/themeSlice" ;
7
6
8
7
const reducer = {
9
- theme : themeSlice ,
10
8
[ githubApi . reducerPath ] : githubApi . reducer ,
11
9
// devsHistory: devsHistorySlice,
12
10
} ;
Original file line number Diff line number Diff line change 2
2
import { Provider } from "react-redux" ;
3
3
import { store } from "@/RTK/store" ;
4
4
import { ReactNode } from "react" ;
5
+ import useTheme from "@/hooks/useTheme" ;
5
6
6
- import Theme from "@/components/InitialState" ;
7
7
8
8
export const ClientLayout = ( { children } : { children : ReactNode } ) => {
9
+ useTheme ( ) ;
10
+
9
11
return (
10
12
< Provider store = { store } >
11
- < Theme />
12
13
< main className = "container mx-auto" > { children } </ main >
13
14
</ Provider >
14
15
) ;
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 2
2
import { NotFound } from "../errors/NotFound" ;
3
3
import { devFace } from "@/types/devFace" ;
4
4
import { useEffect } from "react" ;
5
-
6
5
import setDevToLocalstorage from "@/utils/setDevToLocalstorage" ;
7
6
import isValidGitHubUserId from "@/utils/isValidGitHubUserId" ;
8
7
import Searchbox from "../Searchbox" ;
Original file line number Diff line number Diff line change 1
1
"use client" ;
2
- import { useDispatch , useSelector } from "react-redux" ;
3
2
import { MdOutlineLightMode } from "react-icons/md" ;
4
- import { setTheme } from "@/RTK/slices/themeSlice" ;
5
3
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" ;
9
5
import Image from "next/image" ;
10
6
import Link from "next/link" ;
11
7
12
8
const Menu = ( ) => {
13
- const { dark } = useSelector ( ( state : RootState ) => state . theme ) ;
14
- const dispatch = useDispatch ( ) ;
9
+ const [ theme , setTheme ] = useTheme ( )
15
10
16
11
return (
17
12
< nav className = "w-full flex items-center justify-between p-1" >
@@ -30,11 +25,11 @@ const Menu = () => {
30
25
< button
31
26
type = "button"
32
27
title = "Switch theme"
33
- onClick = { ( ) => dispatch ( setTheme ( theme ( ) ) ) }
28
+ onClick = { setTheme }
34
29
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"
35
30
>
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 /> }
38
33
</ button >
39
34
</ nav >
40
35
) ;
Original file line number Diff line number Diff line change
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 ;
You can’t perform that action at this time.
0 commit comments