floating-ui.utils.dom.umd.js 6.7 KB

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