Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
159
rated 0 times [  165] [ 6]  / answers: 1 / hits: 71822  / 9 Years ago, mon, march 2, 2015, 12:00:00

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)?


More From » reactjs

 Answers
-1

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.


[#67606] Saturday, February 28, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
janiajohnnad

Total Points: 146
Total Questions: 92
Total Answers: 107

Location: Faroe Islands
Member since Thu, Apr 8, 2021
3 Years ago
;