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.

154 lines
5.7 KiB

1 month ago
  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. // https://drafts.csswg.org/css-transforms-2/#individual-transforms
  71. 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));
  72. }
  73. function getContainingBlock(element) {
  74. let currentNode = getParentNode(element);
  75. while (isHTMLElement(currentNode) && !isLastTraversableNode(currentNode)) {
  76. if (isContainingBlock(currentNode)) {
  77. return currentNode;
  78. } else if (isTopLayer(currentNode)) {
  79. return null;
  80. }
  81. currentNode = getParentNode(currentNode);
  82. }
  83. return null;
  84. }
  85. function isWebKit() {
  86. if (typeof CSS === 'undefined' || !CSS.supports) return false;
  87. return CSS.supports('-webkit-backdrop-filter', 'none');
  88. }
  89. function isLastTraversableNode(node) {
  90. return ['html', 'body', '#document'].includes(getNodeName(node));
  91. }
  92. function getComputedStyle(element) {
  93. return getWindow(element).getComputedStyle(element);
  94. }
  95. function getNodeScroll(element) {
  96. if (isElement(element)) {
  97. return {
  98. scrollLeft: element.scrollLeft,
  99. scrollTop: element.scrollTop
  100. };
  101. }
  102. return {
  103. scrollLeft: element.scrollX,
  104. scrollTop: element.scrollY
  105. };
  106. }
  107. function getParentNode(node) {
  108. if (getNodeName(node) === 'html') {
  109. return node;
  110. }
  111. const result =
  112. // Step into the shadow DOM of the parent of a slotted node.
  113. node.assignedSlot ||
  114. // DOM Element detected.
  115. node.parentNode ||
  116. // ShadowRoot detected.
  117. isShadowRoot(node) && node.host ||
  118. // Fallback.
  119. getDocumentElement(node);
  120. return isShadowRoot(result) ? result.host : result;
  121. }
  122. function getNearestOverflowAncestor(node) {
  123. const parentNode = getParentNode(node);
  124. if (isLastTraversableNode(parentNode)) {
  125. return node.ownerDocument ? node.ownerDocument.body : node.body;
  126. }
  127. if (isHTMLElement(parentNode) && isOverflowElement(parentNode)) {
  128. return parentNode;
  129. }
  130. return getNearestOverflowAncestor(parentNode);
  131. }
  132. function getOverflowAncestors(node, list, traverseIframes) {
  133. var _node$ownerDocument2;
  134. if (list === void 0) {
  135. list = [];
  136. }
  137. if (traverseIframes === void 0) {
  138. traverseIframes = true;
  139. }
  140. const scrollableAncestor = getNearestOverflowAncestor(node);
  141. const isBody = scrollableAncestor === ((_node$ownerDocument2 = node.ownerDocument) == null ? void 0 : _node$ownerDocument2.body);
  142. const win = getWindow(scrollableAncestor);
  143. if (isBody) {
  144. const frameElement = getFrameElement(win);
  145. return list.concat(win, win.visualViewport || [], isOverflowElement(scrollableAncestor) ? scrollableAncestor : [], frameElement && traverseIframes ? getOverflowAncestors(frameElement) : []);
  146. }
  147. return list.concat(scrollableAncestor, getOverflowAncestors(scrollableAncestor, [], traverseIframes));
  148. }
  149. function getFrameElement(win) {
  150. return win.parent && Object.getPrototypeOf(win.parent) ? win.frameElement : null;
  151. }
  152. export { getComputedStyle, getContainingBlock, getDocumentElement, getFrameElement, getNearestOverflowAncestor, getNodeName, getNodeScroll, getOverflowAncestors, getParentNode, getWindow, isContainingBlock, isElement, isHTMLElement, isLastTraversableNode, isNode, isOverflowElement, isShadowRoot, isTableElement, isTopLayer, isWebKit };