You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
6.9 KiB

  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. // https://drafts.csswg.org/css-transforms-2/#individual-transforms
  76. return ['transform', 'translate', 'scale', 'rotate', 'perspective'].some(value => css[value] ? css[value] !== 'none' : false) || (css.containerType ? css.containerType !== 'normal' : false) || !webkit && (css.backdropFilter ? css.backdropFilter !== 'none' : false) || !webkit && (css.filter ? css.filter !== 'none' : false) || ['transform', 'translate', 'scale', 'rotate', 'perspective', 'filter'].some(value => (css.willChange || '').includes(value)) || ['paint', 'layout', 'strict', 'content'].some(value => (css.contain || '').includes(value));
  77. }
  78. function getContainingBlock(element) {
  79. let currentNode = getParentNode(element);
  80. while (isHTMLElement(currentNode) && !isLastTraversableNode(currentNode)) {
  81. if (isContainingBlock(currentNode)) {
  82. return currentNode;
  83. } else if (isTopLayer(currentNode)) {
  84. return null;
  85. }
  86. currentNode = getParentNode(currentNode);
  87. }
  88. return null;
  89. }
  90. function isWebKit() {
  91. if (typeof CSS === 'undefined' || !CSS.supports) return false;
  92. return CSS.supports('-webkit-backdrop-filter', 'none');
  93. }
  94. function isLastTraversableNode(node) {
  95. return ['html', 'body', '#document'].includes(getNodeName(node));
  96. }
  97. function getComputedStyle(element) {
  98. return getWindow(element).getComputedStyle(element);
  99. }
  100. function getNodeScroll(element) {
  101. if (isElement(element)) {
  102. return {
  103. scrollLeft: element.scrollLeft,
  104. scrollTop: element.scrollTop
  105. };
  106. }
  107. return {
  108. scrollLeft: element.scrollX,
  109. scrollTop: element.scrollY
  110. };
  111. }
  112. function getParentNode(node) {
  113. if (getNodeName(node) === 'html') {
  114. return node;
  115. }
  116. const result =
  117. // Step into the shadow DOM of the parent of a slotted node.
  118. node.assignedSlot ||
  119. // DOM Element detected.
  120. node.parentNode ||
  121. // ShadowRoot detected.
  122. isShadowRoot(node) && node.host ||
  123. // Fallback.
  124. getDocumentElement(node);
  125. return isShadowRoot(result) ? result.host : result;
  126. }
  127. function getNearestOverflowAncestor(node) {
  128. const parentNode = getParentNode(node);
  129. if (isLastTraversableNode(parentNode)) {
  130. return node.ownerDocument ? node.ownerDocument.body : node.body;
  131. }
  132. if (isHTMLElement(parentNode) && isOverflowElement(parentNode)) {
  133. return parentNode;
  134. }
  135. return getNearestOverflowAncestor(parentNode);
  136. }
  137. function getOverflowAncestors(node, list, traverseIframes) {
  138. var _node$ownerDocument2;
  139. if (list === void 0) {
  140. list = [];
  141. }
  142. if (traverseIframes === void 0) {
  143. traverseIframes = true;
  144. }
  145. const scrollableAncestor = getNearestOverflowAncestor(node);
  146. const isBody = scrollableAncestor === ((_node$ownerDocument2 = node.ownerDocument) == null ? void 0 : _node$ownerDocument2.body);
  147. const win = getWindow(scrollableAncestor);
  148. if (isBody) {
  149. const frameElement = getFrameElement(win);
  150. return list.concat(win, win.visualViewport || [], isOverflowElement(scrollableAncestor) ? scrollableAncestor : [], frameElement && traverseIframes ? getOverflowAncestors(frameElement) : []);
  151. }
  152. return list.concat(scrollableAncestor, getOverflowAncestors(scrollableAncestor, [], traverseIframes));
  153. }
  154. function getFrameElement(win) {
  155. return win.parent && Object.getPrototypeOf(win.parent) ? win.frameElement : null;
  156. }
  157. exports.getComputedStyle = getComputedStyle;
  158. exports.getContainingBlock = getContainingBlock;
  159. exports.getDocumentElement = getDocumentElement;
  160. exports.getFrameElement = getFrameElement;
  161. exports.getNearestOverflowAncestor = getNearestOverflowAncestor;
  162. exports.getNodeName = getNodeName;
  163. exports.getNodeScroll = getNodeScroll;
  164. exports.getOverflowAncestors = getOverflowAncestors;
  165. exports.getParentNode = getParentNode;
  166. exports.getWindow = getWindow;
  167. exports.isContainingBlock = isContainingBlock;
  168. exports.isElement = isElement;
  169. exports.isHTMLElement = isHTMLElement;
  170. exports.isLastTraversableNode = isLastTraversableNode;
  171. exports.isNode = isNode;
  172. exports.isOverflowElement = isOverflowElement;
  173. exports.isShadowRoot = isShadowRoot;
  174. exports.isTableElement = isTableElement;
  175. exports.isTopLayer = isTopLayer;
  176. exports.isWebKit = isWebKit;
  177. }));