Skip to content

Commit 0a13bee

Browse files
author
Ben Grynhaus
committed
Add cross-browser support for checking if a node is in the DOM
1 parent 902ae07 commit 0a13bee

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

libs/core/src/lib/renderer/react-content.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import * as React from 'react';
55
import * as ReactDOM from 'react-dom';
66
import { Omit } from '../declarations/omit';
7+
import * as dom from '../utils/dom';
78

89
const DEBUG = false;
910
export const CHILDREN_TO_APPEND_PROP = 'children-to-append';
@@ -48,7 +49,7 @@ export class ReactContent extends React.PureComponent<AllReactContentProps> {
4849
const hostElement = this.props.legacyRenderMode ? element : element.parentElement;
4950

5051
// Only add children not already in the DOM
51-
this.props[CHILDREN_TO_APPEND_PROP].filter(child => !child.isConnected).forEach(child =>
52+
this.props[CHILDREN_TO_APPEND_PROP].filter(child => !dom.isNodeInDOM(child)).forEach(child =>
5253
hostElement.appendChild(child)
5354
);
5455
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Checks if a node is in the DOM.
3+
*
4+
* @param node The node to check
5+
* @returns whether the node is in the DOM
6+
*/
7+
export const isNodeInDOM = (node: Node) => node.isConnected || document.body.contains(node);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './dom-utils';

0 commit comments

Comments
 (0)