Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use createPortal for rendering DivOverlays #475

Merged
merged 1 commit into from
May 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 6 additions & 25 deletions src/DivOverlay.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @flow

import React, { Fragment } from 'react'
import { render, unmountComponentAtNode } from 'react-dom'
import { createPortal } from 'react-dom'

import MapComponent from './MapComponent'
import updateClassName from './utils/updateClassName'
Expand Down Expand Up @@ -41,48 +40,30 @@ export default class DivOverlay<
this.updateLeafletElement(prevProps, this.props)

if (this.leafletElement.isOpen()) {
this.renderContent()
this.leafletElement.update()
this.onRender()
}
}

onClose = () => {
this.removeContent()
if (this.props.onClose) {
this.props.onClose()
}
}

onOpen = () => {
this.renderContent()
this.forceUpdate() // Re-render now that leafletElement is created
if (this.props.onOpen) {
this.props.onOpen()
}
}

onRender() {}

renderContent = () => {
if (this.props.children == null) {
this.removeContent()
} else {
render(
<Fragment>{this.props.children}</Fragment>,
this.leafletElement._contentNode,
() => {
this.leafletElement.update()
this.onRender()
},
)
}
}

removeContent = () => {
render() {
if (this.leafletElement._contentNode) {
unmountComponentAtNode(this.leafletElement._contentNode)
return createPortal(this.props.children, this.leafletElement._contentNode)
}
}

render() {
return null
}
}
1 change: 0 additions & 1 deletion src/Popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class Popup extends DivOverlay<LeafletElement, Props> {
}

componentWillUnmount() {
this.removeContent()
const { map } = this.props.leaflet

if (map != null) {
Expand Down
2 changes: 0 additions & 2 deletions src/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ class Tooltip extends DivOverlay<LeafletElement, Props> {
}

componentWillUnmount() {
this.removeContent()

const { popupContainer } = this.props.leaflet
if (popupContainer == null) return

Expand Down