-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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] Add overriding-component-structure
doc to Material UI
#45186
Open
siriwatknp
wants to merge
5
commits into
mui:master
Choose a base branch
from
siriwatknp:docs/mui-override-component
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2559c6f
duplicate overriding-component-structure to material-ui
siriwatknp fe5d8a7
Merge branch 'master' into docs/mui-override-component
sai6855 95bb036
run docs:i18n
siriwatknp 0acfec2
Merge branch 'docs/mui-override-component' of github.com:siriwatknp/m…
siriwatknp c900458
Update docs/data/material/customization/overriding-component-structur…
siriwatknp 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
38 changes: 38 additions & 0 deletions
38
docs/data/material/customization/overriding-component-structure/OverridingInternalSlot.js
This file contains 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,38 @@ | ||
import * as React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import Box from '@mui/material/Box'; | ||
import Autocomplete from '@mui/material/Autocomplete'; | ||
import TextField from '@mui/material/TextField'; | ||
|
||
function PopperComponent(props) { | ||
const { disablePortal, anchorEl, open, ...other } = props; | ||
return <div {...other} />; | ||
} | ||
|
||
PopperComponent.propTypes = { | ||
anchorEl: PropTypes.any, | ||
disablePortal: PropTypes.bool, | ||
open: PropTypes.bool.isRequired, | ||
}; | ||
|
||
export default function OverridingInternalSlot() { | ||
return ( | ||
<Box | ||
sx={{ display: 'flex', flexDirection: 'column', width: 320, minHeight: 220 }} | ||
> | ||
<Autocomplete | ||
open | ||
options={[ | ||
{ label: '🆘 Need help' }, | ||
{ label: '✨ Improvement' }, | ||
{ label: '🚀 New feature' }, | ||
{ label: '🐛 Bug fix' }, | ||
]} | ||
renderInput={(params) => <TextField {...params} />} | ||
slots={{ | ||
popper: PopperComponent, | ||
}} | ||
/> | ||
</Box> | ||
); | ||
} |
37 changes: 37 additions & 0 deletions
37
docs/data/material/customization/overriding-component-structure/OverridingInternalSlot.tsx
This file contains 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,37 @@ | ||
import * as React from 'react'; | ||
import Box from '@mui/material/Box'; | ||
import Autocomplete from '@mui/material/Autocomplete'; | ||
import TextField from '@mui/material/TextField'; | ||
|
||
interface PopperComponentProps { | ||
anchorEl?: any; | ||
disablePortal?: boolean; | ||
open: boolean; | ||
} | ||
|
||
function PopperComponent(props: PopperComponentProps) { | ||
const { disablePortal, anchorEl, open, ...other } = props; | ||
return <div {...other} />; | ||
} | ||
|
||
export default function OverridingInternalSlot() { | ||
return ( | ||
<Box | ||
sx={{ display: 'flex', flexDirection: 'column', width: 320, minHeight: 220 }} | ||
> | ||
<Autocomplete | ||
open | ||
options={[ | ||
{ label: '🆘 Need help' }, | ||
{ label: '✨ Improvement' }, | ||
{ label: '🚀 New feature' }, | ||
{ label: '🐛 Bug fix' }, | ||
]} | ||
renderInput={(params) => <TextField {...params} />} | ||
slots={{ | ||
popper: PopperComponent, | ||
}} | ||
/> | ||
</Box> | ||
); | ||
} |
13 changes: 13 additions & 0 deletions
13
.../material/customization/overriding-component-structure/OverridingInternalSlot.tsx.preview
This file contains 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,13 @@ | ||
<Autocomplete | ||
open | ||
options={[ | ||
{ label: '🆘 Need help' }, | ||
{ label: '✨ Improvement' }, | ||
{ label: '🚀 New feature' }, | ||
{ label: '🐛 Bug fix' }, | ||
]} | ||
renderInput={(params) => <TextField {...params} />} | ||
slots={{ | ||
popper: PopperComponent, | ||
}} | ||
/> |
15 changes: 15 additions & 0 deletions
15
docs/data/material/customization/overriding-component-structure/OverridingRootSlot.js
This file contains 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,15 @@ | ||
import * as React from 'react'; | ||
import Button from '@mui/material/Button'; | ||
|
||
export default function OverridingRootSlot() { | ||
return ( | ||
<Button | ||
component="a" | ||
href="https://mui.com/about/" | ||
target="_blank" | ||
rel="noopener" | ||
> | ||
About us | ||
</Button> | ||
); | ||
} |
15 changes: 15 additions & 0 deletions
15
docs/data/material/customization/overriding-component-structure/OverridingRootSlot.tsx
This file contains 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,15 @@ | ||
import * as React from 'react'; | ||
import Button from '@mui/material/Button'; | ||
|
||
export default function OverridingRootSlot() { | ||
return ( | ||
<Button | ||
component="a" | ||
href="https://mui.com/about/" | ||
target="_blank" | ||
rel="noopener" | ||
> | ||
About us | ||
</Button> | ||
); | ||
} |
8 changes: 8 additions & 0 deletions
8
...data/material/customization/overriding-component-structure/OverridingRootSlot.tsx.preview
This file contains 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,8 @@ | ||
<Button | ||
component="a" | ||
href="https://mui.com/about/" | ||
target="_blank" | ||
rel="noopener" | ||
> | ||
About us | ||
</Button> |
93 changes: 93 additions & 0 deletions
93
.../customization/overriding-component-structure/overriding-component-structure.md
This file contains 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,93 @@ | ||
# Overriding component structure | ||
|
||
<p class="description">Learn how to override the default DOM structure of Material UI components.</p> | ||
|
||
Material UI components are designed to suit the widest possible range of use cases, but you may occasionally need to change how a component's structure is rendered in the DOM. | ||
|
||
To understand how to do this, it helps to have an accurate mental model of the components. | ||
|
||
## The mental model | ||
|
||
A component's structure is determined by the elements that fill that component's **slots**. | ||
Slots are most commonly filled by HTML tags, but may also be filled by React components. | ||
|
||
All components contain a root slot that defines their primary node in the DOM tree; more complex components also contain additional interior slots named after the elements they represent. | ||
|
||
All _non-utility_ Material UI components accept two props for overriding their rendered HTML structure: | ||
|
||
- `component`—to override the root slot | ||
- `slots`—to replace any interior slots (when present) as well as the root | ||
|
||
Additionally, you can pass custom props to interior slots using `slotProps`. | ||
|
||
## The root slot | ||
|
||
The root slot represents the component's outermost element. It is filled by a styled component with an appropriate HTML element. | ||
|
||
For example, the [Button's](/material-ui/react-button/) root slot is a `<button>` element. | ||
This component _only_ has a root slot; more complex components may have additional [interior slots](#interior-slots). | ||
|
||
### The component prop | ||
|
||
Use the `component` prop to override a component's root slot. | ||
The demo below shows how to replace the Button's `<button>` tag with a `<a>` to create a link button: | ||
|
||
{{"demo": "OverridingRootSlot.js"}} | ||
|
||
:::info | ||
The props `href`, `target`, and `rel` are specific to `<a>` tag. Be sure to use the appropriate attributes when you provide a custom `component` prop. | ||
::: | ||
|
||
## Interior slots | ||
|
||
Complex components are composed of one or more interior slots in addition to the root. | ||
These slots are often (but not necessarily) nested within the root. | ||
|
||
For example, the [Autocomplete](/material-ui/react-autocomplete/) is composed of a root `<div>` that houses several interior slots named for the elements they represent: input, startDecorator, endDecorator, clearIndicator, popupIndicator and so on. | ||
|
||
### The slots prop | ||
|
||
Use the `slots` prop to replace a component's interior slots. | ||
The example below shows how to replace the popper slot in the [Autocomplete](/material-ui/react-autocomplete/) component to remove the popup functionality: | ||
|
||
{{"demo": "OverridingInternalSlot.js"}} | ||
|
||
### The slotProps prop | ||
|
||
The `slotProps` prop is an object that contains the props for all slots within a component. | ||
You can use it to define additional custom props to pass to a component's interior slots. | ||
|
||
For example, the code snippet below shows how to add a custom `data-testid` to the popper slot of the [Autocomplete](/material-ui/react-autocomplete/) component: | ||
|
||
```jsx | ||
<Autocomplete slotProps={{ popper: { 'data-testid': 'my-popper' } }} /> | ||
``` | ||
|
||
All additional props placed on the primary component are also propagated into the root slot (just as if they were placed in `slotProps.root`). | ||
These two examples are equivalent: | ||
|
||
```jsx | ||
<Autocomplete id="badge1"> | ||
``` | ||
|
||
```jsx | ||
<Autocomplete slotProps={{ root: { id: 'badge1' } }}> | ||
``` | ||
|
||
:::warning | ||
If both `slotProps.root` and additional props have the same keys but different values, the `slotProps.root` props will take precedence. | ||
Check warning on line 78 in docs/data/material/customization/overriding-component-structure/overriding-component-structure.md
|
||
This does not apply to classes or the `style` prop—they will be merged instead. | ||
Check warning on line 79 in docs/data/material/customization/overriding-component-structure/overriding-component-structure.md
|
||
::: | ||
|
||
## Best practices | ||
|
||
Use `component` or `slotProps.{slot}.component` prop to override the element by preserving the styles of the slot. | ||
|
||
Use `slots` prop to replace the slot's styles and functionality with your custom component. | ||
|
||
Overriding with `component` lets you apply the attributes of that element directly to the root. | ||
For instance, if you override the Button's root with an `<li>` tag, you can add the `<li>` attribute `value` directly to the component. | ||
If you did the same with `slots.root`, you would need to place this attribute on the `slotProps.root` object in order to avoid a TypeScript error. | ||
|
||
Be mindful of your rendered DOM structure when overriding the slots of more complex components. | ||
You can easily break the rules of semantic and accessible HTML if you deviate too far from the default structure—for instance, by unintentionally nesting block-level elements inside of inline elements. |
This file contains 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/material-ui/customization/overriding-component-structure.js
This file contains 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 MarkdownDocs from 'docs/src/modules/components/MarkdownDocs'; | ||
import * as pageProps from 'docs/data/material/customization/overriding-component-structure/overriding-component-structure.md?muiMarkdown'; | ||
|
||
export default function Page() { | ||
return <MarkdownDocs {...pageProps} />; | ||
} |
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can pass options without
label
property: