[react-float] feature detect getRootNode (#25689)
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.
Jan Kassens committed
Nov 16, 2022 at 13:12 UTC
c343f80254306e7ea4343a614c678bb03c4be9fd
1 file changed
+13
-5
packages/react-dom-bindings/src/client/ReactDOMFloatClient.js
+13
-5
@@ -167,9 +167,7 @@ let lastCurrentDocument: ?Document = null;
167
168
let previousDispatcher = null;
169
export function prepareToRenderResources(rootContainer: Container) {
170
- // Flot thinks that getRootNode returns a Node but it actually returns a
171
- // Document or ShadowRoot
172
- const rootNode: FloatRoot = (rootContainer.getRootNode(): any);
170
+ const rootNode = getRootNode(rootContainer);
171
lastCurrentDocument = getDocumentFromRoot(rootNode);
172
173
previousDispatcher = Dispatcher.current;
@@ -191,10 +189,20 @@ export type FloatRoot = Document | ShadowRoot;
189
// global maps of Resources
190
const preloadResources: Map<string, PreloadResource> = new Map();
191
192
+// getRootNode is missing from IE and old jsdom versions
193
+function getRootNode(container: Container): FloatRoot {
194
+ // $FlowFixMe[method-unbinding]
195
+ return typeof container.getRootNode === 'function'
196
+ ? /* $FlowFixMe[incompatible-return] Flow types this as returning a `Node`,
197
+ * but it's either a `Document` or `ShadowRoot`. */
198
+ container.getRootNode()
199
+ : container.ownerDocument;
200
+}
201
+
202
function getCurrentResourceRoot(): null | FloatRoot {
203
const currentContainer = getCurrentRootHostContainer();
204
// $FlowFixMe flow should know currentContainer is a Node and has getRootNode
197
- return currentContainer ? currentContainer.getRootNode() : null;
205
+ return currentContainer ? getRootNode(currentContainer) : null;
206
}
207
208
// This resource type constraint can be loosened. It really is everything except PreloadResource
@@ -204,7 +212,7 @@ function resetInstance(resource: ScriptResource | HeadResource) {
212
}
213
214
export function clearRootResources(rootContainer: Container): void {
207
- const rootNode: FloatRoot = (rootContainer.getRootNode(): any);
215
+ const rootNode = getRootNode(rootContainer);
216
const resources = getResourcesFromRoot(rootNode);
217
218
// We can't actually delete the resource cache because this function is called