Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] Update CSS theme variables related content #43130

Merged
merged 99 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 92 commits
Commits
Show all changes
99 commits
Select commit Hold shift + click to select a range
037f62b
wip
siriwatknp Jul 29, 2024
e2fae64
Merge branch 'next' of https://github.com/mui/material-ui into remove…
siriwatknp Jul 29, 2024
2258db1
rename type
siriwatknp Jul 29, 2024
e22d068
only call extendTheme when CssVarsProvider is used
siriwatknp Jul 29, 2024
5ba82d5
create new ThemeProvider and createTheme
siriwatknp Jul 29, 2024
3106aa9
finish first step
siriwatknp Jul 30, 2024
ab0424a
move some props to ThemeProviderCssVariablesProps
siriwatknp Jul 30, 2024
e7bf7dc
remove old createTheme and extendTheme
siriwatknp Jul 30, 2024
36ec12e
fix import paths
siriwatknp Jul 30, 2024
2603b25
fix all tests
siriwatknp Jul 30, 2024
71d750a
Merge branch 'next' of https://github.com/mui/material-ui into remove…
siriwatknp Jul 30, 2024
4e0f400
switch to new ThemeProvider
siriwatknp Jul 30, 2024
82aa32e
fix export path
siriwatknp Jul 30, 2024
584056e
switch to enable method
siriwatknp Jul 30, 2024
de9fb70
fix types
siriwatknp Jul 30, 2024
15440a5
Merge branch 'next' of https://github.com/mui/material-ui into remove…
siriwatknp Jul 30, 2024
d1b7783
fix rsc
siriwatknp Jul 30, 2024
bfac926
fix test
siriwatknp Jul 30, 2024
20d1da5
widen the type
siriwatknp Jul 30, 2024
e076c5c
Revert "fix all tests"
siriwatknp Jul 30, 2024
28873d0
fix disableTransitionOnChange test
siriwatknp Jul 30, 2024
fd557f5
fix getInitialTheme
siriwatknp Jul 30, 2024
7337a7d
add test
siriwatknp Jul 30, 2024
7d8f690
fix types
siriwatknp Jul 30, 2024
0bc2274
flatten nested folder
siriwatknp Jul 30, 2024
d239b2f
fix types
siriwatknp Jul 30, 2024
cc97041
fix lint
siriwatknp Jul 31, 2024
015b68e
Merge branch 'next' of https://github.com/mui/material-ui into remove…
siriwatknp Jul 31, 2024
24f9a88
fix alpha usage
siriwatknp Jul 31, 2024
4e36f1e
fix naming
siriwatknp Jul 31, 2024
9066fd1
remove function name
siriwatknp Jul 31, 2024
7987ead
extract error code
siriwatknp Jul 31, 2024
351deb2
revert unnecessary change
siriwatknp Jul 31, 2024
5e0ae73
refine condition
siriwatknp Jul 31, 2024
44f6618
remove CssVarsProvider file
siriwatknp Jul 31, 2024
eea2f01
prettier
siriwatknp Jul 31, 2024
40a78a4
fix test
siriwatknp Jul 31, 2024
6b5f671
add missing types
siriwatknp Jul 31, 2024
0005888
support 'class' and 'data' for InitColorSchemeScript
siriwatknp Jul 31, 2024
6689fa2
fix error code
siriwatknp Jul 31, 2024
df4e189
refactor for code review
siriwatknp Jul 31, 2024
5456030
update docs
siriwatknp Jul 31, 2024
f9bf0c7
update migration docs
siriwatknp Aug 1, 2024
12e9204
optimize useCurrentColorScheme
siriwatknp Aug 1, 2024
cc7ab2e
remove error throwing from useColorScheme
siriwatknp Aug 1, 2024
9f0217c
add more tests for createTheme
siriwatknp Aug 1, 2024
7fb6e02
optimize useCurrentColorScheme
siriwatknp Aug 1, 2024
87dc39f
remove error throwing from useColorScheme
siriwatknp Aug 1, 2024
c2c6c0c
add more tests for createTheme
siriwatknp Aug 1, 2024
ea872b0
prettier
siriwatknp Aug 1, 2024
d3ebf57
revert unnecessary change
siriwatknp Aug 1, 2024
b8ba6ac
Merge branch 'next' of https://github.com/mui/material-ui into remove…
siriwatknp Aug 1, 2024
8c3f2e0
fix test
siriwatknp Aug 1, 2024
26d992c
remove unused type
siriwatknp Aug 1, 2024
6593968
Merge branch 'remove-css-vars-provider' into docs/merge-css-vars-prov…
siriwatknp Aug 2, 2024
b122f12
update migration guide
siriwatknp Aug 2, 2024
4da44de
remove migrating to css-theme-variables page
siriwatknp Aug 2, 2024
786821a
update guide
siriwatknp Aug 2, 2024
415fe27
add demos
siriwatknp Aug 2, 2024
b98e8dc
replace fireEvent with user.selectOptions
siriwatknp Aug 2, 2024
66331ae
fix as suggested
siriwatknp Aug 2, 2024
023c756
fix test
siriwatknp Aug 2, 2024
7d2dde9
use createTheme
siriwatknp Aug 2, 2024
a56e5c8
prevent theme augmentation tests
siriwatknp Aug 2, 2024
1814385
Merge branch 'next' of https://github.com/mui/material-ui into remove…
siriwatknp Aug 5, 2024
4306f26
perf: remove unnecessary iteration
siriwatknp Aug 5, 2024
47d98eb
fix: custom color schemes
siriwatknp Aug 5, 2024
9ca6e73
add createColorScheme
siriwatknp Aug 5, 2024
9c35d23
fix types
siriwatknp Aug 5, 2024
1c3d639
Merge branch 'next' of https://github.com/mui/material-ui into docs/m…
siriwatknp Aug 5, 2024
ddd8004
Merge branch 'remove-css-vars-provider' into docs/merge-css-vars-prov…
siriwatknp Aug 5, 2024
a1c3f4f
adjust dark mode content
siriwatknp Aug 5, 2024
9971cbd
update usage
siriwatknp Aug 5, 2024
e82f542
Merge branch 'next' of https://github.com/mui/material-ui into docs/m…
siriwatknp Aug 6, 2024
71d5f18
update content
siriwatknp Aug 6, 2024
d7860b2
add deprecation message
siriwatknp Aug 7, 2024
401ba02
add comment
siriwatknp Aug 7, 2024
81487de
Merge branch 'deprecation/css-vars-provider' into docs/merge-css-vars…
siriwatknp Aug 7, 2024
a7c3a27
run link-check
siriwatknp Aug 7, 2024
7ccfb2d
Merge branch 'next' of https://github.com/mui/material-ui into docs/m…
siriwatknp Aug 9, 2024
e8a5784
remove CssVarsProvider
siriwatknp Aug 9, 2024
6071865
add suppressHydrationWarning
siriwatknp Aug 9, 2024
a490227
Update docs/data/material/customization/dark-mode/dark-mode.md
siriwatknp Aug 9, 2024
c795bdd
Merge branch 'next' of https://github.com/mui/material-ui into docs/m…
siriwatknp Aug 12, 2024
6780db5
Apply suggestions from code review
siriwatknp Aug 12, 2024
381bf84
Merge branch 'docs/merge-css-vars-provider' of github.com:siriwatknp/…
siriwatknp Aug 12, 2024
10e9ba2
update docs
siriwatknp Aug 12, 2024
e7f173d
prettier
siriwatknp Aug 12, 2024
df26e30
Apply suggestions from code review
siriwatknp Aug 14, 2024
01c520c
Merge branch 'next' of https://github.com/mui/material-ui into docs/m…
siriwatknp Aug 14, 2024
830909e
remove duplicated
siriwatknp Aug 14, 2024
4b9bfe5
update media prefers-color-scheme section
siriwatknp Aug 14, 2024
c20365b
Use ThemeProvider in guide
DiegoAndai Aug 14, 2024
67d69cb
Merge branch 'next' of https://github.com/mui/material-ui into docs/m…
siriwatknp Aug 16, 2024
ab432e1
update redirect and fix hash
siriwatknp Aug 16, 2024
7f6c7b6
fix link error
siriwatknp Aug 16, 2024
a671307
Merge branch 'next' of https://github.com/mui/material-ui into docs/m…
siriwatknp Aug 19, 2024
f08a601
Apply suggestions from code review
siriwatknp Aug 20, 2024
185c758
Update docs/data/material/customization/theming/theming.md
siriwatknp Aug 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/.link-check-errors.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Broken links found by `pnpm docs:link-check` that exist:

- https://mui.com/material-ui/customization/css-theme-variables/configuration/#advanced-configuration
- https://mui.com/material-ui/customization/css-theme-variables/configuration/#changing-variable-prefixes
- https://mui.com/material-ui/customization/theme-components/#creating-new-component-variants
- https://mui.com/material-ui/customization/theme-components/#overrides-based-on-props
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import * as React from 'react';
import { createTheme, ThemeProvider, useColorScheme } from '@mui/material/styles';
import Stack from '@mui/material/Stack';
import MenuItem from '@mui/material/MenuItem';
import Switch from '@mui/material/Switch';
import Select from '@mui/material/Select';
import FormControlLabel from '@mui/material/FormControlLabel';

const theme = createTheme({
cssVariables: {
colorSchemeSelector: '.demo-disable-transition-%s',
},
colorSchemes: { dark: true },
});

function ModeSwitcher() {
const { mode, setMode } = useColorScheme();
if (!mode) {
return null;
}
return (
<Select
value={mode}
onChange={(event) => setMode(event.target.value)}
sx={{ minWidth: 120 }}
>
<MenuItem value="system">System</MenuItem>
<MenuItem value="light">Light</MenuItem>
<MenuItem value="dark">Dark</MenuItem>
</Select>
);
}

export default function DisableTransitionOnChange() {
const [disableTransition, setDisableTransition] = React.useState(false);
return (
<ThemeProvider
theme={theme}
disableNestedContext
disableTransitionOnChange={disableTransition}
>
<Stack
sx={{
width: '100%',
borderRadius: '4px',
p: 2,
gap: 2,
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
bgcolor: 'background.default',
color: 'text.primary',
transition: '1s',
}}
>
<ModeSwitcher />
<FormControlLabel
control={
<Switch
checked={disableTransition}
onChange={(event) => setDisableTransition(event.target.checked)}
/>
}
label="Disable transition"
/>
</Stack>
</ThemeProvider>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import * as React from 'react';
import { createTheme, ThemeProvider, useColorScheme } from '@mui/material/styles';
import Stack from '@mui/material/Stack';
import MenuItem from '@mui/material/MenuItem';
import Switch from '@mui/material/Switch';
import Select from '@mui/material/Select';
import FormControlLabel from '@mui/material/FormControlLabel';

const theme = createTheme({
cssVariables: {
colorSchemeSelector: '.demo-disable-transition-%s',
},
colorSchemes: { dark: true },
});

function ModeSwitcher() {
const { mode, setMode } = useColorScheme();
if (!mode) {
return null;
}
return (
<Select
value={mode}
onChange={(event) =>
setMode(event.target.value as 'system' | 'light' | 'dark')
}
sx={{ minWidth: 120 }}
>
<MenuItem value="system">System</MenuItem>
<MenuItem value="light">Light</MenuItem>
<MenuItem value="dark">Dark</MenuItem>
</Select>
);
}

export default function DisableTransitionOnChange() {
const [disableTransition, setDisableTransition] = React.useState(false);
return (
<ThemeProvider
theme={theme}
disableNestedContext
disableTransitionOnChange={disableTransition}
>
<Stack
sx={{
width: '100%',
borderRadius: '4px',
p: 2,
gap: 2,
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
bgcolor: 'background.default',
color: 'text.primary',
transition: '1s',
}}
>
<ModeSwitcher />
<FormControlLabel
control={
<Switch
checked={disableTransition}
onChange={(event) => setDisableTransition(event.target.checked)}
/>
}
label="Disable transition"
/>
</Stack>
</ThemeProvider>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
To change the default variable prefix (`--mui`), provide a string to `cssVarPrefix` property, as shown below:

```js
extendTheme({ cssVarPrefix: 'any' });
createTheme({ cssVariables: { cssVarPrefix: 'any' } });

// generated stylesheet:
// --any-palette-primary-main: ...;
Expand All @@ -16,7 +16,7 @@
To remove the prefix, use an empty string as a value:

```js
extendTheme({ cssVarPrefix: '' });
createTheme({ cssVariables: { cssVarPrefix: '' } });

// generated stylesheet:
// --palette-primary-main: ...;
Expand All @@ -29,9 +29,11 @@
<codeblock>

```js class
extendTheme({
createTheme({
colorSchemes: { light: true, dark: true },
colorSchemeSelector: 'class'
cssVariables: {
colorSchemeSelector: 'class'
}
});

// CSS Result
Expand All @@ -40,9 +42,11 @@
```

```js data
extendTheme({
createTheme({
colorSchemes: { light: true, dark: true },
colorSchemeSelector: 'data'
cssVariables: {
colorSchemeSelector: 'data'
}
});

// CSS Result
Expand All @@ -52,9 +56,11 @@

```js string
// The value must start with dot (.) for class or square brackets ([]) for data
extendTheme({
createTheme({
colorSchemes: { light: true, dark: true },
colorSchemeSelector: '.theme-%s'
cssVariables: {
colorSchemeSelector: '.theme-%s'
}
});

// CSS Result
Expand All @@ -66,44 +72,13 @@

Then, use `useColorScheme` hook to switch between modes:

<codeblock>

```jsx client-side-app
import { useColorScheme } from '@mui/material/styles';

function ModeSwitcher() {
const { mode, setMode } = useColorScheme();

return (
<select
value={mode}
onChange={(event) => {
setMode(event.target.value);
// For TypeScript, cast `event.target.value as 'light' | 'dark' | 'system'`:
}}
>
<option value="system">System</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
);
}
```

```jsx server-side-app
```jsx
import { useColorScheme } from '@mui/material/styles';

function ModeSwitcher() {
const { mode, setMode } = useColorScheme();
const [mounted, setMounted] = React.useState(false);

React.useEffect(() => {
setMounted(true);
}, []);

if (!mounted) {
// for server-side rendering
// learn more at https://github.com/pacocoursey/next-themes#avoid-hydration-mismatch
if (!mode) {
return null;
}

Expand All @@ -123,10 +98,8 @@
}
```

</codeblock>

:::success
The mode will be `system` by default to follow the user's preference.
After React hydrates the tree, the mode is set to `system` to follow the user's preference.
:::

### Determining the system mode
Expand Down Expand Up @@ -190,7 +163,13 @@
The `attribute` has to be the same as the one you set in the `colorSchemeSelector` property:

```js
<InitColorSchemeScript attribute=".mode-%s" />
createTheme({
cssVariables: {
colorSchemeSelector: 'class'
}
})

<InitColorSchemeScript attribute="class" />
```

:::
Expand All @@ -204,17 +183,21 @@

export default function RootLayout({ children }) {
return (
<html lang="en">
<html lang="en" suppressHydrationWarning>
<body>
{/* must come before the <main> element */}
<InitColorSchemeScript attribute=".mode-%s" />
<InitColorSchemeScript attribute="class" />
<main>{children}</main>
</body>
</html>
);
}
```

:::warning
If you don't add `suppressHydrationWarning` to your `<html>` tag, you will see warnings about `"Extra attributes from the server"` because `InitColorSchemeScript` updates that element.

Check warning on line 198 in docs/data/material/customization/css-theme-variables/configuration.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Google.Will] Avoid using 'will'. Raw Output: {"message": "[Google.Will] Avoid using 'will'.", "location": {"path": "docs/data/material/customization/css-theme-variables/configuration.md", "range": {"start": {"line": 198, "column": 71}}}, "severity": "WARNING"}
:::

### Next.js Pages Router

Add the following code to the custom [`pages/_document.js`](https://nextjs.org/docs/pages/building-your-application/routing/custom-document) file:
Expand All @@ -226,11 +209,11 @@
export default class MyDocument extends Document {
render() {
return (
<Html data-color-scheme="light">
<Html>
<Head>...</Head>
<body>
{/* must come before the <Main> element */}
<InitColorSchemeScript attribute=".mode-%s" />
<InitColorSchemeScript attribute="class" />
<Main />
<NextScript />
</body>
Expand All @@ -249,7 +232,7 @@
import InitColorSchemeScript from '@mui/material/InitColorSchemeScript';

export function onRenderBody({ setPreBodyComponents }) {
setPreBodyComponents([<InitColorSchemeScript attribute=".mode-%s" />]);
setPreBodyComponents([<InitColorSchemeScript attribute="class" />]);
}
```

Expand Down Expand Up @@ -289,12 +272,12 @@

## Disabling CSS color scheme

By default, the `extendTheme` attach [CSS color-scheme](https://developer.mozilla.org/en-US/docs/Web/CSS/color-scheme) based on the palette mode. If you want to disable it, set `disableCssColorScheme` to `true`:
By default, `createTheme` attaches a [CSS `color-scheme` property](https://developer.mozilla.org/en-US/docs/Web/CSS/color-scheme) based on the palette mode.
You can disable this by setting `disableCssColorScheme` to `true`:

```js
extendTheme({
colorSchemes: { light: true, dark: true },
disableCssColorScheme: true,
createTheme({
cssVariables: { disableCssColorScheme: true },
});
```

Expand All @@ -312,8 +295,10 @@

## Instant transition between color schemes

To disable CSS transition when switching between modes, use `disableTransitionOnChange` prop:
To disable CSS transitions when switching between modes, apply the `disableTransitionOnChange` prop:

```js
<CssVarsProvider disableTransitionOnChange />
<ThemeProvider disableTransitionOnChange />
```

{{"demo": "DisableTransitionOnChange.js"}}
Loading
Loading