shadow-dom-B0oHn41l.mjs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. let shadowDomIsSupported;
  2. /** Checks whether the user's browser support Shadow DOM. */
  3. function _supportsShadowDom() {
  4. if (shadowDomIsSupported == null) {
  5. const head = typeof document !== 'undefined' ? document.head : null;
  6. shadowDomIsSupported = !!(head && (head.createShadowRoot || head.attachShadow));
  7. }
  8. return shadowDomIsSupported;
  9. }
  10. /** Gets the shadow root of an element, if supported and the element is inside the Shadow DOM. */
  11. function _getShadowRoot(element) {
  12. if (_supportsShadowDom()) {
  13. const rootNode = element.getRootNode ? element.getRootNode() : null;
  14. // Note that this should be caught by `_supportsShadowDom`, but some
  15. // teams have been able to hit this code path on unsupported browsers.
  16. if (typeof ShadowRoot !== 'undefined' && ShadowRoot && rootNode instanceof ShadowRoot) {
  17. return rootNode;
  18. }
  19. }
  20. return null;
  21. }
  22. /**
  23. * Gets the currently-focused element on the page while
  24. * also piercing through Shadow DOM boundaries.
  25. */
  26. function _getFocusedElementPierceShadowDom() {
  27. let activeElement = typeof document !== 'undefined' && document
  28. ? document.activeElement
  29. : null;
  30. while (activeElement && activeElement.shadowRoot) {
  31. const newActiveElement = activeElement.shadowRoot.activeElement;
  32. if (newActiveElement === activeElement) {
  33. break;
  34. }
  35. else {
  36. activeElement = newActiveElement;
  37. }
  38. }
  39. return activeElement;
  40. }
  41. /** Gets the target of an event while accounting for Shadow DOM. */
  42. function _getEventTarget(event) {
  43. // If an event is bound outside the Shadow DOM, the `event.target` will
  44. // point to the shadow root so we have to use `composedPath` instead.
  45. return (event.composedPath ? event.composedPath()[0] : event.target);
  46. }
  47. export { _getEventTarget as _, _getShadowRoot as a, _supportsShadowDom as b, _getFocusedElementPierceShadowDom as c };
  48. //# sourceMappingURL=shadow-dom-B0oHn41l.mjs.map