Skip to content

Commit

Permalink
Use React 16 portal to render map overlay (#643)
Browse files Browse the repository at this point in the history
* Use React 16 portal to render map overlay

This allows the new context API to propagate context properly.

* Rename div to overlay
  • Loading branch information
clayne11 authored and itsmichaeldiego committed Sep 20, 2018
1 parent 28506dd commit b121bb6
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions src/google_map.js
Expand Up @@ -239,7 +239,7 @@ export default class GoogleMap extends Component {
this.zoomAnimationInProgress_ = false;

this.state = {
overlayCreated: false,
overlay: null,
};
}

Expand Down Expand Up @@ -587,7 +587,7 @@ export default class GoogleMap extends Component {
: '2000px';

const div = document.createElement('div');
this.div = div;
this.overlay = div;
div.style.backgroundColor = 'transparent';
div.style.position = 'absolute';
div.style.left = '0px';
Expand All @@ -601,29 +601,21 @@ export default class GoogleMap extends Component {
maps,
overlay.getProjection()
);
ReactDOM.unstable_renderSubtreeIntoContainer(
this_,
<GoogleMapMarkers
experimental={this_.props.experimental}
onChildClick={this_._onChildClick}
onChildMouseDown={this_._onChildMouseDown}
onChildMouseEnter={this_._onChildMouseEnter}
onChildMouseLeave={this_._onChildMouseLeave}
geoService={this_.geoService_}
insideMapPanes
distanceToMouse={this_.props.distanceToMouse}
getHoverDistance={this_._getHoverDistance}
dispatcher={this_.markersDispatcher_}
/>,
div,
// remove prerendered markers
() => this_.setState({ overlayCreated: true })
);

this_.setState(state => ({
...state,
overlay: div,
}));
},

onRemove() {
if (this.div) {
ReactDOM.unmountComponentAtNode(this.div);
this_.setState(state => ({
...state,
overlay: null,
}));

if (this.overlay) {
ReactDOM.unmountComponentAtNode(this.overlay);
}
},

Expand Down Expand Up @@ -1062,7 +1054,7 @@ export default class GoogleMap extends Component {
};

render() {
const mapMarkerPrerender = !this.state.overlayCreated
const mapMarkerPrerender = !this.state.overlay
? <GoogleMapMarkersPrerender
experimental={this.props.experimental}
onChildClick={this._onChildClick}
Expand All @@ -1085,6 +1077,22 @@ export default class GoogleMap extends Component {
onClick={this._onMapClick}
>
<GoogleMapMap registerChild={this._registerChild} />
{this.state.overlay &&
ReactDOM.createPortal(
<GoogleMapMarkers
experimental={this.props.experimental}
onChildClick={this._onChildClick}
onChildMouseDown={this._onChildMouseDown}
onChildMouseEnter={this._onChildMouseEnter}
onChildMouseLeave={this._onChildMouseLeave}
geoService={this.geoService_}
insideMapPanes
distanceToMouse={this.props.distanceToMouse}
getHoverDistance={this._getHoverDistance}
dispatcher={this.markersDispatcher_}
/>,
this.state.overlay
)}

{/* render markers before map load done */}
{mapMarkerPrerender}
Expand Down

0 comments on commit b121bb6

Please sign in to comment.