floating-ui.dom.umd.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@floating-ui/core')) :
  3. typeof define === 'function' && define.amd ? define(['exports', '@floating-ui/core'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.FloatingUIDOM = {}, global.FloatingUICore));
  5. })(this, (function (exports, core) { 'use strict';
  6. /**
  7. * Custom positioning reference element.
  8. * @see https://floating-ui.com/docs/virtual-elements
  9. */
  10. const min = Math.min;
  11. const max = Math.max;
  12. const round = Math.round;
  13. const floor = Math.floor;
  14. const createCoords = v => ({
  15. x: v,
  16. y: v
  17. });
  18. function hasWindow() {
  19. return typeof window !== 'undefined';
  20. }
  21. function getNodeName(node) {
  22. if (isNode(node)) {
  23. return (node.nodeName || '').toLowerCase();
  24. }
  25. // Mocked nodes in testing environments may not be instances of Node. By
  26. // returning `#document` an infinite loop won't occur.
  27. // https://github.com/floating-ui/floating-ui/issues/2317
  28. return '#document';
  29. }
  30. function getWindow(node) {
  31. var _node$ownerDocument;
  32. return (node == null || (_node$ownerDocument = node.ownerDocument) == null ? void 0 : _node$ownerDocument.defaultView) || window;
  33. }
  34. function getDocumentElement(node) {
  35. var _ref;
  36. return (_ref = (isNode(node) ? node.ownerDocument : node.document) || window.document) == null ? void 0 : _ref.documentElement;
  37. }
  38. function isNode(value) {
  39. if (!hasWindow()) {
  40. return false;
  41. }
  42. return value instanceof Node || value instanceof getWindow(value).Node;
  43. }
  44. function isElement(value) {
  45. if (!hasWindow()) {
  46. return false;
  47. }
  48. return value instanceof Element || value instanceof getWindow(value).Element;
  49. }
  50. function isHTMLElement(value) {
  51. if (!hasWindow()) {
  52. return false;
  53. }
  54. return value instanceof HTMLElement || value instanceof getWindow(value).HTMLElement;
  55. }
  56. function isShadowRoot(value) {
  57. if (!hasWindow() || typeof ShadowRoot === 'undefined') {
  58. return false;
  59. }
  60. return value instanceof ShadowRoot || value instanceof getWindow(value).ShadowRoot;
  61. }
  62. function isOverflowElement(element) {
  63. const {
  64. overflow,
  65. overflowX,
  66. overflowY,
  67. display
  68. } = getComputedStyle(element);
  69. return /auto|scroll|overlay|hidden|clip/.test(overflow + overflowY + overflowX) && !['inline', 'contents'].includes(display);
  70. }
  71. function isTableElement(element) {
  72. return ['table', 'td', 'th'].includes(getNodeName(element));
  73. }
  74. function isTopLayer(element) {
  75. return [':popover-open', ':modal'].some(selector => {
  76. try {
  77. return element.matches(selector);
  78. } catch (_e) {
  79. return false;
  80. }
  81. });
  82. }
  83. function isContainingBlock(elementOrCss) {
  84. const webkit = isWebKit();
  85. const css = isElement(elementOrCss) ? getComputedStyle(elementOrCss) : elementOrCss;
  86. // https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block#identifying_the_containing_block
  87. // https://drafts.csswg.org/css-transforms-2/#individual-transforms
  88. 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));
  89. }
  90. function getContainingBlock(element) {
  91. let currentNode = getParentNode(element);
  92. while (isHTMLElement(currentNode) && !isLastTraversableNode(currentNode)) {
  93. if (isContainingBlock(currentNode)) {
  94. return currentNode;
  95. } else if (isTopLayer(currentNode)) {
  96. return null;
  97. }
  98. currentNode = getParentNode(currentNode);
  99. }
  100. return null;
  101. }
  102. function isWebKit() {
  103. if (typeof CSS === 'undefined' || !CSS.supports) return false;
  104. return CSS.supports('-webkit-backdrop-filter', 'none');
  105. }
  106. function isLastTraversableNode(node) {
  107. return ['html', 'body', '#document'].includes(getNodeName(node));
  108. }
  109. function getComputedStyle(element) {
  110. return getWindow(element).getComputedStyle(element);
  111. }
  112. function getNodeScroll(element) {
  113. if (isElement(element)) {
  114. return {
  115. scrollLeft: element.scrollLeft,
  116. scrollTop: element.scrollTop
  117. };
  118. }
  119. return {
  120. scrollLeft: element.scrollX,
  121. scrollTop: element.scrollY
  122. };
  123. }
  124. function getParentNode(node) {
  125. if (getNodeName(node) === 'html') {
  126. return node;
  127. }
  128. const result =
  129. // Step into the shadow DOM of the parent of a slotted node.
  130. node.assignedSlot ||
  131. // DOM Element detected.
  132. node.parentNode ||
  133. // ShadowRoot detected.
  134. isShadowRoot(node) && node.host ||
  135. // Fallback.
  136. getDocumentElement(node);
  137. return isShadowRoot(result) ? result.host : result;
  138. }
  139. function getNearestOverflowAncestor(node) {
  140. const parentNode = getParentNode(node);
  141. if (isLastTraversableNode(parentNode)) {
  142. return node.ownerDocument ? node.ownerDocument.body : node.body;
  143. }
  144. if (isHTMLElement(parentNode) && isOverflowElement(parentNode)) {
  145. return parentNode;
  146. }
  147. return getNearestOverflowAncestor(parentNode);
  148. }
  149. function getOverflowAncestors(node, list, traverseIframes) {
  150. var _node$ownerDocument2;
  151. if (list === void 0) {
  152. list = [];
  153. }
  154. if (traverseIframes === void 0) {
  155. traverseIframes = true;
  156. }
  157. const scrollableAncestor = getNearestOverflowAncestor(node);
  158. const isBody = scrollableAncestor === ((_node$ownerDocument2 = node.ownerDocument) == null ? void 0 : _node$ownerDocument2.body);
  159. const win = getWindow(scrollableAncestor);
  160. if (isBody) {
  161. const frameElement = getFrameElement(win);
  162. return list.concat(win, win.visualViewport || [], isOverflowElement(scrollableAncestor) ? scrollableAncestor : [], frameElement && traverseIframes ? getOverflowAncestors(frameElement) : []);
  163. }
  164. return list.concat(scrollableAncestor, getOverflowAncestors(scrollableAncestor, [], traverseIframes));
  165. }
  166. function getFrameElement(win) {
  167. return win.parent && Object.getPrototypeOf(win.parent) ? win.frameElement : null;
  168. }
  169. function getCssDimensions(element) {
  170. const css = getComputedStyle(element);
  171. // In testing environments, the `width` and `height` properties are empty
  172. // strings for SVG elements, returning NaN. Fallback to `0` in this case.
  173. let width = parseFloat(css.width) || 0;
  174. let height = parseFloat(css.height) || 0;
  175. const hasOffset = isHTMLElement(element);
  176. const offsetWidth = hasOffset ? element.offsetWidth : width;
  177. const offsetHeight = hasOffset ? element.offsetHeight : height;
  178. const shouldFallback = round(width) !== offsetWidth || round(height) !== offsetHeight;
  179. if (shouldFallback) {
  180. width = offsetWidth;
  181. height = offsetHeight;
  182. }
  183. return {
  184. width,
  185. height,
  186. $: shouldFallback
  187. };
  188. }
  189. function unwrapElement(element) {
  190. return !isElement(element) ? element.contextElement : element;
  191. }
  192. function getScale(element) {
  193. const domElement = unwrapElement(element);
  194. if (!isHTMLElement(domElement)) {
  195. return createCoords(1);
  196. }
  197. const rect = domElement.getBoundingClientRect();
  198. const {
  199. width,
  200. height,
  201. $
  202. } = getCssDimensions(domElement);
  203. let x = ($ ? round(rect.width) : rect.width) / width;
  204. let y = ($ ? round(rect.height) : rect.height) / height;
  205. // 0, NaN, or Infinity should always fallback to 1.
  206. if (!x || !Number.isFinite(x)) {
  207. x = 1;
  208. }
  209. if (!y || !Number.isFinite(y)) {
  210. y = 1;
  211. }
  212. return {
  213. x,
  214. y
  215. };
  216. }
  217. const noOffsets = /*#__PURE__*/createCoords(0);
  218. function getVisualOffsets(element) {
  219. const win = getWindow(element);
  220. if (!isWebKit() || !win.visualViewport) {
  221. return noOffsets;
  222. }
  223. return {
  224. x: win.visualViewport.offsetLeft,
  225. y: win.visualViewport.offsetTop
  226. };
  227. }
  228. function shouldAddVisualOffsets(element, isFixed, floatingOffsetParent) {
  229. if (isFixed === void 0) {
  230. isFixed = false;
  231. }
  232. if (!floatingOffsetParent || isFixed && floatingOffsetParent !== getWindow(element)) {
  233. return false;
  234. }
  235. return isFixed;
  236. }
  237. function getBoundingClientRect(element, includeScale, isFixedStrategy, offsetParent) {
  238. if (includeScale === void 0) {
  239. includeScale = false;
  240. }
  241. if (isFixedStrategy === void 0) {
  242. isFixedStrategy = false;
  243. }
  244. const clientRect = element.getBoundingClientRect();
  245. const domElement = unwrapElement(element);
  246. let scale = createCoords(1);
  247. if (includeScale) {
  248. if (offsetParent) {
  249. if (isElement(offsetParent)) {
  250. scale = getScale(offsetParent);
  251. }
  252. } else {
  253. scale = getScale(element);
  254. }
  255. }
  256. const visualOffsets = shouldAddVisualOffsets(domElement, isFixedStrategy, offsetParent) ? getVisualOffsets(domElement) : createCoords(0);
  257. let x = (clientRect.left + visualOffsets.x) / scale.x;
  258. let y = (clientRect.top + visualOffsets.y) / scale.y;
  259. let width = clientRect.width / scale.x;
  260. let height = clientRect.height / scale.y;
  261. if (domElement) {
  262. const win = getWindow(domElement);
  263. const offsetWin = offsetParent && isElement(offsetParent) ? getWindow(offsetParent) : offsetParent;
  264. let currentWin = win;
  265. let currentIFrame = getFrameElement(currentWin);
  266. while (currentIFrame && offsetParent && offsetWin !== currentWin) {
  267. const iframeScale = getScale(currentIFrame);
  268. const iframeRect = currentIFrame.getBoundingClientRect();
  269. const css = getComputedStyle(currentIFrame);
  270. const left = iframeRect.left + (currentIFrame.clientLeft + parseFloat(css.paddingLeft)) * iframeScale.x;
  271. const top = iframeRect.top + (currentIFrame.clientTop + parseFloat(css.paddingTop)) * iframeScale.y;
  272. x *= iframeScale.x;
  273. y *= iframeScale.y;
  274. width *= iframeScale.x;
  275. height *= iframeScale.y;
  276. x += left;
  277. y += top;
  278. currentWin = getWindow(currentIFrame);
  279. currentIFrame = getFrameElement(currentWin);
  280. }
  281. }
  282. return core.rectToClientRect({
  283. width,
  284. height,
  285. x,
  286. y
  287. });
  288. }
  289. // If <html> has a CSS width greater than the viewport, then this will be
  290. // incorrect for RTL.
  291. function getWindowScrollBarX(element, rect) {
  292. const leftScroll = getNodeScroll(element).scrollLeft;
  293. if (!rect) {
  294. return getBoundingClientRect(getDocumentElement(element)).left + leftScroll;
  295. }
  296. return rect.left + leftScroll;
  297. }
  298. function getHTMLOffset(documentElement, scroll, ignoreScrollbarX) {
  299. if (ignoreScrollbarX === void 0) {
  300. ignoreScrollbarX = false;
  301. }
  302. const htmlRect = documentElement.getBoundingClientRect();
  303. const x = htmlRect.left + scroll.scrollLeft - (ignoreScrollbarX ? 0 :
  304. // RTL <body> scrollbar.
  305. getWindowScrollBarX(documentElement, htmlRect));
  306. const y = htmlRect.top + scroll.scrollTop;
  307. return {
  308. x,
  309. y
  310. };
  311. }
  312. function convertOffsetParentRelativeRectToViewportRelativeRect(_ref) {
  313. let {
  314. elements,
  315. rect,
  316. offsetParent,
  317. strategy
  318. } = _ref;
  319. const isFixed = strategy === 'fixed';
  320. const documentElement = getDocumentElement(offsetParent);
  321. const topLayer = elements ? isTopLayer(elements.floating) : false;
  322. if (offsetParent === documentElement || topLayer && isFixed) {
  323. return rect;
  324. }
  325. let scroll = {
  326. scrollLeft: 0,
  327. scrollTop: 0
  328. };
  329. let scale = createCoords(1);
  330. const offsets = createCoords(0);
  331. const isOffsetParentAnElement = isHTMLElement(offsetParent);
  332. if (isOffsetParentAnElement || !isOffsetParentAnElement && !isFixed) {
  333. if (getNodeName(offsetParent) !== 'body' || isOverflowElement(documentElement)) {
  334. scroll = getNodeScroll(offsetParent);
  335. }
  336. if (isHTMLElement(offsetParent)) {
  337. const offsetRect = getBoundingClientRect(offsetParent);
  338. scale = getScale(offsetParent);
  339. offsets.x = offsetRect.x + offsetParent.clientLeft;
  340. offsets.y = offsetRect.y + offsetParent.clientTop;
  341. }
  342. }
  343. const htmlOffset = documentElement && !isOffsetParentAnElement && !isFixed ? getHTMLOffset(documentElement, scroll, true) : createCoords(0);
  344. return {
  345. width: rect.width * scale.x,
  346. height: rect.height * scale.y,
  347. x: rect.x * scale.x - scroll.scrollLeft * scale.x + offsets.x + htmlOffset.x,
  348. y: rect.y * scale.y - scroll.scrollTop * scale.y + offsets.y + htmlOffset.y
  349. };
  350. }
  351. function getClientRects(element) {
  352. return Array.from(element.getClientRects());
  353. }
  354. // Gets the entire size of the scrollable document area, even extending outside
  355. // of the `<html>` and `<body>` rect bounds if horizontally scrollable.
  356. function getDocumentRect(element) {
  357. const html = getDocumentElement(element);
  358. const scroll = getNodeScroll(element);
  359. const body = element.ownerDocument.body;
  360. const width = max(html.scrollWidth, html.clientWidth, body.scrollWidth, body.clientWidth);
  361. const height = max(html.scrollHeight, html.clientHeight, body.scrollHeight, body.clientHeight);
  362. let x = -scroll.scrollLeft + getWindowScrollBarX(element);
  363. const y = -scroll.scrollTop;
  364. if (getComputedStyle(body).direction === 'rtl') {
  365. x += max(html.clientWidth, body.clientWidth) - width;
  366. }
  367. return {
  368. width,
  369. height,
  370. x,
  371. y
  372. };
  373. }
  374. function getViewportRect(element, strategy) {
  375. const win = getWindow(element);
  376. const html = getDocumentElement(element);
  377. const visualViewport = win.visualViewport;
  378. let width = html.clientWidth;
  379. let height = html.clientHeight;
  380. let x = 0;
  381. let y = 0;
  382. if (visualViewport) {
  383. width = visualViewport.width;
  384. height = visualViewport.height;
  385. const visualViewportBased = isWebKit();
  386. if (!visualViewportBased || visualViewportBased && strategy === 'fixed') {
  387. x = visualViewport.offsetLeft;
  388. y = visualViewport.offsetTop;
  389. }
  390. }
  391. return {
  392. width,
  393. height,
  394. x,
  395. y
  396. };
  397. }
  398. // Returns the inner client rect, subtracting scrollbars if present.
  399. function getInnerBoundingClientRect(element, strategy) {
  400. const clientRect = getBoundingClientRect(element, true, strategy === 'fixed');
  401. const top = clientRect.top + element.clientTop;
  402. const left = clientRect.left + element.clientLeft;
  403. const scale = isHTMLElement(element) ? getScale(element) : createCoords(1);
  404. const width = element.clientWidth * scale.x;
  405. const height = element.clientHeight * scale.y;
  406. const x = left * scale.x;
  407. const y = top * scale.y;
  408. return {
  409. width,
  410. height,
  411. x,
  412. y
  413. };
  414. }
  415. function getClientRectFromClippingAncestor(element, clippingAncestor, strategy) {
  416. let rect;
  417. if (clippingAncestor === 'viewport') {
  418. rect = getViewportRect(element, strategy);
  419. } else if (clippingAncestor === 'document') {
  420. rect = getDocumentRect(getDocumentElement(element));
  421. } else if (isElement(clippingAncestor)) {
  422. rect = getInnerBoundingClientRect(clippingAncestor, strategy);
  423. } else {
  424. const visualOffsets = getVisualOffsets(element);
  425. rect = {
  426. x: clippingAncestor.x - visualOffsets.x,
  427. y: clippingAncestor.y - visualOffsets.y,
  428. width: clippingAncestor.width,
  429. height: clippingAncestor.height
  430. };
  431. }
  432. return core.rectToClientRect(rect);
  433. }
  434. function hasFixedPositionAncestor(element, stopNode) {
  435. const parentNode = getParentNode(element);
  436. if (parentNode === stopNode || !isElement(parentNode) || isLastTraversableNode(parentNode)) {
  437. return false;
  438. }
  439. return getComputedStyle(parentNode).position === 'fixed' || hasFixedPositionAncestor(parentNode, stopNode);
  440. }
  441. // A "clipping ancestor" is an `overflow` element with the characteristic of
  442. // clipping (or hiding) child elements. This returns all clipping ancestors
  443. // of the given element up the tree.
  444. function getClippingElementAncestors(element, cache) {
  445. const cachedResult = cache.get(element);
  446. if (cachedResult) {
  447. return cachedResult;
  448. }
  449. let result = getOverflowAncestors(element, [], false).filter(el => isElement(el) && getNodeName(el) !== 'body');
  450. let currentContainingBlockComputedStyle = null;
  451. const elementIsFixed = getComputedStyle(element).position === 'fixed';
  452. let currentNode = elementIsFixed ? getParentNode(element) : element;
  453. // https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block#identifying_the_containing_block
  454. while (isElement(currentNode) && !isLastTraversableNode(currentNode)) {
  455. const computedStyle = getComputedStyle(currentNode);
  456. const currentNodeIsContaining = isContainingBlock(currentNode);
  457. if (!currentNodeIsContaining && computedStyle.position === 'fixed') {
  458. currentContainingBlockComputedStyle = null;
  459. }
  460. const shouldDropCurrentNode = elementIsFixed ? !currentNodeIsContaining && !currentContainingBlockComputedStyle : !currentNodeIsContaining && computedStyle.position === 'static' && !!currentContainingBlockComputedStyle && ['absolute', 'fixed'].includes(currentContainingBlockComputedStyle.position) || isOverflowElement(currentNode) && !currentNodeIsContaining && hasFixedPositionAncestor(element, currentNode);
  461. if (shouldDropCurrentNode) {
  462. // Drop non-containing blocks.
  463. result = result.filter(ancestor => ancestor !== currentNode);
  464. } else {
  465. // Record last containing block for next iteration.
  466. currentContainingBlockComputedStyle = computedStyle;
  467. }
  468. currentNode = getParentNode(currentNode);
  469. }
  470. cache.set(element, result);
  471. return result;
  472. }
  473. // Gets the maximum area that the element is visible in due to any number of
  474. // clipping ancestors.
  475. function getClippingRect(_ref) {
  476. let {
  477. element,
  478. boundary,
  479. rootBoundary,
  480. strategy
  481. } = _ref;
  482. const elementClippingAncestors = boundary === 'clippingAncestors' ? isTopLayer(element) ? [] : getClippingElementAncestors(element, this._c) : [].concat(boundary);
  483. const clippingAncestors = [...elementClippingAncestors, rootBoundary];
  484. const firstClippingAncestor = clippingAncestors[0];
  485. const clippingRect = clippingAncestors.reduce((accRect, clippingAncestor) => {
  486. const rect = getClientRectFromClippingAncestor(element, clippingAncestor, strategy);
  487. accRect.top = max(rect.top, accRect.top);
  488. accRect.right = min(rect.right, accRect.right);
  489. accRect.bottom = min(rect.bottom, accRect.bottom);
  490. accRect.left = max(rect.left, accRect.left);
  491. return accRect;
  492. }, getClientRectFromClippingAncestor(element, firstClippingAncestor, strategy));
  493. return {
  494. width: clippingRect.right - clippingRect.left,
  495. height: clippingRect.bottom - clippingRect.top,
  496. x: clippingRect.left,
  497. y: clippingRect.top
  498. };
  499. }
  500. function getDimensions(element) {
  501. const {
  502. width,
  503. height
  504. } = getCssDimensions(element);
  505. return {
  506. width,
  507. height
  508. };
  509. }
  510. function getRectRelativeToOffsetParent(element, offsetParent, strategy) {
  511. const isOffsetParentAnElement = isHTMLElement(offsetParent);
  512. const documentElement = getDocumentElement(offsetParent);
  513. const isFixed = strategy === 'fixed';
  514. const rect = getBoundingClientRect(element, true, isFixed, offsetParent);
  515. let scroll = {
  516. scrollLeft: 0,
  517. scrollTop: 0
  518. };
  519. const offsets = createCoords(0);
  520. // If the <body> scrollbar appears on the left (e.g. RTL systems). Use
  521. // Firefox with layout.scrollbar.side = 3 in about:config to test this.
  522. function setLeftRTLScrollbarOffset() {
  523. offsets.x = getWindowScrollBarX(documentElement);
  524. }
  525. if (isOffsetParentAnElement || !isOffsetParentAnElement && !isFixed) {
  526. if (getNodeName(offsetParent) !== 'body' || isOverflowElement(documentElement)) {
  527. scroll = getNodeScroll(offsetParent);
  528. }
  529. if (isOffsetParentAnElement) {
  530. const offsetRect = getBoundingClientRect(offsetParent, true, isFixed, offsetParent);
  531. offsets.x = offsetRect.x + offsetParent.clientLeft;
  532. offsets.y = offsetRect.y + offsetParent.clientTop;
  533. } else if (documentElement) {
  534. setLeftRTLScrollbarOffset();
  535. }
  536. }
  537. if (isFixed && !isOffsetParentAnElement && documentElement) {
  538. setLeftRTLScrollbarOffset();
  539. }
  540. const htmlOffset = documentElement && !isOffsetParentAnElement && !isFixed ? getHTMLOffset(documentElement, scroll) : createCoords(0);
  541. const x = rect.left + scroll.scrollLeft - offsets.x - htmlOffset.x;
  542. const y = rect.top + scroll.scrollTop - offsets.y - htmlOffset.y;
  543. return {
  544. x,
  545. y,
  546. width: rect.width,
  547. height: rect.height
  548. };
  549. }
  550. function isStaticPositioned(element) {
  551. return getComputedStyle(element).position === 'static';
  552. }
  553. function getTrueOffsetParent(element, polyfill) {
  554. if (!isHTMLElement(element) || getComputedStyle(element).position === 'fixed') {
  555. return null;
  556. }
  557. if (polyfill) {
  558. return polyfill(element);
  559. }
  560. let rawOffsetParent = element.offsetParent;
  561. // Firefox returns the <html> element as the offsetParent if it's non-static,
  562. // while Chrome and Safari return the <body> element. The <body> element must
  563. // be used to perform the correct calculations even if the <html> element is
  564. // non-static.
  565. if (getDocumentElement(element) === rawOffsetParent) {
  566. rawOffsetParent = rawOffsetParent.ownerDocument.body;
  567. }
  568. return rawOffsetParent;
  569. }
  570. // Gets the closest ancestor positioned element. Handles some edge cases,
  571. // such as table ancestors and cross browser bugs.
  572. function getOffsetParent(element, polyfill) {
  573. const win = getWindow(element);
  574. if (isTopLayer(element)) {
  575. return win;
  576. }
  577. if (!isHTMLElement(element)) {
  578. let svgOffsetParent = getParentNode(element);
  579. while (svgOffsetParent && !isLastTraversableNode(svgOffsetParent)) {
  580. if (isElement(svgOffsetParent) && !isStaticPositioned(svgOffsetParent)) {
  581. return svgOffsetParent;
  582. }
  583. svgOffsetParent = getParentNode(svgOffsetParent);
  584. }
  585. return win;
  586. }
  587. let offsetParent = getTrueOffsetParent(element, polyfill);
  588. while (offsetParent && isTableElement(offsetParent) && isStaticPositioned(offsetParent)) {
  589. offsetParent = getTrueOffsetParent(offsetParent, polyfill);
  590. }
  591. if (offsetParent && isLastTraversableNode(offsetParent) && isStaticPositioned(offsetParent) && !isContainingBlock(offsetParent)) {
  592. return win;
  593. }
  594. return offsetParent || getContainingBlock(element) || win;
  595. }
  596. const getElementRects = async function (data) {
  597. const getOffsetParentFn = this.getOffsetParent || getOffsetParent;
  598. const getDimensionsFn = this.getDimensions;
  599. const floatingDimensions = await getDimensionsFn(data.floating);
  600. return {
  601. reference: getRectRelativeToOffsetParent(data.reference, await getOffsetParentFn(data.floating), data.strategy),
  602. floating: {
  603. x: 0,
  604. y: 0,
  605. width: floatingDimensions.width,
  606. height: floatingDimensions.height
  607. }
  608. };
  609. };
  610. function isRTL(element) {
  611. return getComputedStyle(element).direction === 'rtl';
  612. }
  613. const platform = {
  614. convertOffsetParentRelativeRectToViewportRelativeRect,
  615. getDocumentElement,
  616. getClippingRect,
  617. getOffsetParent,
  618. getElementRects,
  619. getClientRects,
  620. getDimensions,
  621. getScale,
  622. isElement,
  623. isRTL
  624. };
  625. function rectsAreEqual(a, b) {
  626. return a.x === b.x && a.y === b.y && a.width === b.width && a.height === b.height;
  627. }
  628. // https://samthor.au/2021/observing-dom/
  629. function observeMove(element, onMove) {
  630. let io = null;
  631. let timeoutId;
  632. const root = getDocumentElement(element);
  633. function cleanup() {
  634. var _io;
  635. clearTimeout(timeoutId);
  636. (_io = io) == null || _io.disconnect();
  637. io = null;
  638. }
  639. function refresh(skip, threshold) {
  640. if (skip === void 0) {
  641. skip = false;
  642. }
  643. if (threshold === void 0) {
  644. threshold = 1;
  645. }
  646. cleanup();
  647. const elementRectForRootMargin = element.getBoundingClientRect();
  648. const {
  649. left,
  650. top,
  651. width,
  652. height
  653. } = elementRectForRootMargin;
  654. if (!skip) {
  655. onMove();
  656. }
  657. if (!width || !height) {
  658. return;
  659. }
  660. const insetTop = floor(top);
  661. const insetRight = floor(root.clientWidth - (left + width));
  662. const insetBottom = floor(root.clientHeight - (top + height));
  663. const insetLeft = floor(left);
  664. const rootMargin = -insetTop + "px " + -insetRight + "px " + -insetBottom + "px " + -insetLeft + "px";
  665. const options = {
  666. rootMargin,
  667. threshold: max(0, min(1, threshold)) || 1
  668. };
  669. let isFirstUpdate = true;
  670. function handleObserve(entries) {
  671. const ratio = entries[0].intersectionRatio;
  672. if (ratio !== threshold) {
  673. if (!isFirstUpdate) {
  674. return refresh();
  675. }
  676. if (!ratio) {
  677. // If the reference is clipped, the ratio is 0. Throttle the refresh
  678. // to prevent an infinite loop of updates.
  679. timeoutId = setTimeout(() => {
  680. refresh(false, 1e-7);
  681. }, 1000);
  682. } else {
  683. refresh(false, ratio);
  684. }
  685. }
  686. if (ratio === 1 && !rectsAreEqual(elementRectForRootMargin, element.getBoundingClientRect())) {
  687. // It's possible that even though the ratio is reported as 1, the
  688. // element is not actually fully within the IntersectionObserver's root
  689. // area anymore. This can happen under performance constraints. This may
  690. // be a bug in the browser's IntersectionObserver implementation. To
  691. // work around this, we compare the element's bounding rect now with
  692. // what it was at the time we created the IntersectionObserver. If they
  693. // are not equal then the element moved, so we refresh.
  694. refresh();
  695. }
  696. isFirstUpdate = false;
  697. }
  698. // Older browsers don't support a `document` as the root and will throw an
  699. // error.
  700. try {
  701. io = new IntersectionObserver(handleObserve, {
  702. ...options,
  703. // Handle <iframe>s
  704. root: root.ownerDocument
  705. });
  706. } catch (_e) {
  707. io = new IntersectionObserver(handleObserve, options);
  708. }
  709. io.observe(element);
  710. }
  711. refresh(true);
  712. return cleanup;
  713. }
  714. /**
  715. * Automatically updates the position of the floating element when necessary.
  716. * Should only be called when the floating element is mounted on the DOM or
  717. * visible on the screen.
  718. * @returns cleanup function that should be invoked when the floating element is
  719. * removed from the DOM or hidden from the screen.
  720. * @see https://floating-ui.com/docs/autoUpdate
  721. */
  722. function autoUpdate(reference, floating, update, options) {
  723. if (options === void 0) {
  724. options = {};
  725. }
  726. const {
  727. ancestorScroll = true,
  728. ancestorResize = true,
  729. elementResize = typeof ResizeObserver === 'function',
  730. layoutShift = typeof IntersectionObserver === 'function',
  731. animationFrame = false
  732. } = options;
  733. const referenceEl = unwrapElement(reference);
  734. const ancestors = ancestorScroll || ancestorResize ? [...(referenceEl ? getOverflowAncestors(referenceEl) : []), ...getOverflowAncestors(floating)] : [];
  735. ancestors.forEach(ancestor => {
  736. ancestorScroll && ancestor.addEventListener('scroll', update, {
  737. passive: true
  738. });
  739. ancestorResize && ancestor.addEventListener('resize', update);
  740. });
  741. const cleanupIo = referenceEl && layoutShift ? observeMove(referenceEl, update) : null;
  742. let reobserveFrame = -1;
  743. let resizeObserver = null;
  744. if (elementResize) {
  745. resizeObserver = new ResizeObserver(_ref => {
  746. let [firstEntry] = _ref;
  747. if (firstEntry && firstEntry.target === referenceEl && resizeObserver) {
  748. // Prevent update loops when using the `size` middleware.
  749. // https://github.com/floating-ui/floating-ui/issues/1740
  750. resizeObserver.unobserve(floating);
  751. cancelAnimationFrame(reobserveFrame);
  752. reobserveFrame = requestAnimationFrame(() => {
  753. var _resizeObserver;
  754. (_resizeObserver = resizeObserver) == null || _resizeObserver.observe(floating);
  755. });
  756. }
  757. update();
  758. });
  759. if (referenceEl && !animationFrame) {
  760. resizeObserver.observe(referenceEl);
  761. }
  762. resizeObserver.observe(floating);
  763. }
  764. let frameId;
  765. let prevRefRect = animationFrame ? getBoundingClientRect(reference) : null;
  766. if (animationFrame) {
  767. frameLoop();
  768. }
  769. function frameLoop() {
  770. const nextRefRect = getBoundingClientRect(reference);
  771. if (prevRefRect && !rectsAreEqual(prevRefRect, nextRefRect)) {
  772. update();
  773. }
  774. prevRefRect = nextRefRect;
  775. frameId = requestAnimationFrame(frameLoop);
  776. }
  777. update();
  778. return () => {
  779. var _resizeObserver2;
  780. ancestors.forEach(ancestor => {
  781. ancestorScroll && ancestor.removeEventListener('scroll', update);
  782. ancestorResize && ancestor.removeEventListener('resize', update);
  783. });
  784. cleanupIo == null || cleanupIo();
  785. (_resizeObserver2 = resizeObserver) == null || _resizeObserver2.disconnect();
  786. resizeObserver = null;
  787. if (animationFrame) {
  788. cancelAnimationFrame(frameId);
  789. }
  790. };
  791. }
  792. /**
  793. * Resolves with an object of overflow side offsets that determine how much the
  794. * element is overflowing a given clipping boundary on each side.
  795. * - positive = overflowing the boundary by that number of pixels
  796. * - negative = how many pixels left before it will overflow
  797. * - 0 = lies flush with the boundary
  798. * @see https://floating-ui.com/docs/detectOverflow
  799. */
  800. const detectOverflow = core.detectOverflow;
  801. /**
  802. * Modifies the placement by translating the floating element along the
  803. * specified axes.
  804. * A number (shorthand for `mainAxis` or distance), or an axes configuration
  805. * object may be passed.
  806. * @see https://floating-ui.com/docs/offset
  807. */
  808. const offset = core.offset;
  809. /**
  810. * Optimizes the visibility of the floating element by choosing the placement
  811. * that has the most space available automatically, without needing to specify a
  812. * preferred placement. Alternative to `flip`.
  813. * @see https://floating-ui.com/docs/autoPlacement
  814. */
  815. const autoPlacement = core.autoPlacement;
  816. /**
  817. * Optimizes the visibility of the floating element by shifting it in order to
  818. * keep it in view when it will overflow the clipping boundary.
  819. * @see https://floating-ui.com/docs/shift
  820. */
  821. const shift = core.shift;
  822. /**
  823. * Optimizes the visibility of the floating element by flipping the `placement`
  824. * in order to keep it in view when the preferred placement(s) will overflow the
  825. * clipping boundary. Alternative to `autoPlacement`.
  826. * @see https://floating-ui.com/docs/flip
  827. */
  828. const flip = core.flip;
  829. /**
  830. * Provides data that allows you to change the size of the floating element —
  831. * for instance, prevent it from overflowing the clipping boundary or match the
  832. * width of the reference element.
  833. * @see https://floating-ui.com/docs/size
  834. */
  835. const size = core.size;
  836. /**
  837. * Provides data to hide the floating element in applicable situations, such as
  838. * when it is not in the same clipping context as the reference element.
  839. * @see https://floating-ui.com/docs/hide
  840. */
  841. const hide = core.hide;
  842. /**
  843. * Provides data to position an inner element of the floating element so that it
  844. * appears centered to the reference element.
  845. * @see https://floating-ui.com/docs/arrow
  846. */
  847. const arrow = core.arrow;
  848. /**
  849. * Provides improved positioning for inline reference elements that can span
  850. * over multiple lines, such as hyperlinks or range selections.
  851. * @see https://floating-ui.com/docs/inline
  852. */
  853. const inline = core.inline;
  854. /**
  855. * Built-in `limiter` that will stop `shift()` at a certain point.
  856. */
  857. const limitShift = core.limitShift;
  858. /**
  859. * Computes the `x` and `y` coordinates that will place the floating element
  860. * next to a given reference element.
  861. */
  862. const computePosition = (reference, floating, options) => {
  863. // This caches the expensive `getClippingElementAncestors` function so that
  864. // multiple lifecycle resets re-use the same result. It only lives for a
  865. // single call. If other functions become expensive, we can add them as well.
  866. const cache = new Map();
  867. const mergedOptions = {
  868. platform,
  869. ...options
  870. };
  871. const platformWithCache = {
  872. ...mergedOptions.platform,
  873. _c: cache
  874. };
  875. return core.computePosition(reference, floating, {
  876. ...mergedOptions,
  877. platform: platformWithCache
  878. });
  879. };
  880. exports.arrow = arrow;
  881. exports.autoPlacement = autoPlacement;
  882. exports.autoUpdate = autoUpdate;
  883. exports.computePosition = computePosition;
  884. exports.detectOverflow = detectOverflow;
  885. exports.flip = flip;
  886. exports.getOverflowAncestors = getOverflowAncestors;
  887. exports.hide = hide;
  888. exports.inline = inline;
  889. exports.limitShift = limitShift;
  890. exports.offset = offset;
  891. exports.platform = platform;
  892. exports.shift = shift;
  893. exports.size = size;
  894. }));