@@ -19,7 +19,7 @@ import nodeListToArray from './utils/nodeListToArray';
19
19
import { generateUUID } from './utils/uuid' ;
20
20
21
21
/* CSS */
22
- import './index.scss' ;
22
+ import baseCss from './index.scss' ;
23
23
import { generateTooltipStyle } from './decorators/styler' ;
24
24
25
25
@staticMethods
@@ -146,6 +146,7 @@ class ReactTooltip extends React.Component {
146
146
147
147
this . bindListener ( ) ; // Bind listener for tooltip
148
148
this . bindWindowEvents ( resizeHide ) ; // Bind global event for static method
149
+ this . injectStyles ( ) ; // Inject styles for each DOM root having tooltip.
149
150
}
150
151
151
152
static getDerivedStateFromProps ( nextProps , prevState ) {
@@ -173,6 +174,34 @@ class ReactTooltip extends React.Component {
173
174
this . unbindWindowEvents ( ) ;
174
175
}
175
176
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
+
176
205
/**
177
206
* Return if the mouse is on the tooltip.
178
207
* @returns {boolean } true - mouse is on the tooltip
0 commit comments