Skip to content

Commit 1e10cce

Browse files
authored
fix(styles): change style injection way (#668)
fix #650
1 parent 13ade50 commit 1e10cce

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

Diff for: src/index.js

+26-20
Original file line numberDiff line numberDiff line change
@@ -176,29 +176,35 @@ class ReactTooltip extends React.Component {
176176

177177
/* Look for the closest DOM root having tooltip and inject styles. */
178178
injectStyles() {
179-
const { id } = this.props;
180-
const targetArray = this.getTargetArray(id);
181-
const domRoots = [];
182-
targetArray.forEach(target => {
183-
let parentNode = target.parentNode;
184-
while (parentNode.parentNode && !parentNode.host) {
185-
parentNode = parentNode.parentNode;
186-
}
187-
const head = parentNode.querySelector('head');
188-
domRoots.push(head || parentNode);
189-
});
190-
if (domRoots.length) {
179+
const { tooltipRef } = this;
180+
if (!tooltipRef) {
181+
return;
182+
}
183+
184+
let parentNode = tooltipRef.parentNode;
185+
while (parentNode.parentNode) {
186+
parentNode = parentNode.parentNode;
187+
}
188+
189+
let domRoot;
190+
191+
switch (parentNode.constructor.name) {
192+
case 'HTMLDocument':
193+
domRoot = parentNode.head;
194+
break;
195+
case 'ShadowRoot':
196+
default:
197+
domRoot = parentNode;
198+
break;
199+
}
200+
201+
// Prevent styles duplication.
202+
if (!domRoot.querySelector('style[data-react-tooltip]')) {
191203
const style = document.createElement('style');
192204
style.textContent = baseCss;
193205
style.setAttribute('data-react-tooltip', 'true');
194-
domRoots
195-
.filter((item, idx, src) => src.indexOf(item) === idx)
196-
.forEach(domRoot => {
197-
// Prevent styles duplication.
198-
if (!domRoot.querySelector('style[data-react-tooltip]')) {
199-
domRoot.appendChild(style);
200-
}
201-
});
206+
207+
domRoot.appendChild(style);
202208
}
203209
}
204210

0 commit comments

Comments
 (0)