-
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.
add change theme dark/light button with react redux
- Loading branch information
Showing
7 changed files
with
62 additions
and
3 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useDispatch } from "react-redux" | ||
import { toggleTheme } from "../store/themeSlice" | ||
|
||
function ThemeButton() { | ||
const dispatch = useDispatch() | ||
|
||
return ( | ||
<navbar className="absolute right-8"> | ||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="size-6 hover:cursor-pointer" onClick={() => dispatch(toggleTheme())}> | ||
<path strokeLinecap="round" strokeLinejoin="round" d="M12 3v2.25m6.364.386-1.591 1.591M21 12h-2.25m-.386 6.364-1.591-1.591M12 18.75V21m-4.773-4.227-1.591 1.591M5.25 12H3m4.227-4.773L5.636 5.636M15.75 12a3.75 3.75 0 1 1-7.5 0 3.75 3.75 0 0 1 7.5 0Z" /> | ||
</svg> | ||
</navbar> | ||
) | ||
} | ||
|
||
export default ThemeButton |
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,10 +1,16 @@ | ||
import { StrictMode } from 'react' | ||
import { createRoot } from 'react-dom/client' | ||
|
||
import { Provider } from 'react-redux' | ||
import { store } from './store/store.js' | ||
|
||
import App from './App.jsx' | ||
import './index.css' | ||
|
||
createRoot(document.getElementById('root')).render( | ||
<StrictMode> | ||
<App /> | ||
<Provider store={store}> | ||
<App /> | ||
</Provider> | ||
</StrictMode>, | ||
) |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { configureStore } from '@reduxjs/toolkit'; | ||
import themeReducer from './themeSlice'; | ||
|
||
export const store = 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,19 @@ | ||
import { createSlice } from '@reduxjs/toolkit' | ||
|
||
const initialState = { | ||
theme: 'dark' | ||
}; | ||
|
||
const themeSlice = createSlice({ | ||
name: 'theme', | ||
initialState, | ||
reducers: { | ||
toggleTheme: (state) => { | ||
state.theme = state.theme === 'light' ? 'dark' : 'light'; | ||
} | ||
} | ||
}) | ||
|
||
export const { toggleTheme } = themeSlice.actions; | ||
|
||
export default themeSlice.reducer |