@base-ui/react
Version:
Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.
48 lines (44 loc) • 1.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.activeElement = activeElement;
exports.contains = contains;
exports.getTarget = getTarget;
var _dom = require("@floating-ui/utils/dom");
function activeElement(doc) {
let element = doc.activeElement;
while (element?.shadowRoot?.activeElement != null) {
element = element.shadowRoot.activeElement;
}
return element;
}
function contains(parent, child) {
if (!parent || !child) {
return false;
}
const rootNode = child.getRootNode?.();
// First, attempt with the faster native method.
if (parent.contains(child)) {
return true;
}
// Then fall back to traversing out of shadow roots when needed.
if (rootNode && (0, _dom.isShadowRoot)(rootNode)) {
let next = child;
while (next) {
if (parent === next) {
return true;
}
next = next.parentNode || next.host;
}
}
return false;
}
function getTarget(event) {
if ('composedPath' in event) {
return event.composedPath()[0];
}
// TS assumes `composedPath()` always exists, but older browsers without
// shadow DOM support still fall back to `target`.
return event.target;
}