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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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) && display !== 'inline' && display !== 'contents';
  53. }
  54. function isTableElement(element) {
  55. return /^(table|td|th)$/.test(getNodeName(element));
  56. }
  57. function isTopLayer(element) {
  58. try {
  59. if (element.matches(':popover-open')) {
  60. return true;
  61. }
  62. } catch (_e) {
  63. // no-op
  64. }
  65. try {
  66. return element.matches(':modal');
  67. } catch (_e) {
  68. return false;
  69. }
  70. }
  71. const willChangeRe = /transform|translate|scale|rotate|perspective|filter/;
  72. const containRe = /paint|layout|strict|content/;
  73. const isNotNone = value => !!value && value !== 'none';
  74. let isWebKitValue;
  75. function isContainingBlock(elementOrCss) {
  76. const css = isElement(elementOrCss) ? getComputedStyle(elementOrCss) : elementOrCss;
  77. // https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block#identifying_the_containing_block
  78. // https://drafts.csswg.org/css-transforms-2/#individual-transforms
  79. return isNotNone(css.transform) || isNotNone(css.translate) || isNotNone(css.scale) || isNotNone(css.rotate) || isNotNone(css.perspective) || !isWebKit() && (isNotNone(css.backdropFilter) || isNotNone(css.filter)) || willChangeRe.test(css.willChange || '') || containRe.test(css.contain || '');
  80. }
  81. function getContainingBlock(element) {
  82. let currentNode = getParentNode(element);
  83. while (isHTMLElement(currentNode) && !isLastTraversableNode(currentNode)) {
  84. if (isContainingBlock(currentNode)) {
  85. return currentNode;
  86. } else if (isTopLayer(currentNode)) {
  87. return null;
  88. }
  89. currentNode = getParentNode(currentNode);
  90. }
  91. return null;
  92. }
  93. function isWebKit() {
  94. if (isWebKitValue == null) {
  95. isWebKitValue = typeof CSS !== 'undefined' && CSS.supports && CSS.supports('-webkit-backdrop-filter', 'none');
  96. }
  97. return isWebKitValue;
  98. }
  99. function isLastTraversableNode(node) {
  100. return /^(html|body|#document)$/.test(getNodeName(node));
  101. }
  102. function getComputedStyle(element) {
  103. return getWindow(element).getComputedStyle(element);
  104. }
  105. function getNodeScroll(element) {
  106. if (isElement(element)) {
  107. return {
  108. scrollLeft: element.scrollLeft,
  109. scrollTop: element.scrollTop
  110. };
  111. }
  112. return {
  113. scrollLeft: element.scrollX,
  114. scrollTop: element.scrollY
  115. };
  116. }
  117. function getParentNode(node) {
  118. if (getNodeName(node) === 'html') {
  119. return node;
  120. }
  121. const result =
  122. // Step into the shadow DOM of the parent of a slotted node.
  123. node.assignedSlot ||
  124. // DOM Element detected.
  125. node.parentNode ||
  126. // ShadowRoot detected.
  127. isShadowRoot(node) && node.host ||
  128. // Fallback.
  129. getDocumentElement(node);
  130. return isShadowRoot(result) ? result.host : result;
  131. }
  132. function getNearestOverflowAncestor(node) {
  133. const parentNode = getParentNode(node);
  134. if (isLastTraversableNode(parentNode)) {
  135. return node.ownerDocument ? node.ownerDocument.body : node.body;
  136. }
  137. if (isHTMLElement(parentNode) && isOverflowElement(parentNode)) {
  138. return parentNode;
  139. }
  140. return getNearestOverflowAncestor(parentNode);
  141. }
  142. function getOverflowAncestors(node, list, traverseIframes) {
  143. var _node$ownerDocument2;
  144. if (list === void 0) {
  145. list = [];
  146. }
  147. if (traverseIframes === void 0) {
  148. traverseIframes = true;
  149. }
  150. const scrollableAncestor = getNearestOverflowAncestor(node);
  151. const isBody = scrollableAncestor === ((_node$ownerDocument2 = node.ownerDocument) == null ? void 0 : _node$ownerDocument2.body);
  152. const win = getWindow(scrollableAncestor);
  153. if (isBody) {
  154. const frameElement = getFrameElement(win);
  155. return list.concat(win, win.visualViewport || [], isOverflowElement(scrollableAncestor) ? scrollableAncestor : [], frameElement && traverseIframes ? getOverflowAncestors(frameElement) : []);
  156. } else {
  157. return list.concat(scrollableAncestor, getOverflowAncestors(scrollableAncestor, [], traverseIframes));
  158. }
  159. }
  160. function getFrameElement(win) {
  161. return win.parent && Object.getPrototypeOf(win.parent) ? win.frameElement : null;
  162. }
  163. export { getComputedStyle, getContainingBlock, getDocumentElement, getFrameElement, getNearestOverflowAncestor, getNodeName, getNodeScroll, getOverflowAncestors, getParentNode, getWindow, isContainingBlock, isElement, isHTMLElement, isLastTraversableNode, isNode, isOverflowElement, isShadowRoot, isTableElement, isTopLayer, isWebKit };