Skip to content

Commit 37e9dee

Browse files
authored
[material-ui][docs] Add first batch of v6 migration (#42242)
1 parent fb476c6 commit 37e9dee

File tree

10 files changed

+306
-148
lines changed

10 files changed

+306
-148
lines changed
Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
# Migrating to v6
2+
3+
<p class="description">This guide explains how and why to migrate from Material UI v5 to v6.</p>
4+
5+
## Start using the alpha release
6+
7+
In your `package.json` file, change the package version from `latest` to `next`.
8+
9+
```diff title="package.json"
10+
-"@mui/material": "latest",
11+
+"@mui/material": "next",
12+
```
13+
14+
Optionally, if you are using one of these packages, you can also change their version to `next`:
15+
16+
- `@mui/icons-material`
17+
- `@mui/system`
18+
- `@mui/lab`
19+
- `@mui/material-nextjs`
20+
- `@mui/styled-engine-sc`
21+
- `@mui/utils`
22+
23+
Using `next` ensures your project always uses the latest v6 alpha release.
24+
Alternatively, you can also target and fix it to a specific version, for example, `6.0.0-alpha.0`.
25+
26+
## Why you should migrate
27+
28+
Material UI v6's biggest highlight is the introduction of Pigment CSS, a next-gen zero-runtime CSS-in-JS library, as an opt-in styling engine.
29+
Using it will make your project compatible with React Server Components, as well as help reduce its bundle size due to styles being extracted at build time, avoiding client-side recalculation.
30+
31+
As a lesson learned from v5, this major release introduces minimal breaking changes.
32+
Namely, browser support updates, a Node.js version bump, and the removal of the UMD bundle.
33+
These updates reduced the Material UI package size by 2.5MB, 25% of the total size, and can be, for the most part, migrated automatically via codemods.
34+
35+
Aside from that, v6 also includes a few quality-of-life improvements regarding styling:
36+
37+
- The `CssVarsProvider` API is now stable. That enables you to rely on CSS variables, allowing for more intricate and performant customization possibilities, along with improved overall developer experience.
38+
- Support for container queries within the theme.
39+
- A new theme utility for adding styles to specific color modes.
40+
41+
## Supported browsers and Node versions
42+
43+
The targets of the default bundle have changed in v6.
44+
45+
The exact versions will be pinned on release from the browserslist query `"> 0.5%, last 2 versions, Firefox ESR, not dead, safari >= 15.4, iOS >= 15.4"`.
46+
47+
<!-- #stable-snapshot -->
48+
49+
- Node.js 18 (up from 12)
50+
- Chrome 109 (up from 90)
51+
- Edge 121 (up from 91)
52+
- Firefox 115 (up from 78)
53+
- Safari 15.4 in both macOS and iOS (up from 14 in macOS and 12.5 in iOS)
54+
- and more (see [.browserslistrc (`stable` entry)](https://github.com/mui/material-ui/blob/v6.0.0/.browserslistrc#L11))
55+
56+
### Removed support for IE 11
57+
58+
Support for IE 11 is completely removed, by dropping the legacy bundle and all IE 11 related code.
59+
This allows us to decrease bundle size and keep the scope manageable.
60+
If you need to support IE 11, you can use Material UI v5's [legacy bundle](https://v5.mui.com/material-ui/guides/minimizing-bundle-size/#legacy-bundle), but it won't get updates or bug fixes.
61+
62+
## Update React & TypeScript version
63+
64+
### Update React
65+
66+
The minimum supported version of React is v17.0.0 (the same as v5).
67+
Use the snippet below to update your project (replace the `<version>` with the one you want):
68+
69+
<codeblock storageKey="package-manager">
70+
71+
```bash npm
72+
npm install react@<version> react-dom@<version>
73+
```
74+
75+
```bash yarn
76+
yarn add react@<version> react-dom@<version>
77+
```
78+
79+
```bash pnpm
80+
pnpm add react@<version> react-dom@<version>
81+
```
82+
83+
</codeblock>
84+
85+
### Update TypeScript
86+
87+
The minimum supported version of TypeScript has been increased from v3.5 to 4.1.
88+
89+
:::info
90+
We align with types released by [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped) (published on npm under the `@types` namespace).
91+
92+
We will not change the minimum supported version in a minor version of Material UI.
93+
However, we recommend not using a TypeScript version older than the lowest supported version by DefinitelyTyped.
94+
:::
95+
96+
If your project includes these packages, you'll need to update them:
97+
98+
- `@types/react`
99+
- `@types/react-dom`
100+
101+
:::warning
102+
Make sure that your application is still running without errors, and commit the changes before continuing to the next step.
103+
:::
104+
105+
## Breaking changes
106+
107+
:::info
108+
This list is a work in progress.
109+
Expect updates as new breaking changes are introduced.
110+
:::
111+
112+
### UMD bundle was removed
113+
114+
To align with React 19's removal of UMD builds, Material UI has also removed its UMD bundle.
115+
This results in a reduction of the `@mui/material` package size by 2.5MB or 25% of the total package size.
116+
117+
Instead, using ESM-based CDNs such as [esm.sh](https://esm.sh/) is recommended.
118+
For alternative installation methods, refer to the [CDN documentation](/material-ui/getting-started/installation/#cdn).
119+
120+
### Chip
121+
122+
Previously, the Chip component lost focus when the escape button was pressed, which differed from how other button-like components work.
123+
This issue has been resolved, and the Chip now retains focus as expected.
124+
125+
If you want to keep the previous behavior, add a custom `onKeyUp` handler, as shown below:
126+
127+
```js
128+
import * as React from 'react';
129+
import Chip from '@mui/material/Chip';
130+
131+
export default function ChipExample() {
132+
const chipRef = React.useRef(null);
133+
const keyUpHandler = (event) => {
134+
if (event.key === 'Escape' && chipRef.current) {
135+
chipRef.current.blur();
136+
}
137+
};
138+
return (
139+
<Chip
140+
label="Chip Outlined"
141+
variant="outlined"
142+
ref={chipRef}
143+
onKeyUp={keyUpHandler}
144+
/>
145+
);
146+
}
147+
```
148+
149+
### Loading Button
150+
151+
The `children` passed to the Loading Button component is now wrapped in a `<span>` tag to avoid [issues](https://github.com/mui/material-ui/issues/27853) when using tools to translate websites.
152+
153+
### Grid v2 (Unstable_Grid)
154+
155+
The spacing mechanism was reworked to use the `gap` CSS property.
156+
This maps better with current layout practices and removes the need for using React Context.
157+
It brings some breaking changes described in the following sections.
158+
159+
#### Removal of the disableEqualOverflow prop
160+
161+
Previously, the Grid overflowed its parent.
162+
In v6, this is fixed, with the Grid being contained inside its parent's padding:
163+
164+
<img src="/static/material-ui/migration-v5/grid-overflow-change.png" style="width: 814px;" alt="Before and after of the Grid no longer overflowing its parent in v6." width="1628" height="400" />
165+
166+
This removes the need for the `disableEqualOverflow` prop:
167+
168+
```diff
169+
- <Grid disableEqualOverflow />
170+
+ <Grid />
171+
```
172+
173+
#### Spacing is no longer considered inside the Grid item's box
174+
175+
Previously, Grid items included spacing in their boxes.
176+
In v6, this is fixed:
177+
178+
<img src="/static/material-ui/migration-v5/grid-spacing-change.png" style="width: 814px;" alt="Before and after of the Grid items no longer including spacing in their box." width="1628" height="400" />
179+
180+
:::warning
181+
Both of these changes might slightly affect your layout.
182+
Note that the items' position doesn't change.
183+
We recommend adopting this new behavior and **not trying to replicate the old one**, as this is a more predictable and modern approach.
184+
:::
185+
186+
### useMediaQuery
187+
188+
The following deprecated types were removed:
189+
190+
- `MuiMediaQueryList`: use `MediaQueryList` (from lib.dom.d.ts) instead.
191+
- `MuiMediaQueryListEvent`: use `MediaQueryListEvent` (from lib.dom.d.ts) instead.
192+
- `MuiMediaQueryListListener`: use `(event: MediaQueryListEvent) => void` instead.
193+
194+
## Stabilized APIs
195+
196+
### CssVarsProvider and extendTheme
197+
198+
The `CssVarsProvider` and `extendTheme` APIs are now stable.
199+
If you already use them in v5, you can now drop the experimental prefix.
200+
201+
```diff
202+
- import { experimental_extendTheme as extendTheme, Experimental_CssVarsProvider as CssVarsProvider } from '@mui/material/styles';
203+
+ import { extendTheme, CssVarsProvider } from '@mui/material/styles';
204+
```
205+
206+
Check out the [CSS theme variables page](/material-ui/customization/css-theme-variables/overview/) to learn more about it.
207+
208+
### Add styles for specific color modes
209+
210+
Material UI v6 introduces a new utility for adding styles to specific color modes called `theme.applyStyles`.
211+
It's designed to replace `theme.palette.mode` when applying light or dark styles.
212+
213+
```diff
214+
const MyComponent = styled('button')(({ theme }) => ({
215+
padding: '0.5rem 1rem',
216+
border: '1px solid,
217+
- borderColor: theme.palette.mode === 'dark' ? '#fff' : '#000',
218+
+ borderColor: '#000',
219+
+ ...theme.applyStyles('dark', {
220+
+ borderColor: '#fff',
221+
+ })
222+
}))
223+
```
224+
225+
Use these codemods to migrate your project to `theme.applyStyles`:
226+
227+
```bash
228+
npx @mui/codemod@next v6.0.0/styled <path/to/folder-or-file>
229+
npx @mui/codemod@next v6.0.0/sx-prop <path/to/folder-or-file>
230+
npx @mui/codemod@next v6.0.0/theme-v6 <path/to/theme-file>
231+
```
232+
233+
:::info
234+
Run `v6.0.0/theme-v6` against the file that contains the custom `styleOverrides`. Ignore this codemod if you don't have a custom theme.
235+
:::
236+
237+
## Deprecations
238+
239+
### Components props
240+
241+
The `components` and `componentsProps` props have been deprecated in favor of `slots` and `slotProps`, making the API surface of the components more consistent.
242+
243+
Check out the [deprecations page](/material-ui/migration/migrating-from-deprecated-apis/) to learn which component no longer has these props.
244+
245+
### System props
246+
247+
System props, such as `mt={*}`, `bgcolor={*}`, and others, are deprecated in the Box, Typography, Link, Grid, and Stack components.
248+
Move all system props into the `sx` prop by using the codemod below:
249+
250+
```bash
251+
npx @mui/codemod@next v6.0.0/system-props <path/to/folder>
252+
```
253+
254+
Or do it manually like the example below:
255+
256+
```diff
257+
- <Button mr={2}>...</Button>
258+
+ <Button sx={{ mr: 2 }}>...</Button>
259+
```
260+
261+
### Theme component variants
262+
263+
Custom component variants defined in the theme are now merged with the theme style overrides, defined within the `root` slot of the component.
264+
Update the theme file using the codemod:
265+
266+
```bash
267+
npx @mui/codemod@next v6.0.0/theme-v6 <path/to/theme>
268+
```
269+
270+
Or do it manually like the example below:
271+
272+
```diff
273+
createTheme({
274+
components: {
275+
MuiButton: {
276+
- variants: [ ... ],
277+
+ styleOverrides: {
278+
+ root: {
279+
+ variants: [ ... ]
280+
+ }
281+
+ }
282+
}
283+
}
284+
})
285+
```
286+
287+
This reduces the API surface and lets you define variants in other slots of the component.
288+
289+
## Pigment CSS integration (optional)
290+
291+
:::info
292+
⏳ This section is under construction
293+
:::

0 commit comments

Comments
 (0)