diff --git a/docs/src/pages/customization/palette/ToggleColorMode.js b/docs/src/pages/customization/palette/ToggleColorMode.js new file mode 100644 index 00000000000000..89c4f7b70b984a --- /dev/null +++ b/docs/src/pages/customization/palette/ToggleColorMode.js @@ -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 ( + + {theme.palette.mode} mode + + {theme.palette.mode === 'dark' ? : } + + + ); +} + +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 ( + + + + + + ); +} diff --git a/docs/src/pages/customization/palette/ToggleColorMode.tsx b/docs/src/pages/customization/palette/ToggleColorMode.tsx new file mode 100644 index 00000000000000..92a9e55691c868 --- /dev/null +++ b/docs/src/pages/customization/palette/ToggleColorMode.tsx @@ -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 ( + + {theme.palette.mode} mode + + {theme.palette.mode === 'dark' ? : } + + + ); +} + +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 ( + + + + + + ); +} diff --git a/docs/src/pages/customization/palette/palette.md b/docs/src/pages/customization/palette/palette.md index 2cb1fcdd13642a..936e31b61ffe04 100644 --- a/docs/src/pages/customization/palette/palette.md +++ b/docs/src/pages/customization/palette/palette.md @@ -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({ @@ -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.