generated from the-collab-lab/smart-shopping-list
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from the-collab-lab/ag-dark-light-mode
Add ThemeContext and toggle theme functionality
- Loading branch information
Showing
5 changed files
with
82 additions
and
72 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,38 @@ | ||
import { createContext, useContext, useState } from 'react'; | ||
import { | ||
createTheme, | ||
ThemeProvider as MUIThemeProvider, | ||
} from '@mui/material/styles'; | ||
import { CssBaseline } from '@mui/material'; | ||
// uses React's Context API to create a global theme state | ||
const ThemeContext = createContext({ | ||
// prevent error of attempt to use toggleColorMode without the ThemeProvider properly set up | ||
toggleColorMode: () => {}, | ||
}); | ||
// custom hook | ||
export const useThemeContext = () => useContext(ThemeContext); | ||
|
||
export const ThemeProvider = ({ children }) => { | ||
// state for theme mode | ||
const [mode, setMode] = useState('light'); | ||
// theme mode toggle function | ||
const toggleColorMode = () => { | ||
setMode((prevMode) => (prevMode === 'light' ? 'dark' : 'light')); | ||
}; | ||
// MUI theme creation | ||
const theme = createTheme({ | ||
palette: { | ||
mode, | ||
}, | ||
}); | ||
|
||
return ( | ||
// Providing the Context and Theme | ||
<ThemeContext.Provider value={{ toggleColorMode }}> | ||
<MUIThemeProvider theme={theme}> | ||
<CssBaseline /> | ||
{children} | ||
</MUIThemeProvider> | ||
</ThemeContext.Provider> | ||
); | ||
}; |
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