floating-ui.utils.dom.esm.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. function hasWindow() {
  2. return typeof window !== 'undefined';
  3. }
  4. function getNodeName(node) {
  5. if (isNode(node)) {
  6. return (node.nodeName || '').toLowerCase();
  7. }
  8. // Mocked nodes in testing environments may not be instances of Node. By
  9. // returning `#document` an infinite loop won't occur.
  10. // https://github.com/floating-ui/floating-ui/issues/2317
  11. return '#document';
  12. }
  13. function getWindow(node) {
  14. var _node$ownerDocument;
  15. return (node == null || (_node$ownerDocument = node.ownerDocument) == null ? void 0 : _node$ownerDocument.defaultView) || window;
  16. }
  17. function getDocumentElement(node) {
  18. var _ref;
  19. return (_ref = (isNode(node) ? node.ownerDocument : node.document) || window.document) == null ? void 0 : _ref.documentElement;
  20. }
  21. function isNode(value) {
  22. if (!hasWindow()) {
  23. return false;
  24. }
  25. return value instanceof Node || value instanceof getWindow(value).Node;
  26. }
  27. function isElement(value) {
  28. if (!hasWindow()) {
  29. return false;
  30. }
  31. return value instanceof Element || value instanceof getWindow(value).Element;
  32. }
  33. function isHTMLElement(value) {
  34. if (!hasWindow()) {
  35. return false;
  36. }
  37. return value instanceof HTMLElement || value instanceof getWindow(value).HTMLElement;
  38. }
  39. function isShadowRoot(value) {
  40. if (!hasWindow() || typeof ShadowRoot === 'undefined') {
  41. return false;
  42. }
  43. return value instanceof ShadowRoot || value instanceof getWindow(value).ShadowRoot;
  44. }
  45. function isOverflowElement(element) {
  46. const {
  47. overflow,
  48. overflowX,
  49. overflowY,
  50. display
  51. } = getComputedStyle(element);
  52. return /auto|scroll|overlay|hidden|clip/.test(overflow + overflowY + overflowX) && !['inline', 'contents'].includes(display);
  53. }
  54. function isTableElement(element) {
  55. return ['table', 'td', 'th'].includes(getNodeName(element));
  56. }
  57. function isTopLayer(element) {
  58. return [':popover-open', ':modal'].some(selector => {
  59. try {
  60. return element.matches(selector);
  61. } catch (e) {
  62. return false;
  63. }
  64. });
  65. }
  66. function isContainingBlock(elementOrCss) {
  67. const webkit = isWebKit();
  68. const css = isElement(elementOrCss) ? getComputedStyle(elementOrCss) : elementOrCss;
  69. // https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block#identifying_the_containing_block
  70. return css.transform !== 'none' || css.perspective !== 'none' || (css.containerType ? css.containerType !== 'normal' : false) || !webkit && (css.backdropFilter ? css.backdropFilter !== 'none' : false) || !webkit && (css.filter ? css.filter !== 'none' : false) || ['transform', 'perspective', 'filter'].some(value => (css.willChange || '').includes(value)) || ['paint', 'layout', 'strict', 'content'].some(value => (css.contain || '').includes(value));
  71. }
  72. function getContainingBlock(element) {
  73. let currentNode = getParentNode(element);
  74. while (isHTMLElement(currentNode) && !isLastTraversableNode(currentNode)) {
  75. if (isContainingBlock(currentNode)) {
  76. return currentNode;
  77. } else if (isTopLayer(currentNode)) {
  78. return null;
  79. }
  80. currentNode = getParentNode(currentNode);
  81. }
  82. return null;
  83. }
  84. function isWebKit() {
  85. if (typeof CSS === 'undefined' || !CSS.supports) return false;
  86. return CSS.supports('-webkit-backdrop-filter', 'none');
  87. }
  88. function isLastTraversableNode(node) {
  89. return ['html', 'body', '#document'].includes(getNodeName(node));
  90. }
  91. function getComputedStyle(element) {
  92. return getWindow(element).getComputedStyle(element);
  93. }
  94. function getNodeScroll(element) {
  95. if (isElement(element)) {
  96. return {
  97. scrollLeft: element.scrollLeft,
  98. scrollTop: element.scrollTop
  99. };
  100. }
  101. return {
  102. scrollLeft: element.scrollX,
  103. scrollTop: element.scrollY
  104. };
  105. }
  106. function getParentNode(node) {
  107. if (getNodeName(node) === 'html') {
  108. return node;
  109. }
  110. const result =
  111. // Step into the shadow DOM of the parent of a slotted node.
  112. node.assignedSlot ||
  113. // DOM Element detected.
  114. node.parentNode ||
  115. // ShadowRoot detected.
  116. isShadowRoot(node) && node.host ||
  117. // Fallback.
  118. getDocumentElement(node);
  119. return isShadowRoot(result) ? result.host : result;
  120. }
  121. function getNearestOverflowAncestor(node) {
  122. const parentNode = getParentNode(node);
  123. if (isLastTraversableNode(parentNode)) {
  124. return node.ownerDocument ? node.ownerDocument.body : node.body;
  125. }
  126. if (isHTMLElement(parentNode) && isOverflowElement(parentNode)) {
  127. return parentNode;
  128. }
  129. return getNearestOverflowAncestor(parentNode);
  130. }
  131. function getOverflowAncestors(node, list, traverseIframes) {
  132. var _node$ownerDocument2;
  133. if (list === void 0) {
  134. list = [];
  135. }
  136. if (traverseIframes === void 0) {
  137. traverseIframes = true;
  138. }
  139. const scrollableAncestor = getNearestOverflowAncestor(node);
  140. const isBody = scrollableAncestor === ((_node$ownerDocument2 = node.ownerDocument) == null ? void 0 : _node$ownerDocument2.body);
  141. const win = getWindow(scrollableAncestor);
  142. if (isBody) {
  143. const frameElement = getFrameElement(win);
  144. return list.concat(win, win.visualViewport || [], isOverflowElement(scrollableAncestor) ? scrollableAncestor : [], frameElement && traverseIframes ? getOverflowAncestors(frameElement) : []);
  145. }
  146. return list.concat(scrollableAncestor, getOverflowAncestors(scrollableAncestor, [], traverseIframes));
  147. }
  148. function getFrameElement(win) {
  149. return win.parent && Object.getPrototypeOf(win.parent) ? win.frameElement : null;
  150. }
  151. export { getComputedStyle, getContainingBlock, getDocumentElement, getFrameElement, getNearestOverflowAncestor, getNodeName, getNodeScroll, getOverflowAncestors, getParentNode, getWindow, isContainingBlock, isElement, isHTMLElement, isLastTraversableNode, isNode, isOverflowElement, isShadowRoot, isTableElement, isTopLayer, isWebKit };