Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add styles for shadow dom #636

Merged
merged 1 commit into from
Nov 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default {
plugins: [simplevars(), nested()],
modules: true
}),
sass({ insert: true }),
sass({ insert: false }),
url(),
svgr(),
babel({
Expand Down
31 changes: 30 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import nodeListToArray from './utils/nodeListToArray';
import { generateUUID } from './utils/uuid';

/* CSS */
import './index.scss';
import baseCss from './index.scss';
import { generateTooltipStyle } from './decorators/styler';

@staticMethods
Expand Down Expand Up @@ -146,6 +146,7 @@ class ReactTooltip extends React.Component {

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

static getDerivedStateFromProps(nextProps, prevState) {
Expand Down Expand Up @@ -173,6 +174,34 @@ class ReactTooltip extends React.Component {
this.unbindWindowEvents();
}

/* Look for the closest DOM root having tooltip and inject styles. */
injectStyles() {
const { id } = this.props;
const targetArray = this.getTargetArray(id);
const domRoots = [];
targetArray.forEach(target => {
let parentNode = target.parentNode;
while (parentNode.parentNode && !parentNode.host) {
parentNode = parentNode.parentNode;
}
const head = parentNode.querySelector('head');
domRoots.push(head || parentNode);
});
if (domRoots.length) {
const style = document.createElement('style');
style.textContent = baseCss;
style.setAttribute('data-react-tooltip', 'true');
domRoots
.filter((item, idx, src) => src.indexOf(item) === idx)
.forEach(domRoot => {
// Prevent styles duplication.
if (!domRoot.querySelector('style[data-react-tooltip]')) {
domRoot.appendChild(style);
}
});
}
}

/**
* Return if the mouse is on the tooltip.
* @returns {boolean} true - mouse is on the tooltip
Expand Down