From 8c47648bf53c43fc4a9fac12d0a4b4538d6d3c45 Mon Sep 17 00:00:00 2001 From: Chris Breiding Date: Mon, 29 Jul 2019 10:46:52 -0400 Subject: [PATCH] fix: gracefully handle portal element being removed from dom (#25) --- src/portal-popper.spec.jsx | 1 + src/portal.jsx | 6 +++++- src/portal.spec.jsx | 10 ++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/portal-popper.spec.jsx b/src/portal-popper.spec.jsx index 6b700b1..592f8e8 100644 --- a/src/portal-popper.spec.jsx +++ b/src/portal-popper.spec.jsx @@ -84,6 +84,7 @@ describe('', () => { 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') }) diff --git a/src/portal.jsx b/src/portal.jsx index 3931494..e3b2d7e 100644 --- a/src/portal.jsx +++ b/src/portal.jsx @@ -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 () { diff --git a/src/portal.spec.jsx b/src/portal.spec.jsx index 542e54c..fa935ae 100644 --- a/src/portal.spec.jsx +++ b/src/portal.spec.jsx @@ -28,4 +28,14 @@ describe('', () => { 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() + + document.getElementById('portal-0').remove() + + component.unmount() // should not error + expect(document.getElementById('portal-0')).not.to.exist + }) })