-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] Add example with switch dark/light mode (#25823)
- Loading branch information
Showing
3 changed files
with
134 additions
and
4 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,62 @@ | ||
import * as React from 'react'; | ||
import IconButton from '@material-ui/core/IconButton'; | ||
import Box from '@material-ui/core/Box'; | ||
import { useTheme, ThemeProvider, createMuiTheme } from '@material-ui/core/styles'; | ||
import Brightness4Icon from '@material-ui/icons/Brightness4'; | ||
import Brightness7Icon from '@material-ui/icons/Brightness7'; | ||
|
||
const ColorModeContext = React.createContext({ toggleColorMode: () => {} }); | ||
|
||
function MyApp() { | ||
const theme = useTheme(); | ||
const colorMode = React.useContext(ColorModeContext); | ||
return ( | ||
<Box | ||
sx={{ | ||
display: 'flex', | ||
width: '100%', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
bgcolor: 'background.default', | ||
color: 'text.primary', | ||
borderRadius: 1, | ||
p: 3, | ||
}} | ||
> | ||
{theme.palette.mode} mode | ||
<IconButton sx={{ ml: 1 }} onClick={colorMode.toggleColorMode} color="inherit"> | ||
{theme.palette.mode === 'dark' ? <Brightness7Icon /> : <Brightness4Icon />} | ||
</IconButton> | ||
</Box> | ||
); | ||
} | ||
|
||
export default function ToggleColorMode() { | ||
const [mode, setMode] = React.useState('light'); | ||
const colorMode = React.useMemo( | ||
() => ({ | ||
toggleColorMode: () => { | ||
setMode((prevMode) => (prevMode === 'light' ? 'dark' : 'light')); | ||
}, | ||
}), | ||
[], | ||
); | ||
|
||
const theme = React.useMemo( | ||
() => | ||
createMuiTheme({ | ||
palette: { | ||
mode, | ||
}, | ||
}), | ||
[mode], | ||
); | ||
|
||
return ( | ||
<ColorModeContext.Provider value={colorMode}> | ||
<ThemeProvider theme={theme}> | ||
<MyApp /> | ||
</ThemeProvider> | ||
</ColorModeContext.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import * as React from 'react'; | ||
import IconButton from '@material-ui/core/IconButton'; | ||
import Box from '@material-ui/core/Box'; | ||
import { useTheme, ThemeProvider, createMuiTheme } from '@material-ui/core/styles'; | ||
import Brightness4Icon from '@material-ui/icons/Brightness4'; | ||
import Brightness7Icon from '@material-ui/icons/Brightness7'; | ||
|
||
const ColorModeContext = React.createContext({ toggleColorMode: () => {} }); | ||
|
||
function MyApp() { | ||
const theme = useTheme(); | ||
const colorMode = React.useContext(ColorModeContext); | ||
return ( | ||
<Box | ||
sx={{ | ||
display: 'flex', | ||
width: '100%', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
bgcolor: 'background.default', | ||
color: 'text.primary', | ||
borderRadius: 1, | ||
p: 3, | ||
}} | ||
> | ||
{theme.palette.mode} mode | ||
<IconButton sx={{ ml: 1 }} onClick={colorMode.toggleColorMode} color="inherit"> | ||
{theme.palette.mode === 'dark' ? <Brightness7Icon /> : <Brightness4Icon />} | ||
</IconButton> | ||
</Box> | ||
); | ||
} | ||
|
||
export default function ToggleColorMode() { | ||
const [mode, setMode] = React.useState<'light' | 'dark'>('light'); | ||
const colorMode = React.useMemo( | ||
() => ({ | ||
toggleColorMode: () => { | ||
setMode((prevMode) => (prevMode === 'light' ? 'dark' : 'light')); | ||
}, | ||
}), | ||
[], | ||
); | ||
|
||
const theme = React.useMemo( | ||
() => | ||
createMuiTheme({ | ||
palette: { | ||
mode, | ||
}, | ||
}), | ||
[mode], | ||
); | ||
|
||
return ( | ||
<ColorModeContext.Provider value={colorMode}> | ||
<ThemeProvider theme={theme}> | ||
<MyApp /> | ||
</ThemeProvider> | ||
</ColorModeContext.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