Support reverse portals in iframes and other windows. #41
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #29
Hi! Me again! It's been a while :)
With the latest versions of
react-new-window
andreact-reverse-portal
(react-new-window 1.0.1 and react-reverse-portal 2.1.1), I'm now getting the problem with exceptions being thrown because a DIV element is not an instanceof HTMLElement in Chrome, Firefox and Edge.It turns out that the
HTMLElement
andSVGElement
objects are not actually globals - they're values on thewindow
object. Each window object has its own instances of those objects, which is whyinstanceof
is returning false for cross-window tests. You apparently get the same behaviour if you test an element in an iframe.So, to get it working, I've made this PR which gets the parent window of the
domElement
(using the technique described in this stackoverflow answer), and then tests ifdomElement instanceof window.HTMLElement
ordomElement instanceof window.SVGElement
. This works for me in the above three browsers when I tested rendering the reverse portal in a new window.The code needs to cast things to
any
unfortunately, because Typescript doesn't know about the ancientparentWindow
field that's needed if you want to support IE8. Even if you very reasonably don't care about that, Typescript also doesn't believe you can useWindow.HTMLElement
andWindow.SVGElement
in instanceof tests (they're interfaces as far as it's concerned).Anyway, hopefully this change looks good to you. Let me know if you have any questions!