Skip to content

Commit

Permalink
fix: gracefully handle portal element being removed from dom (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbreiding committed Jul 29, 2019
1 parent 552f71c commit 8c47648
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/portal-popper.spec.jsx
Expand Up @@ -84,6 +84,7 @@ describe('<PortalPopper />', () => {
expect(Popper.firstCall.args[2].modifiers.preventOverflow).to.eql({
boundariesElement: 'boundary',
})

expect(Popper.firstCall.args[2].onCreate).to.be.a('function')
expect(Popper.firstCall.args[2].onUpdate).to.be.a('function')
})
Expand Down
6 changes: 5 additions & 1 deletion src/portal.jsx
Expand Up @@ -25,7 +25,11 @@ class Portal extends Component {
componentWillUnmount () {
const appendTo = this.props.appendTo

appendTo.removeChild(this._element)
// it's possible the element was removed from the dom or the dom has been
// blown away, which will cause `removeChild` to throw an exception
try {
appendTo.removeChild(this._element)
} catch (err) {} // eslint-disable-line no-empty
}

render () {
Expand Down
10 changes: 10 additions & 0 deletions src/portal.spec.jsx
Expand Up @@ -28,4 +28,14 @@ describe('<Portal />', () => {
component.unmount()
expect(document.getElementById('portal-0')).not.to.exist
})

it('gracefully handles the portal div no longer existing on unmount', () => {
Portal.idNum = 0
const component = mount(<Portal />)

document.getElementById('portal-0').remove()

component.unmount() // should not error
expect(document.getElementById('portal-0')).not.to.exist
})
})

0 comments on commit 8c47648

Please sign in to comment.