Skip to content

[react-float] feature detect getRootNode #25689

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

Merged
merged 1 commit into from
Nov 16, 2022
Merged
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
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;
}
Comment on lines +193 to +200
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we feature detect and swap implementations rather than do this for every runtime call?

If we do feature detect we should also update the getDocumentFromRoot function to always just return the passed in root


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