Skip to content

Commit

Permalink
[docs] Base Portal style revisions and final review (#32157)
Browse files Browse the repository at this point in the history
Co-authored-by: Michał Dudak <[email protected]>
Co-authored-by: Matt <[email protected]>
  • Loading branch information
3 people authored May 12, 2022
1 parent 00c604f commit feddf0e
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions docs/data/base/components/portal/portal.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,33 @@ packageName: '@mui/base'

# Portal

<p class="description">The portal component renders its children into a new "subtree" outside of the current DOM hierarchy.</p>
<p class="description">The Portal component lets you render its children into a DOM node that exists outside of its own DOM hierarchy.</p>

The children of the portal component will be appended to the `container` specified.
The component is used internally by the [`Modal`](/material-ui/react-modal/) and [`Popper`](/material-ui/react-popper/) components.
`Portal` is a utility component built around [React's `createPortal()` API](https://reactjs.org/docs/portals.html).
It gives you the functionality of `createPortal()` in a convenient component form.

{{"component": "modules/components/ComponentLinkHeader.js", "design": false}}
The `Portal` component is used internally by the [`ModalUnstyled`](/base/react-modal/) and [`Popper`](/base/react-popper/) components.

## Example
## Basic usage

Normally, children of a component are rendered within that component's DOM tree.
But sometimes it's necessary to mount a child at a different location in the DOM.

:::info
💡 According to [the React docs](https://reactjs.org/docs/portals.html), portals are useful when "you need the child element to visually 'break out' of its container"—for instance, modals and tooltips, which need to exist outside of the normal flow of the document.
:::

The `Portal` component accepts a `container` prop that passes a `ref` to the DOM node where its children will be mounted.

The following demo shows how a `<span>` nested within a `Portal` can be appended to a node outside of its own DOM hierarchy—click **Mount children** to see how it behaves:

{{"demo": "SimplePortal.js"}}

## Server-side

React [doesn't support](https://github.com/facebook/react/issues/13097) the [`createPortal()`](https://reactjs.org/docs/portals.html) API on the server.
You have to wait for the client-side hydration to see the children.
:::warning
⚠️ **Note:** React doesn't support the [`createPortal()` API](https://reactjs.org/docs/portals.html) on the server.
See [this GitHub issue](https://github.com/facebook/react/issues/13097) for details.
:::

The `Portal` component cannot be used to render child elements on the server—client-side hydration is necessary.

0 comments on commit feddf0e

Please sign in to comment.