-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
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 Shadow DOM guide #33007
Merged
Merged
[docs] Add Shadow DOM guide #33007
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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,16 @@ | ||
import * as React from 'react'; | ||
|
||
export default function ShadowDOMDemo() { | ||
return ( | ||
<iframe | ||
title="codesandbox" | ||
src="https://codesandbox.io/embed/basicselect-demo-material-ui-forked-rki9k5?hidenavigation=1&fontsize=14&view=preview&hidedevtools=1" | ||
style={{ | ||
width: '100%', | ||
height: 350, | ||
border: 0, | ||
}} | ||
sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin hidenavigation" | ||
/> | ||
); | ||
} |
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,16 @@ | ||
import * as React from 'react'; | ||
|
||
export default function ShadowDOMDemo() { | ||
return ( | ||
<iframe | ||
title="codesandbox" | ||
src="https://codesandbox.io/embed/basicselect-demo-material-ui-forked-rki9k5?hidenavigation=1&fontsize=14&view=preview&hidedevtools=1" | ||
style={{ | ||
width: '100%', | ||
height: 350, | ||
border: 0, | ||
}} | ||
sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin hidenavigation" | ||
/> | ||
); | ||
} |
10 changes: 10 additions & 0 deletions
10
docs/data/material/guides/shadow-dom/ShadowDOMDemo.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,10 @@ | ||
<iframe | ||
title="codesandbox" | ||
src="https://codesandbox.io/embed/basicselect-demo-material-ui-forked-rki9k5?hidenavigation=1&fontsize=14&view=preview&hidedevtools=1" | ||
style={{ | ||
width: '100%', | ||
height: 350, | ||
border: 0, | ||
}} | ||
sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin hidenavigation" | ||
/> |
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,68 @@ | ||
# Shadow DOM | ||
|
||
<p class="description">The shadow DOM lets you encapsulate parts of an app to keep them separate from global styles that target the regular DOM tree.</p> | ||
|
||
## How to use the shadow DOM with Material UI | ||
|
||
### 1. Styles | ||
|
||
The shadow DOM is an API that provides a way to attach a hidden separated DOM to an element. | ||
This is useful when you need to keep the structure, style, and behavior of different components separate from the rest of the code on the page, to prevent conflicts. | ||
See [the MDN docs on the shadow DOM](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM) for more information. | ||
The following code snippet shows how to apply styles inside of the shadow DOM: | ||
|
||
```tsx | ||
const container = document.querySelector('#root'); | ||
const shadowContainer = container.attachShadow({ mode: 'open' }); | ||
const emotionRoot = document.createElement('style'); | ||
const shadowRootElement = document.createElement('div'); | ||
shadowContainer.appendChild(emotionRoot); | ||
shadowContainer.appendChild(shadowRootElement); | ||
|
||
const cache = createCache({ | ||
key: 'css', | ||
prepend: true, | ||
container: emotionRoot, | ||
}); | ||
|
||
ReactDOM.createRoot(shadowRootElement).render( | ||
<CacheProvider value={cache}> | ||
<App /> | ||
</CacheProvider>, | ||
); | ||
``` | ||
|
||
### 2. Theme | ||
|
||
MUI components like `Menu`, `Dialog`, `Popover` and others use [`Portal`](/material-ui/react-portal/) to render a new "subtree" in a container outside of current DOM hierarchy. | ||
By default, this container is `document.body`. | ||
But since the styles are applied only inside of the Shadow DOM, we need to render portals inside the Shadow DOM container as well: | ||
|
||
```tsx | ||
const theme = createTheme({ | ||
components: { | ||
MuiPopover: { | ||
defaultProps: { | ||
container: shadowRootElement, | ||
}, | ||
}, | ||
MuiPopper: { | ||
defaultProps: { | ||
container: shadowRootElement, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
// ... | ||
|
||
<ThemeProvider theme={theme}> | ||
<App /> | ||
</ThemeProvider>; | ||
``` | ||
|
||
## Demo | ||
|
||
In the example below you can see that the component outside of the shadow DOM is affected by global styles, while the component inside of the shadow DOM is not: | ||
|
||
{{"demo": "ShadowDOMDemo.js", "defaultCodeOpen": false}} | ||
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 |
---|---|---|
|
@@ -208,6 +208,7 @@ const pages = [ | |
{ pathname: '/material-ui/guides/content-security-policy', title: 'Content Security Policy' }, | ||
{ pathname: '/material-ui/guides/right-to-left', title: 'Right-to-left' }, | ||
{ pathname: '/material-ui/guides/flow' }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that we could drop this page, it's outdated now. I have taken note in https://www.notion.so/mui-org/Opportunities-408a9771556a4715830d18f3749af753#167b4a8cef8e44e2a31c9c2568ec12fb. |
||
{ pathname: '/material-ui/guides/shadow-dom', title: 'Shadow DOM' }, | ||
], | ||
}, | ||
{ | ||
|
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,11 @@ | ||
import * as React from 'react'; | ||
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs'; | ||
import { | ||
demos, | ||
docs, | ||
demoComponents, | ||
} from 'docs/data/material/guides/shadow-dom/shadow-dom.md?@mui/markdown'; | ||
|
||
export default function Page() { | ||
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} />; | ||
} |
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
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.
I did a few changes around this demo in #33122