forked from dungeon-revealer/dungeon-revealer
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add ThemeContext for dark mode support with
- Loading branch information
1 parent
a8cdc1d
commit bee1eb9
Showing
1 changed file
with
37 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React, { createContext, useContext, ReactNode } from "react"; | ||
import { usePersistedState } from "../hooks/use-persisted-state"; | ||
|
||
type ThemeContextType = { | ||
theme: string; | ||
toggleTheme: () => void; | ||
}; | ||
|
||
const defaultState: ThemeContextType = { | ||
theme: "light", | ||
toggleTheme: () => {}, | ||
}; | ||
|
||
const ThemeContext = createContext<ThemeContextType>(defaultState); | ||
|
||
interface ThemeProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
export const ThemeProvider = ({ children }: ThemeProviderProps) => { | ||
const [theme, setTheme] = usePersistedState<string>("theme", { | ||
encode: JSON.stringify, | ||
decode: JSON.parse, | ||
}); | ||
|
||
const toggleTheme = () => { | ||
setTheme((prevTheme) => (prevTheme === "light" ? "dark" : "light")); | ||
}; | ||
|
||
return ( | ||
<ThemeContext.Provider value={{ theme, toggleTheme }}> | ||
{children} | ||
</ThemeContext.Provider> | ||
); | ||
}; | ||
|
||
export const useTheme = () => useContext(ThemeContext); |