ahooks
Version:
react hooks library
31 lines (30 loc) • 840 B
JavaScript
import { getTargetElement } from '../utils/domTarget';
const checkIfAllInShadow = (targets) => {
return targets.every((item) => {
const targetElement = getTargetElement(item);
if (!targetElement) {
return false;
}
if (targetElement.getRootNode() instanceof ShadowRoot) {
return true;
}
return false;
});
};
const getShadow = (node) => {
if (!node) {
return document;
}
return node.getRootNode();
};
const getDocumentOrShadow = (target) => {
if (!target || !document.getRootNode) {
return document;
}
const targets = Array.isArray(target) ? target : [target];
if (checkIfAllInShadow(targets)) {
return getShadow(getTargetElement(targets[0]));
}
return document;
};
export default getDocumentOrShadow;