Skip to content

Commit

Permalink
[react-float] feature detect getRootNode (#25689)
Browse files Browse the repository at this point in the history
Some old environments like IE11 or very old versions of jsdom are
missing `getRootNode()`. Use feature detection to fall back to
`ownerDocuments` in these environments that also won't be supporting
shadow DOM anyway.
  • Loading branch information
kassens committed Nov 16, 2022
1 parent e1dd0a2 commit c343f80
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/react-dom-bindings/src/client/ReactDOMFloatClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,7 @@ let lastCurrentDocument: ?Document = null;

let previousDispatcher = null;
export function prepareToRenderResources(rootContainer: Container) {
// Flot thinks that getRootNode returns a Node but it actually returns a
// Document or ShadowRoot
const rootNode: FloatRoot = (rootContainer.getRootNode(): any);
const rootNode = getRootNode(rootContainer);
lastCurrentDocument = getDocumentFromRoot(rootNode);

previousDispatcher = Dispatcher.current;
Expand All @@ -191,10 +189,20 @@ export type FloatRoot = Document | ShadowRoot;
// global maps of Resources
const preloadResources: Map<string, PreloadResource> = new Map();

// getRootNode is missing from IE and old jsdom versions
function getRootNode(container: Container): FloatRoot {
// $FlowFixMe[method-unbinding]
return typeof container.getRootNode === 'function'
? /* $FlowFixMe[incompatible-return] Flow types this as returning a `Node`,
* but it's either a `Document` or `ShadowRoot`. */
container.getRootNode()
: container.ownerDocument;
}

function getCurrentResourceRoot(): null | FloatRoot {
const currentContainer = getCurrentRootHostContainer();
// $FlowFixMe flow should know currentContainer is a Node and has getRootNode
return currentContainer ? currentContainer.getRootNode() : null;
return currentContainer ? getRootNode(currentContainer) : null;
}

// This resource type constraint can be loosened. It really is everything except PreloadResource
Expand All @@ -204,7 +212,7 @@ function resetInstance(resource: ScriptResource | HeadResource) {
}

export function clearRootResources(rootContainer: Container): void {
const rootNode: FloatRoot = (rootContainer.getRootNode(): any);
const rootNode = getRootNode(rootContainer);
const resources = getResourcesFromRoot(rootNode);

// We can't actually delete the resource cache because this function is called
Expand Down

0 comments on commit c343f80

Please sign in to comment.