-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update components to play well with dark theme (#10)
* chore: Add createStore function for managing state * feat: Add theme provider for managing app theme * chore: Update color palette in index.css - add dark theme color palette * chore: Update color palette in index.css * feat: update to play well with dark theme
- Loading branch information
Showing
9 changed files
with
242 additions
and
240 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
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,37 @@ | ||
import { Dispatch, PropsWithChildren, createContext, useContext, useReducer } from "react"; | ||
|
||
/** | ||
* Creates a store with the specified reducer and initial state. | ||
* | ||
* @template State - The type of the state. | ||
* @template Action - The type of the action. | ||
* @param {function(state: State, action: Action): State} reducer - The reducer function that updates the state based on the action. | ||
* @param {State} initialState - The initial state of the store. | ||
* @returns {{ StoreProvider: React.ComponentType<PropsWithChildren>, useDispatch: function(): Dispatch<Action>, useStore: function(): State }} - An object containing the StoreProvider component, useDispatch hook, and useStore hook. | ||
*/ | ||
export default function createStore<State, Action>( | ||
reducer: (state: State, action: Action) => State, | ||
initialState: State | ||
) { | ||
const StoreContext = createContext(initialState); | ||
const DispatchContext = createContext<Dispatch<Action>>(() => {}); | ||
|
||
function StoreProvider({ children }: PropsWithChildren) { | ||
const [store, dispatch] = useReducer(reducer, initialState); | ||
|
||
return ( | ||
<StoreContext.Provider value={store}> | ||
<DispatchContext.Provider value={dispatch}>{children}</DispatchContext.Provider> | ||
</StoreContext.Provider> | ||
); | ||
} | ||
|
||
function useStore() { | ||
return useContext(StoreContext); | ||
} | ||
|
||
function useDispatch() { | ||
return useContext(DispatchContext); | ||
} | ||
return { StoreProvider, useDispatch, useStore }; | ||
} |
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,15 @@ | ||
import createStore from "@/lib/makeStore"; | ||
|
||
export type Theme = "dark" | "light"; | ||
|
||
export const { | ||
StoreProvider: ThemeProvider, | ||
useDispatch: setTheme, | ||
useStore: useTheme, | ||
} = createStore( | ||
(_, next: Theme) => { | ||
localStorage.setItem("theme", next); | ||
return next; | ||
}, | ||
(localStorage.getItem("theme") === "dark" ? "dark" : "light") as Theme | ||
); |
Oops, something went wrong.