Skip to content

Commit

Permalink
modify CssBaseline import and add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
amandaguan-ag committed Mar 31, 2024
1 parent 1b03fc3 commit 54abe7c
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/components/ThemeContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,32 @@ import { createContext, useContext, useState } from 'react';
import {
createTheme,
ThemeProvider as MUIThemeProvider,
CssBaseline,
} 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 />
Expand Down

0 comments on commit 54abe7c

Please sign in to comment.