50 lines
1.9 KiB
JavaScript
Executable File
50 lines
1.9 KiB
JavaScript
Executable File
let shadowDomIsSupported;
|
|
/** Checks whether the user's browser support Shadow DOM. */
|
|
function _supportsShadowDom() {
|
|
if (shadowDomIsSupported == null) {
|
|
const head = typeof document !== 'undefined' ? document.head : null;
|
|
shadowDomIsSupported = !!(head && (head.createShadowRoot || head.attachShadow));
|
|
}
|
|
return shadowDomIsSupported;
|
|
}
|
|
/** Gets the shadow root of an element, if supported and the element is inside the Shadow DOM. */
|
|
function _getShadowRoot(element) {
|
|
if (_supportsShadowDom()) {
|
|
const rootNode = element.getRootNode ? element.getRootNode() : null;
|
|
// Note that this should be caught by `_supportsShadowDom`, but some
|
|
// teams have been able to hit this code path on unsupported browsers.
|
|
if (typeof ShadowRoot !== 'undefined' && ShadowRoot && rootNode instanceof ShadowRoot) {
|
|
return rootNode;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
/**
|
|
* Gets the currently-focused element on the page while
|
|
* also piercing through Shadow DOM boundaries.
|
|
*/
|
|
function _getFocusedElementPierceShadowDom() {
|
|
let activeElement = typeof document !== 'undefined' && document
|
|
? document.activeElement
|
|
: null;
|
|
while (activeElement && activeElement.shadowRoot) {
|
|
const newActiveElement = activeElement.shadowRoot.activeElement;
|
|
if (newActiveElement === activeElement) {
|
|
break;
|
|
}
|
|
else {
|
|
activeElement = newActiveElement;
|
|
}
|
|
}
|
|
return activeElement;
|
|
}
|
|
/** Gets the target of an event while accounting for Shadow DOM. */
|
|
function _getEventTarget(event) {
|
|
// If an event is bound outside the Shadow DOM, the `event.target` will
|
|
// point to the shadow root so we have to use `composedPath` instead.
|
|
return (event.composedPath ? event.composedPath()[0] : event.target);
|
|
}
|
|
|
|
export { _getEventTarget, _getFocusedElementPierceShadowDom, _getShadowRoot, _supportsShadowDom };
|
|
//# sourceMappingURL=shadow-dom.mjs.map
|