Skip to content

Commit 00d1539

Browse files
Шляпкин Григорий Владимировичroggervalf
Шляпкин Григорий Владимирович
authored andcommitted
fix(styles): add styles for shadow dom
fix #597
1 parent c11841c commit 00d1539

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

Diff for: rollup.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default {
4343
plugins: [simplevars(), nested()],
4444
modules: true
4545
}),
46-
sass({ insert: true }),
46+
sass({ insert: false }),
4747
url(),
4848
svgr(),
4949
babel({

Diff for: src/index.js

+30-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import nodeListToArray from './utils/nodeListToArray';
1919
import { generateUUID } from './utils/uuid';
2020

2121
/* CSS */
22-
import './index.scss';
22+
import baseCss from './index.scss';
2323
import { generateTooltipStyle } from './decorators/styler';
2424

2525
@staticMethods
@@ -146,6 +146,7 @@ class ReactTooltip extends React.Component {
146146

147147
this.bindListener(); // Bind listener for tooltip
148148
this.bindWindowEvents(resizeHide); // Bind global event for static method
149+
this.injectStyles(); // Inject styles for each DOM root having tooltip.
149150
}
150151

151152
static getDerivedStateFromProps(nextProps, prevState) {
@@ -173,6 +174,34 @@ class ReactTooltip extends React.Component {
173174
this.unbindWindowEvents();
174175
}
175176

177+
/* Look for the closest DOM root having tooltip and inject styles. */
178+
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) {
191+
const style = document.createElement('style');
192+
style.textContent = baseCss;
193+
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+
});
202+
}
203+
}
204+
176205
/**
177206
* Return if the mouse is on the tooltip.
178207
* @returns {boolean} true - mouse is on the tooltip

0 commit comments

Comments
 (0)