@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.
40 lines (37 loc) • 1.03 kB
JavaScript
import { isShadowRoot } from '@floating-ui/utils/dom';
export function activeElement(doc) {
let element = doc.activeElement;
while (element?.shadowRoot?.activeElement != null) {
element = element.shadowRoot.activeElement;
}
return element;
}
export 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 && isShadowRoot(rootNode)) {
let next = child;
while (next) {
if (parent === next) {
return true;
}
next = next.parentNode || next.host;
}
}
return false;
}
export 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;
}