Skip to content

Commit 229168a

Browse files
siriwatknpjoserodolfofreitas
authored andcommitted
[material-ui] Refine extendTheme and CssVarsProvider API (mui#42839)
1 parent 779bc36 commit 229168a

File tree

66 files changed

+1572
-1161
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1572
-1161
lines changed

docs/data/joy/customization/dark-mode/DarkModeByDefault.js

Lines changed: 0 additions & 43 deletions
This file was deleted.

docs/data/joy/customization/dark-mode/DarkModeByDefault.tsx

Lines changed: 0 additions & 43 deletions
This file was deleted.

docs/data/joy/customization/dark-mode/IdentifySystemMode.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ function Identifier() {
3939
export default function IdentifySystemMode() {
4040
return (
4141
<CssVarsProvider
42-
defaultMode="system"
4342
// The props below are specific to this demo,
4443
// you might not need them in your app.
4544
//

docs/data/joy/customization/dark-mode/IdentifySystemMode.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ function Identifier() {
3939
export default function IdentifySystemMode() {
4040
return (
4141
<CssVarsProvider
42-
defaultMode="system"
4342
// The props below are specific to this demo,
4443
// you might not need them in your app.
4544
//

docs/data/joy/customization/dark-mode/IdentifySystemMode.tsx.preview

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<CssVarsProvider
2-
defaultMode="system"
32
// The props below are specific to this demo,
43
// you might not need them in your app.
54
//
Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import * as React from 'react';
2-
import { CssVarsProvider, useColorScheme } from '@mui/joy/styles';
3-
import Box from '@mui/joy/Box';
4-
import Button from '@mui/joy/Button';
5-
6-
const useEnhancedEffect =
7-
typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
2+
import { CssVarsProvider, useColorScheme, extendTheme } from '@mui/joy/styles';
3+
import Select from '@mui/joy/Select';
4+
import Option from '@mui/joy/Option';
85

96
function ModeSwitcher() {
107
const { mode, setMode } = useColorScheme();
@@ -18,50 +15,40 @@ function ModeSwitcher() {
1815
return null;
1916
}
2017
return (
21-
<Button
18+
<Select
2219
variant="soft"
23-
color="neutral"
24-
onClick={() => setMode(mode === 'dark' ? 'light' : 'dark')}
20+
value={mode}
21+
onChange={(event, newMode) => {
22+
setMode(newMode);
23+
}}
2524
>
26-
{mode === 'dark' ? 'Turn light' : 'Turn dark'}
27-
</Button>
25+
<Option value="system">System</Option>
26+
<Option value="light">Light</Option>
27+
<Option value="dark">Dark</Option>
28+
</Select>
2829
);
2930
}
3031

31-
export default function ModeToggle() {
32-
// the `node` is used for attaching CSS variables to this demo,
33-
// you might not need it in your application.
34-
const [node, setNode] = React.useState(null);
35-
useEnhancedEffect(() => {
36-
setNode(document.getElementById('mode-toggle'));
37-
}, []);
32+
const theme = extendTheme({
33+
cssVarPrefix: 'mode-toggle',
34+
colorSchemeSelector: '.demo_mode-toggle-%s',
35+
});
3836

37+
export default function ModeToggle() {
3938
return (
4039
<CssVarsProvider
4140
// the props below are specific to this demo,
4241
// you might not need them in your app.
4342
//
44-
// the element to apply [data-joy-color-scheme] attribute.
45-
colorSchemeNode={node || null}
46-
//
47-
// the selector to apply the CSS theme variables stylesheet.
48-
colorSchemeSelector="#mode-toggle"
43+
theme={theme}
4944
//
5045
// the local storage key to use.
5146
modeStorageKey="mode-toggle-demo"
47+
//
48+
// set as root provider
49+
disableNestedContext
5250
>
53-
<Box
54-
id="mode-toggle"
55-
sx={{
56-
textAlign: 'center',
57-
flexGrow: 1,
58-
p: 2,
59-
m: -3,
60-
borderRadius: [0, 'sm'],
61-
}}
62-
>
63-
<ModeSwitcher />
64-
</Box>
51+
<ModeSwitcher />
6552
</CssVarsProvider>
6653
);
6754
}
Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import * as React from 'react';
2-
import { CssVarsProvider, useColorScheme } from '@mui/joy/styles';
3-
import Box from '@mui/joy/Box';
4-
import Button from '@mui/joy/Button';
5-
6-
const useEnhancedEffect =
7-
typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
2+
import { CssVarsProvider, useColorScheme, extendTheme } from '@mui/joy/styles';
3+
import Select from '@mui/joy/Select';
4+
import Option from '@mui/joy/Option';
85

96
function ModeSwitcher() {
107
const { mode, setMode } = useColorScheme();
@@ -18,50 +15,40 @@ function ModeSwitcher() {
1815
return null;
1916
}
2017
return (
21-
<Button
18+
<Select
2219
variant="soft"
23-
color="neutral"
24-
onClick={() => setMode(mode === 'dark' ? 'light' : 'dark')}
20+
value={mode}
21+
onChange={(event, newMode) => {
22+
setMode(newMode);
23+
}}
2524
>
26-
{mode === 'dark' ? 'Turn light' : 'Turn dark'}
27-
</Button>
25+
<Option value="system">System</Option>
26+
<Option value="light">Light</Option>
27+
<Option value="dark">Dark</Option>
28+
</Select>
2829
);
2930
}
3031

31-
export default function ModeToggle() {
32-
// the `node` is used for attaching CSS variables to this demo,
33-
// you might not need it in your application.
34-
const [node, setNode] = React.useState<HTMLElement | null>(null);
35-
useEnhancedEffect(() => {
36-
setNode(document.getElementById('mode-toggle'));
37-
}, []);
32+
const theme = extendTheme({
33+
cssVarPrefix: 'mode-toggle',
34+
colorSchemeSelector: '.demo_mode-toggle-%s',
35+
});
3836

37+
export default function ModeToggle() {
3938
return (
4039
<CssVarsProvider
4140
// the props below are specific to this demo,
4241
// you might not need them in your app.
4342
//
44-
// the element to apply [data-joy-color-scheme] attribute.
45-
colorSchemeNode={node || null}
46-
//
47-
// the selector to apply the CSS theme variables stylesheet.
48-
colorSchemeSelector="#mode-toggle"
43+
theme={theme}
4944
//
5045
// the local storage key to use.
5146
modeStorageKey="mode-toggle-demo"
47+
//
48+
// set as root provider
49+
disableNestedContext
5250
>
53-
<Box
54-
id="mode-toggle"
55-
sx={{
56-
textAlign: 'center',
57-
flexGrow: 1,
58-
p: 2,
59-
m: -3,
60-
borderRadius: [0, 'sm'],
61-
}}
62-
>
63-
<ModeSwitcher />
64-
</Box>
51+
<ModeSwitcher />
6552
</CssVarsProvider>
6653
);
6754
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<CssVarsProvider
2+
// the props below are specific to this demo,
3+
// you might not need them in your app.
4+
//
5+
theme={theme}
6+
//
7+
// the local storage key to use.
8+
modeStorageKey="mode-toggle-demo"
9+
//
10+
// set as root provider
11+
disableNestedContext
12+
>
13+
<ModeSwitcher />
14+
</CssVarsProvider>

docs/data/joy/customization/dark-mode/dark-mode.md

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,23 @@
22

33
<p class="description">Learn about the different methods for applying dark mode to a Joy UI app.</p>
44

5-
## Set as default
5+
## Media prefers-color-scheme
66

7-
To set dark mode as the default for your app, add `defaultMode: 'dark'` to your `<CssVarsProvider>` wrapper component:
8-
9-
:::warning
10-
When you change the `defaultMode` to another value, you must clear the local storage for it to take effect.
11-
:::
12-
13-
{{"demo": "DarkModeByDefault.js"}}
14-
15-
For server-side applications, check out the framework setup in [the section below](#server-side-rendering) and provide the same value to the `InitColorSchemeScript` component:
7+
Create a theme with `colorSchemeSelector: 'media'` to use `@media (prefers-color-scheme)` instead of the default `data-joy-color-scheme` attribute.
168

179
```js
18-
<InitColorSchemeScript defaultMode="dark" />
19-
```
10+
import { extendTheme } from '@mui/joy/styles';
2011

21-
## Matching device's preference
22-
23-
Use `defaultMode: 'system'` to set your app's default mode to match the user's chosen preference on their device.
24-
25-
```jsx
26-
import { CssVarsProvider } from '@mui/joy/styles';
27-
28-
<CssVarsProvider defaultMode="system">...</CssVarsProvider>;
29-
```
12+
const theme = extendTheme({
13+
colorSchemeSelector: 'media',
14+
});
3015

31-
For server-side applications, check out the framework setup in [the section below](#server-side-rendering) and provide the same value to the `InitColorSchemeScript` component:
32-
33-
```js
34-
<InitColorSchemeScript defaultMode="system" />
16+
function App() {
17+
return <CssVarsProvider theme={theme}>...</CssVarsProvider>;
18+
}
3519
```
3620

37-
### Identify the system mode
21+
## Identify the system mode
3822

3923
Use the `useColorScheme` React hook to check if the user's preference is in light or dark mode:
4024

@@ -58,25 +42,7 @@ The `useColorScheme()` hook only works with components nested inside of `<CssVar
5842

5943
You can create a toggle component to give users the option to select between modes.
6044

61-
In the example below, we're using a `Button` component that calls `setMode` from the `useColorSchemes()` hook to handle the mode toggling.
62-
63-
```js
64-
import { useColorScheme } from '@mui/joy/styles';
65-
import Button from '@mui/joy/Button';
66-
67-
function ModeToggle() {
68-
const { mode, setMode } = useColorScheme();
69-
return (
70-
<Button
71-
variant="outlined"
72-
color="neutral"
73-
onClick={() => setMode(mode === 'dark' ? 'light' : 'dark')}
74-
>
75-
{mode === 'dark' ? 'Turn light' : 'Turn dark'}
76-
</Button>
77-
);
78-
}
79-
```
45+
In the example below, we're using a `Select` component that calls `setMode` from the `useColorSchemes()` hook to handle the mode switching.
8046

8147
{{"demo": "ModeToggle.js"}}
8248

docs/data/joy/customization/theme-colors/BootstrapVariantTokens.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,7 @@ export default function BootstrapVariantTokens() {
116116
}, []);
117117

118118
return (
119-
<CssVarsProvider
120-
theme={bootstrapTheme}
121-
colorSchemeNode={node || null}
122-
colorSchemeSelector="#bootstrap-buttons-demo"
123-
>
119+
<CssVarsProvider theme={bootstrapTheme} colorSchemeNode={node || null}>
124120
<Box
125121
id="bootstrap-buttons-demo"
126122
sx={{ display: 'flex', gap: 1, flexWrap: 'wrap' }}

docs/data/joy/customization/theme-colors/RemoveActiveTokens.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ export default function RemoveActiveTokens() {
3434
}, []);
3535

3636
return (
37-
<CssVarsProvider
38-
theme={theme}
39-
colorSchemeNode={node || null}
40-
colorSchemeSelector="#remove-active-tokens-demo"
41-
>
37+
<CssVarsProvider theme={theme} colorSchemeNode={node || null}>
4238
<Box
4339
id="remove-active-tokens-demo"
4440
sx={{ display: 'flex', gap: 1, flexWrap: 'wrap' }}

0 commit comments

Comments
 (0)