-
-
Notifications
You must be signed in to change notification settings - Fork 32.8k
[blog] Bringing consistency to Material UI customization APIs #41040
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
175b15a
Initial commit
DiegoAndai 00c2593
Merge branch 'master' into deprecations-blog
DiegoAndai 86b026e
Update content
DiegoAndai 33bb37e
Add explanation to migrating guide
DiegoAndai e79d2ec
markdownlint
DiegoAndai 4088360
Avoid "will"
DiegoAndai 38685bd
add OG card and update the tag
danilo-leal 846b359
Merge branch 'master' into pr/41040
danilo-leal c834a68
Sam's initial review
DiegoAndai 7ea0505
Change title
DiegoAndai a1c130c
pnpm prettier
DiegoAndai 7d51c93
update card
danilo-leal 32f6e4e
Apply suggestions from code review
DiegoAndai 48f36d1
Danilo's review
DiegoAndai 5c069ee
Final details
DiegoAndai ec2202f
Update date
DiegoAndai 33cd143
Point to material-ui repo's HEAD on codemods link
DiegoAndai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
docs/pages/blog/bringing-consistency-to-material-ui-customization-apis.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import * as React from 'react'; | ||
| import TopLayoutBlog from 'docs/src/modules/components/TopLayoutBlog'; | ||
| import { docs } from './bringing-consistency-to-material-ui-customization-apis.md?muiMarkdown'; | ||
|
|
||
| export default function Page() { | ||
| return <TopLayoutBlog docs={docs} />; | ||
| } |
70 changes: 70 additions & 0 deletions
70
docs/pages/blog/bringing-consistency-to-material-ui-customization-apis.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| --- | ||
| title: Bringing consistency to Material UI customization APIs | ||
| description: We're standardizing two key areas of the Material UI customization APIs to reduce complexity and cognitive overhead. Read on to learn what's changing. | ||
| date: 2024-03-11T00:00:00.000Z | ||
| authors: ['diegoandai'] | ||
| tags: ['Material UI', 'Product'] | ||
| card: true | ||
| --- | ||
|
|
||
| The MUI Core team is working on two initiatives to standardize the Material UI API: The first applies to overriding inner elements, and the second applies to component CSS classes. | ||
DiegoAndai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| In both cases, the purpose is to provide a more consistent developer experience for the community. | ||
|
|
||
| Let's explore how these changes are taking shape: | ||
|
|
||
| ## Inner element overrides | ||
|
|
||
| Because Material UI components often contain multiple DOM nodes, it's common to need to modify the structure, behavior, and style of inner elements. | ||
| For example, you might want to modify the Slider's thumb element to grow in size when dragged: | ||
|
|
||
| <iframe src="https://codesandbox.io/embed/nw34ry?view=Editor+%2B+Preview&module=%2Fsrc%2FDemo.tsx&hidenavigation=1" | ||
| style="width:100%; height: 200px; border:0; border-radius: 4px; overflow:hidden;" | ||
| title="blog/material-ui-early-2024-standardization/slider-slots-example" | ||
| allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking" | ||
| sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts" | ||
| ></iframe> | ||
|
|
||
| You can achieve this by providing custom components through the `slots` prop. | ||
| The demo above provides a custom thumb component that uses the Slider's internal `dragging` and `focusedThumbIndex` states to change its appearance. | ||
| [Open the CodeSandbox](https://codesandbox.io/p/sandbox/blog-material-ui-early-2024-deprecations-slider-slots-example-nw34ry?file=%2Fsrc%2FDemo.tsx) to see the implementation. | ||
|
|
||
| The problem is that this slot pattern exposed through the `slots` prop is not consistent across the library. | ||
| Some components implement the `slots` prop, but others have a `components` prop, which works almost exactly as the `slots` prop. | ||
| Other components have props named `<SlotName>Component` for more specific use cases—for example, the Accordion features a `TransitionComponent` prop for implementing custom transitions. | ||
|
|
||
| The same inconsistencies are found with the `slotProps` prop, which is used to provide custom props to inner elements. | ||
| Some components have the `slotsProps` prop; others have a `componentsProps` prop; and still others have props named `<SlotName>Props`. | ||
|
|
||
| This lack of consistency leads to unnecessary complexity for both developers and maintainers. | ||
| To resolve this, the `slots` and `slotProps` API will be standardized across all components, and the analogous APIs will be deprecated and eventually removed. | ||
|
|
||
| ## Component CSS classes | ||
|
|
||
| The most common way to customize a component's look and feel is to target its CSS classes. | ||
| For example, you might want to customize a Chip's primary color and set it to a different color when it's clickable: | ||
|
|
||
| <iframe src="https://codesandbox.io/embed/d7xqr6?view=Editor+%2B+Preview&module=%2Fsrc%2FDemo.tsx&hidenavigation=1" | ||
| style="width:100%; height: 200px; border:0; border-radius: 4px; overflow:hidden;" | ||
| title="blog/material-ui-early-2024-standardization/chip-classes-example" | ||
| allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking" | ||
| sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts" | ||
| ></iframe> | ||
|
|
||
| You can do this by targeting `chipClasses.colorPrimary` and `chipClasses.clickable`, respectively. | ||
| The demo above targets `.MuiChip-colorPrimary` and `.MuiChip-clickable.MuiChip-colorPrimary` to achieve this result. | ||
| [Open the CodeSandbox](https://codesandbox.io/p/sandbox/blog-material-ui-early-2024-deprecations-chip-classes-example-d7xqr6?file=%2Fsrc%2FDemo.tsx) to see the implementation. | ||
|
|
||
| The problem is that you could also use the `chipClasses.clickableColorPrimary` composed class, which composes the atomic clickable and color classes. | ||
| These composed classes bloat the API without adding significant improvements. | ||
| For example, this pattern adds 26 CSS classes to the `Chip` component. | ||
|
|
||
| The composed classes also reduce the predictability of the CSS classes API, as the compose order and which props get composed are arbitrary decisions. | ||
| This adds unnecessary cognitive overhead for developers as well as significant complexity for maintainers. | ||
| Because of these issues, composed CSS classes will be deprecated and eventually removed in favor of atomic class alternatives. | ||
|
|
||
| ## Standardization process | ||
|
|
||
| This initiative aims to improve the developer experience for the Material UI community. | ||
| To provide the smoothest migration from these APIs, they will be deprecated first and removed at a future date, likely the end of 2024. | ||
| With each deprecation, we'll update the [migration guide](https://mui.com/material-ui/migration/migrating-from-deprecated-apis/) and provide [codemods](https://github.com/mui/material-ui/tree/master/packages/mui-codemod#deprecations) to simplify the process. | ||
| As always, please [open a GitHub issue](https://github.com/mui/material-ui/issues/new/choose) if you encounter any unexpected behavior with the standardized APIs. | ||
DiegoAndai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Binary file added
BIN
+191 KB
...lic/static/blog/bringing-consistency-to-material-ui-customization-apis/card.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.