-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
302747b
commit fca2c0f
Showing
9 changed files
with
354 additions
and
66 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,17 @@ | ||
import { useState, useEffect } from 'react'; | ||
import { useEffect } from 'react'; | ||
import { RootStateOrAny, useSelector } from "react-redux"; | ||
|
||
//@ts-ignore | ||
export default function ThemeProvider({ children }) { | ||
const [theme, setTheme] = useState(''); | ||
|
||
function changeTheme(theme: string) { | ||
const localStorageTheme = localStorage.getItem('theme'); | ||
if (localStorageTheme) setTheme(localStorageTheme); | ||
setTheme(theme) | ||
localStorage.setItem('theme', theme); | ||
} | ||
|
||
useEffect(() => { | ||
changeTheme(localStorage.getItem('theme') || '') | ||
}, []); | ||
const theme = useSelector((state: RootStateOrAny) => state.theme.theme); | ||
|
||
useEffect(() => { | ||
localStorage.setItem('theme', theme); | ||
localStorage.setItem('theme', theme ); | ||
}, [theme]); | ||
|
||
return ( | ||
<div className={`${theme}`}> | ||
{children} | ||
<div className="flex justify-center"> | ||
<div className="bg-gray-200 m-4 p-4 rounded"> | ||
<button | ||
className="" | ||
onClick={() => { | ||
changeTheme('') | ||
}}> | ||
Theme Root | ||
</button> | ||
</div> | ||
<div className="pink bg-gray-200 m-4 p-4 rounded"> | ||
<button | ||
className="text-primary-default" | ||
onClick={() => { | ||
changeTheme('pink') | ||
}}> | ||
Pink | ||
</button> | ||
</div> | ||
<div className="dark bg-gray-200 m-4 p-4 rounded"> | ||
<button | ||
className="text-primary-default" | ||
onClick={() => { | ||
changeTheme('dark') | ||
}}> | ||
Dark | ||
</button> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
export default function Home() { | ||
|
||
return ( | ||
<div className="flex justify-center"> | ||
<h3 className="text-primary-default">Hello</h3> | ||
<div> | ||
<h3 className="text-primary-default">This is the homepage</h3> | ||
</div> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useDispatch } from "react-redux"; | ||
import { changeTheme } from "store/theme"; | ||
|
||
export default function Home() { | ||
const dispatch = useDispatch(); | ||
|
||
return ( | ||
<div className="flex justify-center"> | ||
<button className="mx-2 bg-gray-300 px-3 py-2 rounded" onClick={() => { | ||
dispatch(changeTheme('')) | ||
}} | ||
>Base</button> | ||
<button className="dark mx-2 bg-gray-300 px-3 py-2 rounded" onClick={() => { | ||
dispatch(changeTheme('dark')) | ||
}} | ||
>Dark</button> | ||
<button className="pink mx-2 bg-gray-300 px-3 py-2 rounded" onClick={() => { | ||
dispatch(changeTheme('pink')) | ||
}} | ||
>Pink</button> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { configureStore } from '@reduxjs/toolkit'; | ||
import themeReducer from './theme'; | ||
|
||
export default configureStore({ | ||
reducer: { | ||
theme: themeReducer, | ||
}, | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { createSlice, PayloadAction } from "@reduxjs/toolkit"; | ||
|
||
export interface ThemeState { | ||
theme: string; | ||
} | ||
|
||
const initialState: ThemeState = { | ||
theme: 'dark', | ||
}; | ||
|
||
export const themeSlice = createSlice({ | ||
name: "theme", | ||
initialState, | ||
reducers: { | ||
changeTheme: (state, action: PayloadAction<string>) => { | ||
state.theme = action.payload; | ||
localStorage.setItem('theme', action.payload); | ||
}, | ||
}, | ||
}); | ||
|
||
// Action creators are generated for each case reducer function | ||
export const { changeTheme } = themeSlice.actions; | ||
|
||
export default themeSlice.reducer; |