56

There is a modal in this answer https://stackoverflow.com/a/26789089/883571 which is creating a React-based Modal by appending it to <body>. However, I found it not compatible with the transition addons provided by React.

How to create one with transitions(during enter and leave)?

tanguy_k
  • 11,307
  • 6
  • 54
  • 58
jiyinyiyong
  • 4,586
  • 7
  • 45
  • 88

8 Answers8

73

At react conf 2015, Ryan Florence demonstrated using portals. Here's how you can create a simple Portal component...

var Portal = React.createClass({
  render: () => null,
  portalElement: null,
  componentDidMount() {
    var p = this.props.portalId && document.getElementById(this.props.portalId);
    if (!p) {
      var p = document.createElement('div');
      p.id = this.props.portalId;
      document.body.appendChild(p);
    }
    this.portalElement = p;
    this.componentDidUpdate();
  },
  componentWillUnmount() {
    document.body.removeChild(this.portalElement);
  },
  componentDidUpdate() {
    React.render(<div {...this.props}>{this.props.children}</div>, this.portalElement);
  }
});

and then everything you can normally do in React you can do inside of the portal...

    <Portal className="DialogGroup">
       <ReactCSSTransitionGroup transitionName="Dialog-anim">
         { activeDialog === 1 && 
            <div key="0" className="Dialog">
              This is an animated dialog
            </div> }
       </ReactCSSTransitionGroup>
    </Portal> 

jsbin demo

You can also have a look at Ryan's react-modal, although I haven't actually used it so I don't know how well it works with animation.

Gil Birman
  • 35,242
  • 14
  • 75
  • 119
  • Here's some [further reading on the topic](https://github.com/ryanflorence/react-training/commit/be707303a60311dedbe4d2cfa9c8d6aa8c915be3#diff-c91ce7bbbcd815459869edcc469036f8). Another option might be to use [`react-motion`](https://github.com/chenglou/react-motion). – vhs Dec 15 '15 at 01:39
  • 1
    There's also a standalone component for this: https://github.com/cloudflare/react-gateway – James Kyle Dec 17 '15 at 00:26
  • 1
    This is a nice solution, but doesn't work if you use the context. This is a problem with many libs like `redux` and `react-intl`. – DerLola Jul 04 '16 at 13:29
  • 3
    I think we also need a `ReactDOM.unmountComponentAtNode(this.portalElement)` in `componentWillUnmount`, no? Otherwise the React tree still lives in memory. It is also in the `react-modal` source https://github.com/reactjs/react-modal/blob/master/lib/components/Modal.js#L62. – thehammer Aug 02 '16 at 18:14
  • 1
    getting uncaught error, can't find element of id ... I found out it is because it is using React to render. It should be ReactDOM instead. – Pencilcheck Jul 03 '17 at 09:22
49

I wrote the module react-portal that should help you.

Usage:

import { Portal } from 'react-portal';
 
<Portal>
  This text is portaled at the end of document.body!
</Portal>
 
<Portal node={document && document.getElementById('san-francisco')}>
  This text is portaled into San Francisco!
</Portal>
Yehor Androsov
  • 4,885
  • 2
  • 23
  • 40
tajo
  • 611
  • 4
  • 3
31

React 15.x

Here's an ES6 version of the method described in this article:

import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';

export default class BodyEnd extends React.PureComponent {
    
    static propTypes = {
        children: PropTypes.node,
    };
    
    componentDidMount() {
        this._popup = document.createElement('div');
        document.body.appendChild(this._popup);
        this._render();
    }

    componentDidUpdate() {
        this._render();
    }

    componentWillUnmount() {
        ReactDOM.unmountComponentAtNode(this._popup);
        document.body.removeChild(this._popup);
    }

    _render() {
        ReactDOM.render(this.props.children, this._popup);
    }
    
    render() {
        return null;
    }
}

Just wrap any elements you want to be at the end of the DOM with it:

<BodyEnd><Tooltip pos={{x,y}}>{content}</Tooltip></BodyEnd>

React 16.x

Here's an updated version for React 16:

import React from 'react';
import ReactDOM from 'react-dom';

export default class BodyEnd extends React.Component {

    constructor(props) {
        super(props);
        this.el = document.createElement('div');
        this.el.style.display = 'contents';
        // The <div> is a necessary container for our
        // content, but it should not affect our layout.
        // Only works in some browsers, but generally
        // doesn't matter since this is at
        // the end anyway. Feel free to delete this line.
    }
    
    componentDidMount() {
        document.body.appendChild(this.el);
    }

    componentWillUnmount() {
        document.body.removeChild(this.el);
    }
    
    render() {
        return ReactDOM.createPortal(
            this.props.children,
            this.el,
        );
    }
}

Working example

Sufian
  • 6,405
  • 16
  • 66
  • 120
mpen
  • 272,448
  • 266
  • 850
  • 1,236
  • 1
    thanks mpen this above is best and simplest way by far and works on Cordova –  Oct 20 '17 at 06:41
  • @OZZIE Try using `ReactDOM.createPortal` instead. I bet that'll work! (Sorry, I don't the full code to post right now) – mpen Jan 04 '19 at 18:30
  • @mpen ah, yeah I realised after posting the comment that the link was to another solution, I though the code you posted was related to this :) would be awesome if this works, although I would be a bit fond of being able to claim that modals aren't well supported in React but now they are :< I hate modals/popups :) – OZZIE Jan 06 '19 at 15:59
  • 1
    @OZZIE Haha.. they're not so bad once you have some good functions/components for working with them. I updated the code for React 16. – mpen Jan 06 '19 at 21:11
  • @mpen I think they are bad UX, except for some rare prompts like "Are you sure ..?" – OZZIE Jan 07 '19 at 07:23
18

As other answers have stated this can be done using Portals. Starting from v16.0 Portals are included in React.

<body>
  <div id="root"></div>
  <div id="portal"></div>
</body>

Normally, when you return an element from a component's render method, it's mounted into the DOM as a child of the nearest parent node, but with portals you can insert a child into a different location in the DOM.

const PortalComponent = ({ children, onClose }) => {
  return createPortal(
    <div className="modal" style={modalStyle} onClick={onClose}>
      {children}
    </div>,
    // get outer DOM element
    document.getElementById("portal")
  );
};

class App extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      modalOpen: false
    };
  }

  render() {
    return (
      <div style={styles}>
        <Hello name="CodeSandbox" />
        <h2>Start editing to see some magic happen {"\u2728"}</h2>
        <button onClick={() => this.setState({ modalOpen: true })}>
          Open modal
        </button>
        {this.state.modalOpen && (
          <PortalComponent onClose={() => this.setState({ modalOpen: false })}>
            <h1>This is modal content</h1>
          </PortalComponent>
        )}
      </div>
    );
  }
}

render(<App />, document.getElementById("root"));

Check working example here.

Kristaps Taube
  • 2,363
  • 1
  • 17
  • 17
  • 3
    The most up-to-date answer as of January 2018... all the way down here.. makes you wonder whether this repeated issue is something that SO should think about – Elad Katz Jan 04 '18 at 08:16
3

The fundamental problem here is that in React you're only allowed to mount component to its parent, which is not always the desired behavior. But how to address this issue?

I've made the solution, addressed to fix this issue. More detailed problem definition, src and examples can be found here: https://github.com/fckt/react-layer-stack#rationale

Rationale

react/react-dom comes comes with 2 basic assumptions/ideas:

  • every UI is hierarchical naturally. This why we have the idea of components which wrap each other
  • react-dom mounts (physically) child component to its parent DOM node by default

The problem is that sometimes the second property isn't what you want in your case. Sometimes you want to mount your component into different physical DOM node and hold logical connection between parent and child at the same time.

Canonical example is Tooltip-like component: at some point of development process you could find that you need to add some description for your UI element: it'll render in fixed layer and should know its coordinates (which are that UI element coord or mouse coords) and at the same time it needs information whether it needs to be shown right now or not, its content and some context from parent components. This example shows that sometimes logical hierarchy isn't match with the physical DOM hierarchy.

Take a look at https://github.com/fckt/react-layer-stack/blob/master/README.md#real-world-usage-example to see the concrete example which is answer to your question:

import { Layer, LayerContext } from 'react-layer-stack'
// ... for each `object` in array of `objects`
  const modalId = 'DeleteObjectConfirmation' + objects[rowIndex].id
  return (
    <Cell {...props}>
        // the layer definition. The content will show up in the LayerStackMountPoint when `show(modalId)` be fired in LayerContext
        <Layer use={[objects[rowIndex], rowIndex]} id={modalId}> {({
            hideMe, // alias for `hide(modalId)`
            index } // useful to know to set zIndex, for example
            , e) => // access to the arguments (click event data in this example)
          <Modal onClick={ hideMe } zIndex={(index + 1) * 1000}>
            <ConfirmationDialog
              title={ 'Delete' }
              message={ "You're about to delete to " + '"' + objects[rowIndex].name + '"' }
              confirmButton={ <Button type="primary">DELETE</Button> }
              onConfirm={ this.handleDeleteObject.bind(this, objects[rowIndex].name, hideMe) } // hide after confirmation
              close={ hideMe } />
          </Modal> }
        </Layer>

        // this is the toggle for Layer with `id === modalId` can be defined everywhere in the components tree
        <LayerContext id={ modalId }> {({showMe}) => // showMe is alias for `show(modalId)`
          <div style={styles.iconOverlay} onClick={ (e) => showMe(e) }> // additional arguments can be passed (like event)
            <Icon type="trash" />
          </div> }
        </LayerContext>
    </Cell>)
// ...
fckt
  • 571
  • 4
  • 12
  • Please make it explicit that this is your own library, and please read [How to offer personal open-source libraries?](//meta.stackexchange.com/q/229085) – Martijn Pieters Nov 21 '16 at 06:58
  • 3
    "I propose the solution, addressed to fix this issue" - isn't enough? And this is not only about library, but more about the pattern, library is complementary in this case – fckt Nov 22 '16 at 01:11
  • No, it is not enough. You are promoting your own library as the proposed solution. You did so in a lot of places. Our community considers such promotion without disclosure to be a form of [*astroturfing*](https://en.wikipedia.org/wiki/Astroturfing). – Martijn Pieters Nov 22 '16 at 08:28
  • 2
    ok, could we agree first, that this is not clear, barely subjective, controversial topic? What if somebody write answers and explanations instead of me? How it will change the discourse? – fckt Nov 23 '16 at 07:02
  • btw, I read to links you shared, all statements are pretty clear, fair and logically consistent IMO. I'm sorry also for raising such a topic, don't sure this is a good place for it.. – fckt Nov 23 '16 at 07:11
  • what if I'll delete the links to the library? A can delete it from all my topics, not a big deal to me. If ppl will ask, I could share with them privately, no problem ) – fckt Nov 23 '16 at 07:15
3

I think this code is more or less self explanatory and covers the core solution of what most people are looking for:

ReactDOM.render(
  <Modal />,
  document.body.appendChild( document.createElement( 'div' ) ),
)
Itay Grudev
  • 7,055
  • 4
  • 54
  • 86
2

I've written a library to help with this. I avoid the DOM insertion hacks used by Portal strategies out there and instead make use of context based registries to pass along components from a source to a target.

My implementation makes use of the standard React render cycles. The components that you teleport/inject/transport don't cause a double render cycle on the target - everything happens synchronously.

The API is also structured in a manner to discourage the use of magic strings in your code to define the source/target. Instead you are required to explicitly create and decorate components that will be used as the target (Injectable) and the source (Injector). As this sort of thing is generally considered quite magical I think explicit Component representation (requiring direct imports and usage) may help alleviate confusion on where a Component is being injected.

Although my library won't allow you to render as a direct child of the document.body you can achieve an acceptable modal effect by binding to a root level component in your component tree. I plan on adding an example of this use case soon.

See https://github.com/ctrlplusb/react-injectables for more info.

ctrlplusb
  • 12,847
  • 6
  • 55
  • 57
0

Hope it helps. This is my current implementation of a transition modal based on the anwser above:

  React = require 'react/addons'

  keyboard = require '../util/keyboard'
  mixinLayered = require '../mixin/layered'

  $ = React.DOM
  T = React.PropTypes
  cx = React.addons.classSet

  module.exports = React.createFactory React.createClass
    displayName: 'body-modal'
    mixins: [mixinLayered]

    propTypes:
      # this components accepts children
      name:             T.string.isRequired
      title:            T.string
      onCloseClick:     T.func.isRequired
      showCornerClose:  T.bool
      show:             T.bool.isRequired

    componentDidMount: ->
      window.addEventListener 'keydown', @onWindowKeydown

    componentWillUnmount: ->
      window.removeEventListener 'keydown', @onWindowKeydown

    onWindowKeydown: (event) ->
      if event.keyCode is keyboard.esc
        @onCloseClick()

    onCloseClick: ->
      @props.onCloseClick()

    onBackdropClick: (event) ->
      unless @props.showCornerClose
        if event.target is event.currentTarget
          @onCloseClick()

    renderLayer: ->
      className = "body-modal is-for-#{@props.name}"
      $.div className: className, onClick: @onBackdropClick,
        if @props.showCornerClose
          $.a className: 'icon icon-remove', onClick: @onCloseClick
        $.div className: 'box',
          if @props.title?
            $.div className: 'title',
              $.span className: 'name', @props.title
              $.span className: 'icon icon-remove', @onCloseClick
          @props.children

    render: ->
      $.div()
jiyinyiyong
  • 4,586
  • 7
  • 45
  • 88