Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Last active January 9, 2021 16:12
Show Gist options
  • Save ryanflorence/ac5174cf97dd596e949c to your computer and use it in GitHub Desktop.
Save ryanflorence/ac5174cf97dd596e949c to your computer and use it in GitHub Desktop.
React Portal.js
var Dialog = React.createClass({
mixins: [Portal],
createPortal: function() {
this.dialog = $(this.portalNode).dialog({
autoOpen: false,
title: this.props.title,
close: this.props.onClose
}).data('ui-dialog');
},
portalWillReceiveProps: function(props) {
if (props.open)
this.dialog.open();
else
this.dialog.close();
},
componentWillUnmount: function() {
this.dialog.destroy();
}
});
var Portal = {
render: function() {
return React.DOM.div();
},
componentDidMount: function() {
this.portalNode = this.getDOMNode();
this.createPortal();
this._renderPortal(this.props);
},
componentWillReceiveProps: function(newProps) {
this._renderPortal(newProps);
},
_renderPortal: function(props) {
React.renderComponent(React.DOM.div({}, props.children), this.portalNode);
this.portalWillReceiveProps(props);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment