// instantiation first
const modalInstance = new Modal(options);
// use like below
modalInstance.show();
modalInstance.hide();// in render function
<Modal visiable={state.show} />
// use like below
setState({ show: true|false })
const container = document.createElement("div");
document.body.appendChild(container);// in portal render function
// ...
if (visible || this.modalRef) {
return ReactDOM.createPortal(
React.createElement(Modal, { ref: this.modalRef }),
fakeContainer // this will the contain was created above
);
}
return null; // return null before first showshouldComponentUpdate({ visible }) {
return !!(this.props.visible || visible)
}componentWillUnmount() {
if (this.container) {
this.container.parentNode!.removeChild(this.container)
this.container = null
}
}- scroll content can use flex layout to adjust modal to the vertical center
- scroll modal can show more main content
// always return null if without browser environment
if (typeof window === "undefined") {
return null;
}Mixing
display:none;with other transitionsProblem: You want an element to be hidden but also don't take up any space. You set
display:none;Now, when you wanna show it and set it todisplay:block;, it instantly appears. Great, now let's add another transition, like fadein or slide down.. oohh.. snap! Doesn't work.You could add a class with a delayed JS call, or...
If your enviroment allows to rely on keyframe animations: Put all the other transitions into a keyframe animation and it works! \o/