Skip to content

Commit

Permalink
fix(InfoWindow): use ReactDOM.createPortal in React@^16
Browse files Browse the repository at this point in the history
* Closes #691
  • Loading branch information
tomchentw committed Nov 3, 2017
1 parent c4ded8b commit 6a61f2c
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/macros/InfoWindow.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* global google */
import invariant from "invariant"
import canUseDOM from "can-use-dom"
import React from "react"
import ReactDOM from "react-dom"
import PropTypes from "prop-types"
Expand Down Expand Up @@ -52,8 +53,23 @@ export class InfoWindow extends React.PureComponent {
}
}

componentWillMount() {
if (!canUseDOM || this.containerElement) {
return
}
if (React.version.match(/^16/)) {
this.containerElement = document.createElement(`div`)
}
}

componentDidMount() {
componentDidMount(this, this.state[INFO_WINDOW], eventMap)
if (React.version.match(/^16/)) {
this.state[INFO_WINDOW].setContent(this.containerElement)
open(this.state[INFO_WINDOW], this.context[ANCHOR])
this.containerElement = undefined
return
}
const content = document.createElement(`div`)
ReactDOM.unstable_renderSubtreeIntoContainer(
this,
Expand All @@ -72,6 +88,9 @@ export class InfoWindow extends React.PureComponent {
updaterMap,
prevProps
)
if (React.version.match(/^16/)) {
return
}
if (this.props.children !== prevProps.children) {
ReactDOM.unstable_renderSubtreeIntoContainer(
this,
Expand All @@ -85,14 +104,20 @@ export class InfoWindow extends React.PureComponent {
componentWillUnmount(this)
const infoWindow = this.state[INFO_WINDOW]
if (infoWindow) {
if (infoWindow.getContent()) {
if (!React.version.match(/^16/) && infoWindow.getContent()) {
ReactDOM.unmountComponentAtNode(infoWindow.getContent())
}
infoWindow.setMap(null)
}
}

render() {
if (React.version.match(/^16/)) {
return ReactDOM.createPortal(
React.Children.only(this.props.children),
this.containerElement
)
}
return false
}
}
Expand Down

0 comments on commit 6a61f2c

Please sign in to comment.