-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Manage dark mode with the MUI library
- Loading branch information
Showing
7 changed files
with
98 additions
and
102 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
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,28 @@ | ||
'use client'; // This directive ensures the component is treated as a client component | ||
|
||
import React, { useMemo } from 'react'; | ||
import CssBaseline from '@mui/material/CssBaseline'; | ||
import { | ||
ThemeProvider as MuiThemeProvider, | ||
useMediaQuery, | ||
} from '@mui/material'; | ||
import getTheme from '@/theme'; | ||
import { useBoundStore } from './StoreProvider'; | ||
|
||
const ThemeProvider = ({ children }: { children: React.ReactNode }) => { | ||
// Detect the system preference for dark mode | ||
const prefersDarkMode = useMediaQuery('(prefers-color-scheme: dark)'); | ||
|
||
const storeTheme = useBoundStore((state) => state.theme); | ||
const colorTheme = | ||
storeTheme !== 'auto' ? storeTheme : prefersDarkMode ? 'dark' : 'light'; | ||
const theme = useMemo(() => getTheme(colorTheme), [colorTheme]); | ||
return ( | ||
<MuiThemeProvider theme={theme}> | ||
<CssBaseline /> | ||
{children} | ||
</MuiThemeProvider> | ||
); | ||
}; | ||
|
||
export default ThemeProvider; |
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 |
---|---|---|
@@ -1,28 +1,19 @@ | ||
'use client'; | ||
import { createTheme } from '@mui/material/styles'; | ||
declare module '@mui/material/styles/createPalette' { | ||
interface Palette { | ||
custom: { | ||
filledBackground: string; | ||
}; | ||
} | ||
interface PaletteOptions { | ||
custom?: { | ||
filledBackground?: string; | ||
}; | ||
} | ||
} | ||
|
||
const theme = createTheme({ | ||
palette: { | ||
primary: { | ||
main: '#7b1fa2', | ||
light: '#D7B3FF', | ||
const getTheme = (mode: 'dark' | 'light') => | ||
createTheme({ | ||
palette: { | ||
mode: mode, | ||
primary: { | ||
main: mode === 'dark' ? '#D7B3FF' : '#7b1fa2', | ||
light: mode === 'dark' ? '#7b1fa2' : '#D7B3FF', | ||
}, | ||
secondary: { | ||
main: '#7b1fa2', | ||
light: '#D7B3FF', | ||
}, | ||
grey: { 100: mode === 'dark' ? 'rgba(255, 255, 255, 0.09)' : '#f5f5f5' }, | ||
}, | ||
custom: { | ||
filledBackground: 'rgba(0, 0, 0, 0.06)', | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
export default theme; | ||
export default getTheme; |