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] Add Shadow DOM guide #33007

Merged
merged 3 commits into from
Jun 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/data/material/guides/shadow-dom/ShadowDOMDemo.js
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"
/>
);
}
16 changes: 16 additions & 0 deletions docs/data/material/guides/shadow-dom/ShadowDOMDemo.tsx
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 docs/data/material/guides/shadow-dom/ShadowDOMDemo.tsx.preview
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"
/>
69 changes: 69 additions & 0 deletions docs/data/material/guides/shadow-dom/shadow-dom.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 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`.

cherniavskii marked this conversation as resolved.
Show resolved Hide resolved
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}}
Copy link
Member

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

1 change: 1 addition & 0 deletions docs/data/material/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Copy link
Member

Choose a reason for hiding this comment

The 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' },
],
},
{
Expand Down
11 changes: 11 additions & 0 deletions docs/pages/material-ui/guides/shadow-dom.js
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} />;
}
1 change: 1 addition & 0 deletions docs/translations/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@
"/material-ui/guides/content-security-policy": "Content Security Policy",
"/material-ui/guides/right-to-left": "Right-to-left",
"/material-ui/guides/flow": "Flow",
"/material-ui/guides/shadow-dom": "Shadow DOM",
"/material-ui/experimental-api": "Experimental APIs",
"/material-ui/experimental-api/classname-generator": "ClassName generator",
"/material-ui/experimental-api/css-variables": "CSS variables",
Expand Down