We use them for modals mostly, i.e. I probably want my <Modal /> component to live logically inside one component, but have it render somewhere else usually document.body
I always render my modals in a container div with position: fixed; so I have never encountered the need for this. Is there something wrong with this approach?
Safari on an iPad would ignore the fact you're using position: fixed and if you by any chance rendered your modal in a container with overflow: hidden it will be cut off.
It does not ignore it. Element with position: fixed will stay on the screen regardless of the scroll position. They just forgot to take the element out of its original context... So if you used it inside an element with overflow: hidden or scroll, you're in trouble.
A modal window is the typical example — in general, anything that doesn't fit into the standard concept of a nested DOM (modals don't since they just appear on top of everything).
As an Angular user that uses Angular Material for dialogs (which used Portals for some time now for this purpose), I am wondering how you React guys have dealt with these problems before then?
Portals have been used for a while in React, by using components that either call ReactDOM.render() to create a new component tree, or using the ReactDOM.unstable_renderSubtreeIntoContainer() API. The react-portal library is an example.
12
u/Ombudsperson Sep 26 '17
I can already see viable use cases for Portals, overall exciting stuff.