Skip to content

Commit

Permalink
[docs] Add example with switch dark/light mode (#25823)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vikram710 authored Apr 19, 2021
1 parent cf6d544 commit 76c5e34
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 4 deletions.
62 changes: 62 additions & 0 deletions docs/src/pages/customization/palette/ToggleColorMode.js
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>
);
}
62 changes: 62 additions & 0 deletions docs/src/pages/customization/palette/ToggleColorMode.tsx
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>
);
}
14 changes: 10 additions & 4 deletions docs/src/pages/customization/palette/palette.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,8 @@ Need inspiration? The Material Design team has built an [palette configuration t

## Dark mode

Material-UI comes with two palette types, light (the default) and dark.
Material-UI comes with two palette modes: light (the default) and dark.
You can make the theme dark by setting `mode: 'dark'`.
While it's only a single value change, internally it modifies several palette values.

```js
const darkTheme = createMuiTheme({
Expand All @@ -202,11 +201,18 @@ const darkTheme = createMuiTheme({
});
```

The colors modified by the palette type are the following:
While it's only a single value change, the `createMuiTheme` helper modifies several palette values.
The colors modified by the palette mode are the following:

{{"demo": "pages/customization/palette/DarkTheme.js", "bg": "inline", "hideToolbar": true}}

### User preference
### Toggling color mode

You can use the React context to toggle the mode with a button inside your page.

{{"demo": "pages/customization/palette/ToggleColorMode.js", "defaultCodeOpen": false}}

### System preference

Users might have specified a preference for a light or dark theme.
The method by which the user expresses their preference can vary. It might be a system-wide setting exposed by the Operating System, or a setting controlled by the User Agent.
Expand Down

0 comments on commit 76c5e34

Please sign in to comment.