floating-ui.dom.esm.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. import { rectToClientRect, detectOverflow as detectOverflow$1, offset as offset$1, autoPlacement as autoPlacement$1, shift as shift$1, flip as flip$1, size as size$1, hide as hide$1, arrow as arrow$1, inline as inline$1, limitShift as limitShift$1, computePosition as computePosition$1 } from '@floating-ui/core';
  2. import { round, createCoords, max, min, floor } from '@floating-ui/utils';
  3. import { getComputedStyle, isHTMLElement, isElement, getWindow, isWebKit, getFrameElement, getNodeScroll, getDocumentElement, isTopLayer, getNodeName, isOverflowElement, getOverflowAncestors, getParentNode, isLastTraversableNode, isContainingBlock, isTableElement, getContainingBlock } from '@floating-ui/utils/dom';
  4. export { getOverflowAncestors } from '@floating-ui/utils/dom';
  5. function getCssDimensions(element) {
  6. const css = getComputedStyle(element);
  7. // In testing environments, the `width` and `height` properties are empty
  8. // strings for SVG elements, returning NaN. Fallback to `0` in this case.
  9. let width = parseFloat(css.width) || 0;
  10. let height = parseFloat(css.height) || 0;
  11. const hasOffset = isHTMLElement(element);
  12. const offsetWidth = hasOffset ? element.offsetWidth : width;
  13. const offsetHeight = hasOffset ? element.offsetHeight : height;
  14. const shouldFallback = round(width) !== offsetWidth || round(height) !== offsetHeight;
  15. if (shouldFallback) {
  16. width = offsetWidth;
  17. height = offsetHeight;
  18. }
  19. return {
  20. width,
  21. height,
  22. $: shouldFallback
  23. };
  24. }
  25. function unwrapElement(element) {
  26. return !isElement(element) ? element.contextElement : element;
  27. }
  28. function getScale(element) {
  29. const domElement = unwrapElement(element);
  30. if (!isHTMLElement(domElement)) {
  31. return createCoords(1);
  32. }
  33. const rect = domElement.getBoundingClientRect();
  34. const {
  35. width,
  36. height,
  37. $
  38. } = getCssDimensions(domElement);
  39. let x = ($ ? round(rect.width) : rect.width) / width;
  40. let y = ($ ? round(rect.height) : rect.height) / height;
  41. // 0, NaN, or Infinity should always fallback to 1.
  42. if (!x || !Number.isFinite(x)) {
  43. x = 1;
  44. }
  45. if (!y || !Number.isFinite(y)) {
  46. y = 1;
  47. }
  48. return {
  49. x,
  50. y
  51. };
  52. }
  53. const noOffsets = /*#__PURE__*/createCoords(0);
  54. function getVisualOffsets(element) {
  55. const win = getWindow(element);
  56. if (!isWebKit() || !win.visualViewport) {
  57. return noOffsets;
  58. }
  59. return {
  60. x: win.visualViewport.offsetLeft,
  61. y: win.visualViewport.offsetTop
  62. };
  63. }
  64. function shouldAddVisualOffsets(element, isFixed, floatingOffsetParent) {
  65. if (isFixed === void 0) {
  66. isFixed = false;
  67. }
  68. if (!floatingOffsetParent || isFixed && floatingOffsetParent !== getWindow(element)) {
  69. return false;
  70. }
  71. return isFixed;
  72. }
  73. function getBoundingClientRect(element, includeScale, isFixedStrategy, offsetParent) {
  74. if (includeScale === void 0) {
  75. includeScale = false;
  76. }
  77. if (isFixedStrategy === void 0) {
  78. isFixedStrategy = false;
  79. }
  80. const clientRect = element.getBoundingClientRect();
  81. const domElement = unwrapElement(element);
  82. let scale = createCoords(1);
  83. if (includeScale) {
  84. if (offsetParent) {
  85. if (isElement(offsetParent)) {
  86. scale = getScale(offsetParent);
  87. }
  88. } else {
  89. scale = getScale(element);
  90. }
  91. }
  92. const visualOffsets = shouldAddVisualOffsets(domElement, isFixedStrategy, offsetParent) ? getVisualOffsets(domElement) : createCoords(0);
  93. let x = (clientRect.left + visualOffsets.x) / scale.x;
  94. let y = (clientRect.top + visualOffsets.y) / scale.y;
  95. let width = clientRect.width / scale.x;
  96. let height = clientRect.height / scale.y;
  97. if (domElement) {
  98. const win = getWindow(domElement);
  99. const offsetWin = offsetParent && isElement(offsetParent) ? getWindow(offsetParent) : offsetParent;
  100. let currentWin = win;
  101. let currentIFrame = getFrameElement(currentWin);
  102. while (currentIFrame && offsetParent && offsetWin !== currentWin) {
  103. const iframeScale = getScale(currentIFrame);
  104. const iframeRect = currentIFrame.getBoundingClientRect();
  105. const css = getComputedStyle(currentIFrame);
  106. const left = iframeRect.left + (currentIFrame.clientLeft + parseFloat(css.paddingLeft)) * iframeScale.x;
  107. const top = iframeRect.top + (currentIFrame.clientTop + parseFloat(css.paddingTop)) * iframeScale.y;
  108. x *= iframeScale.x;
  109. y *= iframeScale.y;
  110. width *= iframeScale.x;
  111. height *= iframeScale.y;
  112. x += left;
  113. y += top;
  114. currentWin = getWindow(currentIFrame);
  115. currentIFrame = getFrameElement(currentWin);
  116. }
  117. }
  118. return rectToClientRect({
  119. width,
  120. height,
  121. x,
  122. y
  123. });
  124. }
  125. // If <html> has a CSS width greater than the viewport, then this will be
  126. // incorrect for RTL.
  127. function getWindowScrollBarX(element, rect) {
  128. const leftScroll = getNodeScroll(element).scrollLeft;
  129. if (!rect) {
  130. return getBoundingClientRect(getDocumentElement(element)).left + leftScroll;
  131. }
  132. return rect.left + leftScroll;
  133. }
  134. function getHTMLOffset(documentElement, scroll, ignoreScrollbarX) {
  135. if (ignoreScrollbarX === void 0) {
  136. ignoreScrollbarX = false;
  137. }
  138. const htmlRect = documentElement.getBoundingClientRect();
  139. const x = htmlRect.left + scroll.scrollLeft - (ignoreScrollbarX ? 0 :
  140. // RTL <body> scrollbar.
  141. getWindowScrollBarX(documentElement, htmlRect));
  142. const y = htmlRect.top + scroll.scrollTop;
  143. return {
  144. x,
  145. y
  146. };
  147. }
  148. function convertOffsetParentRelativeRectToViewportRelativeRect(_ref) {
  149. let {
  150. elements,
  151. rect,
  152. offsetParent,
  153. strategy
  154. } = _ref;
  155. const isFixed = strategy === 'fixed';
  156. const documentElement = getDocumentElement(offsetParent);
  157. const topLayer = elements ? isTopLayer(elements.floating) : false;
  158. if (offsetParent === documentElement || topLayer && isFixed) {
  159. return rect;
  160. }
  161. let scroll = {
  162. scrollLeft: 0,
  163. scrollTop: 0
  164. };
  165. let scale = createCoords(1);
  166. const offsets = createCoords(0);
  167. const isOffsetParentAnElement = isHTMLElement(offsetParent);
  168. if (isOffsetParentAnElement || !isOffsetParentAnElement && !isFixed) {
  169. if (getNodeName(offsetParent) !== 'body' || isOverflowElement(documentElement)) {
  170. scroll = getNodeScroll(offsetParent);
  171. }
  172. if (isHTMLElement(offsetParent)) {
  173. const offsetRect = getBoundingClientRect(offsetParent);
  174. scale = getScale(offsetParent);
  175. offsets.x = offsetRect.x + offsetParent.clientLeft;
  176. offsets.y = offsetRect.y + offsetParent.clientTop;
  177. }
  178. }
  179. const htmlOffset = documentElement && !isOffsetParentAnElement && !isFixed ? getHTMLOffset(documentElement, scroll, true) : createCoords(0);
  180. return {
  181. width: rect.width * scale.x,
  182. height: rect.height * scale.y,
  183. x: rect.x * scale.x - scroll.scrollLeft * scale.x + offsets.x + htmlOffset.x,
  184. y: rect.y * scale.y - scroll.scrollTop * scale.y + offsets.y + htmlOffset.y
  185. };
  186. }
  187. function getClientRects(element) {
  188. return Array.from(element.getClientRects());
  189. }
  190. // Gets the entire size of the scrollable document area, even extending outside
  191. // of the `<html>` and `<body>` rect bounds if horizontally scrollable.
  192. function getDocumentRect(element) {
  193. const html = getDocumentElement(element);
  194. const scroll = getNodeScroll(element);
  195. const body = element.ownerDocument.body;
  196. const width = max(html.scrollWidth, html.clientWidth, body.scrollWidth, body.clientWidth);
  197. const height = max(html.scrollHeight, html.clientHeight, body.scrollHeight, body.clientHeight);
  198. let x = -scroll.scrollLeft + getWindowScrollBarX(element);
  199. const y = -scroll.scrollTop;
  200. if (getComputedStyle(body).direction === 'rtl') {
  201. x += max(html.clientWidth, body.clientWidth) - width;
  202. }
  203. return {
  204. width,
  205. height,
  206. x,
  207. y
  208. };
  209. }
  210. function getViewportRect(element, strategy) {
  211. const win = getWindow(element);
  212. const html = getDocumentElement(element);
  213. const visualViewport = win.visualViewport;
  214. let width = html.clientWidth;
  215. let height = html.clientHeight;
  216. let x = 0;
  217. let y = 0;
  218. if (visualViewport) {
  219. width = visualViewport.width;
  220. height = visualViewport.height;
  221. const visualViewportBased = isWebKit();
  222. if (!visualViewportBased || visualViewportBased && strategy === 'fixed') {
  223. x = visualViewport.offsetLeft;
  224. y = visualViewport.offsetTop;
  225. }
  226. }
  227. return {
  228. width,
  229. height,
  230. x,
  231. y
  232. };
  233. }
  234. // Returns the inner client rect, subtracting scrollbars if present.
  235. function getInnerBoundingClientRect(element, strategy) {
  236. const clientRect = getBoundingClientRect(element, true, strategy === 'fixed');
  237. const top = clientRect.top + element.clientTop;
  238. const left = clientRect.left + element.clientLeft;
  239. const scale = isHTMLElement(element) ? getScale(element) : createCoords(1);
  240. const width = element.clientWidth * scale.x;
  241. const height = element.clientHeight * scale.y;
  242. const x = left * scale.x;
  243. const y = top * scale.y;
  244. return {
  245. width,
  246. height,
  247. x,
  248. y
  249. };
  250. }
  251. function getClientRectFromClippingAncestor(element, clippingAncestor, strategy) {
  252. let rect;
  253. if (clippingAncestor === 'viewport') {
  254. rect = getViewportRect(element, strategy);
  255. } else if (clippingAncestor === 'document') {
  256. rect = getDocumentRect(getDocumentElement(element));
  257. } else if (isElement(clippingAncestor)) {
  258. rect = getInnerBoundingClientRect(clippingAncestor, strategy);
  259. } else {
  260. const visualOffsets = getVisualOffsets(element);
  261. rect = {
  262. x: clippingAncestor.x - visualOffsets.x,
  263. y: clippingAncestor.y - visualOffsets.y,
  264. width: clippingAncestor.width,
  265. height: clippingAncestor.height
  266. };
  267. }
  268. return rectToClientRect(rect);
  269. }
  270. function hasFixedPositionAncestor(element, stopNode) {
  271. const parentNode = getParentNode(element);
  272. if (parentNode === stopNode || !isElement(parentNode) || isLastTraversableNode(parentNode)) {
  273. return false;
  274. }
  275. return getComputedStyle(parentNode).position === 'fixed' || hasFixedPositionAncestor(parentNode, stopNode);
  276. }
  277. // A "clipping ancestor" is an `overflow` element with the characteristic of
  278. // clipping (or hiding) child elements. This returns all clipping ancestors
  279. // of the given element up the tree.
  280. function getClippingElementAncestors(element, cache) {
  281. const cachedResult = cache.get(element);
  282. if (cachedResult) {
  283. return cachedResult;
  284. }
  285. let result = getOverflowAncestors(element, [], false).filter(el => isElement(el) && getNodeName(el) !== 'body');
  286. let currentContainingBlockComputedStyle = null;
  287. const elementIsFixed = getComputedStyle(element).position === 'fixed';
  288. let currentNode = elementIsFixed ? getParentNode(element) : element;
  289. // https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block#identifying_the_containing_block
  290. while (isElement(currentNode) && !isLastTraversableNode(currentNode)) {
  291. const computedStyle = getComputedStyle(currentNode);
  292. const currentNodeIsContaining = isContainingBlock(currentNode);
  293. if (!currentNodeIsContaining && computedStyle.position === 'fixed') {
  294. currentContainingBlockComputedStyle = null;
  295. }
  296. const shouldDropCurrentNode = elementIsFixed ? !currentNodeIsContaining && !currentContainingBlockComputedStyle : !currentNodeIsContaining && computedStyle.position === 'static' && !!currentContainingBlockComputedStyle && ['absolute', 'fixed'].includes(currentContainingBlockComputedStyle.position) || isOverflowElement(currentNode) && !currentNodeIsContaining && hasFixedPositionAncestor(element, currentNode);
  297. if (shouldDropCurrentNode) {
  298. // Drop non-containing blocks.
  299. result = result.filter(ancestor => ancestor !== currentNode);
  300. } else {
  301. // Record last containing block for next iteration.
  302. currentContainingBlockComputedStyle = computedStyle;
  303. }
  304. currentNode = getParentNode(currentNode);
  305. }
  306. cache.set(element, result);
  307. return result;
  308. }
  309. // Gets the maximum area that the element is visible in due to any number of
  310. // clipping ancestors.
  311. function getClippingRect(_ref) {
  312. let {
  313. element,
  314. boundary,
  315. rootBoundary,
  316. strategy
  317. } = _ref;
  318. const elementClippingAncestors = boundary === 'clippingAncestors' ? isTopLayer(element) ? [] : getClippingElementAncestors(element, this._c) : [].concat(boundary);
  319. const clippingAncestors = [...elementClippingAncestors, rootBoundary];
  320. const firstClippingAncestor = clippingAncestors[0];
  321. const clippingRect = clippingAncestors.reduce((accRect, clippingAncestor) => {
  322. const rect = getClientRectFromClippingAncestor(element, clippingAncestor, strategy);
  323. accRect.top = max(rect.top, accRect.top);
  324. accRect.right = min(rect.right, accRect.right);
  325. accRect.bottom = min(rect.bottom, accRect.bottom);
  326. accRect.left = max(rect.left, accRect.left);
  327. return accRect;
  328. }, getClientRectFromClippingAncestor(element, firstClippingAncestor, strategy));
  329. return {
  330. width: clippingRect.right - clippingRect.left,
  331. height: clippingRect.bottom - clippingRect.top,
  332. x: clippingRect.left,
  333. y: clippingRect.top
  334. };
  335. }
  336. function getDimensions(element) {
  337. const {
  338. width,
  339. height
  340. } = getCssDimensions(element);
  341. return {
  342. width,
  343. height
  344. };
  345. }
  346. function getRectRelativeToOffsetParent(element, offsetParent, strategy) {
  347. const isOffsetParentAnElement = isHTMLElement(offsetParent);
  348. const documentElement = getDocumentElement(offsetParent);
  349. const isFixed = strategy === 'fixed';
  350. const rect = getBoundingClientRect(element, true, isFixed, offsetParent);
  351. let scroll = {
  352. scrollLeft: 0,
  353. scrollTop: 0
  354. };
  355. const offsets = createCoords(0);
  356. if (isOffsetParentAnElement || !isOffsetParentAnElement && !isFixed) {
  357. if (getNodeName(offsetParent) !== 'body' || isOverflowElement(documentElement)) {
  358. scroll = getNodeScroll(offsetParent);
  359. }
  360. if (isOffsetParentAnElement) {
  361. const offsetRect = getBoundingClientRect(offsetParent, true, isFixed, offsetParent);
  362. offsets.x = offsetRect.x + offsetParent.clientLeft;
  363. offsets.y = offsetRect.y + offsetParent.clientTop;
  364. } else if (documentElement) {
  365. // If the <body> scrollbar appears on the left (e.g. RTL systems). Use
  366. // Firefox with layout.scrollbar.side = 3 in about:config to test this.
  367. offsets.x = getWindowScrollBarX(documentElement);
  368. }
  369. }
  370. const htmlOffset = documentElement && !isOffsetParentAnElement && !isFixed ? getHTMLOffset(documentElement, scroll) : createCoords(0);
  371. const x = rect.left + scroll.scrollLeft - offsets.x - htmlOffset.x;
  372. const y = rect.top + scroll.scrollTop - offsets.y - htmlOffset.y;
  373. return {
  374. x,
  375. y,
  376. width: rect.width,
  377. height: rect.height
  378. };
  379. }
  380. function isStaticPositioned(element) {
  381. return getComputedStyle(element).position === 'static';
  382. }
  383. function getTrueOffsetParent(element, polyfill) {
  384. if (!isHTMLElement(element) || getComputedStyle(element).position === 'fixed') {
  385. return null;
  386. }
  387. if (polyfill) {
  388. return polyfill(element);
  389. }
  390. let rawOffsetParent = element.offsetParent;
  391. // Firefox returns the <html> element as the offsetParent if it's non-static,
  392. // while Chrome and Safari return the <body> element. The <body> element must
  393. // be used to perform the correct calculations even if the <html> element is
  394. // non-static.
  395. if (getDocumentElement(element) === rawOffsetParent) {
  396. rawOffsetParent = rawOffsetParent.ownerDocument.body;
  397. }
  398. return rawOffsetParent;
  399. }
  400. // Gets the closest ancestor positioned element. Handles some edge cases,
  401. // such as table ancestors and cross browser bugs.
  402. function getOffsetParent(element, polyfill) {
  403. const win = getWindow(element);
  404. if (isTopLayer(element)) {
  405. return win;
  406. }
  407. if (!isHTMLElement(element)) {
  408. let svgOffsetParent = getParentNode(element);
  409. while (svgOffsetParent && !isLastTraversableNode(svgOffsetParent)) {
  410. if (isElement(svgOffsetParent) && !isStaticPositioned(svgOffsetParent)) {
  411. return svgOffsetParent;
  412. }
  413. svgOffsetParent = getParentNode(svgOffsetParent);
  414. }
  415. return win;
  416. }
  417. let offsetParent = getTrueOffsetParent(element, polyfill);
  418. while (offsetParent && isTableElement(offsetParent) && isStaticPositioned(offsetParent)) {
  419. offsetParent = getTrueOffsetParent(offsetParent, polyfill);
  420. }
  421. if (offsetParent && isLastTraversableNode(offsetParent) && isStaticPositioned(offsetParent) && !isContainingBlock(offsetParent)) {
  422. return win;
  423. }
  424. return offsetParent || getContainingBlock(element) || win;
  425. }
  426. const getElementRects = async function (data) {
  427. const getOffsetParentFn = this.getOffsetParent || getOffsetParent;
  428. const getDimensionsFn = this.getDimensions;
  429. const floatingDimensions = await getDimensionsFn(data.floating);
  430. return {
  431. reference: getRectRelativeToOffsetParent(data.reference, await getOffsetParentFn(data.floating), data.strategy),
  432. floating: {
  433. x: 0,
  434. y: 0,
  435. width: floatingDimensions.width,
  436. height: floatingDimensions.height
  437. }
  438. };
  439. };
  440. function isRTL(element) {
  441. return getComputedStyle(element).direction === 'rtl';
  442. }
  443. const platform = {
  444. convertOffsetParentRelativeRectToViewportRelativeRect,
  445. getDocumentElement,
  446. getClippingRect,
  447. getOffsetParent,
  448. getElementRects,
  449. getClientRects,
  450. getDimensions,
  451. getScale,
  452. isElement,
  453. isRTL
  454. };
  455. // https://samthor.au/2021/observing-dom/
  456. function observeMove(element, onMove) {
  457. let io = null;
  458. let timeoutId;
  459. const root = getDocumentElement(element);
  460. function cleanup() {
  461. var _io;
  462. clearTimeout(timeoutId);
  463. (_io = io) == null || _io.disconnect();
  464. io = null;
  465. }
  466. function refresh(skip, threshold) {
  467. if (skip === void 0) {
  468. skip = false;
  469. }
  470. if (threshold === void 0) {
  471. threshold = 1;
  472. }
  473. cleanup();
  474. const {
  475. left,
  476. top,
  477. width,
  478. height
  479. } = element.getBoundingClientRect();
  480. if (!skip) {
  481. onMove();
  482. }
  483. if (!width || !height) {
  484. return;
  485. }
  486. const insetTop = floor(top);
  487. const insetRight = floor(root.clientWidth - (left + width));
  488. const insetBottom = floor(root.clientHeight - (top + height));
  489. const insetLeft = floor(left);
  490. const rootMargin = -insetTop + "px " + -insetRight + "px " + -insetBottom + "px " + -insetLeft + "px";
  491. const options = {
  492. rootMargin,
  493. threshold: max(0, min(1, threshold)) || 1
  494. };
  495. let isFirstUpdate = true;
  496. function handleObserve(entries) {
  497. const ratio = entries[0].intersectionRatio;
  498. if (ratio !== threshold) {
  499. if (!isFirstUpdate) {
  500. return refresh();
  501. }
  502. if (!ratio) {
  503. // If the reference is clipped, the ratio is 0. Throttle the refresh
  504. // to prevent an infinite loop of updates.
  505. timeoutId = setTimeout(() => {
  506. refresh(false, 1e-7);
  507. }, 1000);
  508. } else {
  509. refresh(false, ratio);
  510. }
  511. }
  512. isFirstUpdate = false;
  513. }
  514. // Older browsers don't support a `document` as the root and will throw an
  515. // error.
  516. try {
  517. io = new IntersectionObserver(handleObserve, {
  518. ...options,
  519. // Handle <iframe>s
  520. root: root.ownerDocument
  521. });
  522. } catch (e) {
  523. io = new IntersectionObserver(handleObserve, options);
  524. }
  525. io.observe(element);
  526. }
  527. refresh(true);
  528. return cleanup;
  529. }
  530. /**
  531. * Automatically updates the position of the floating element when necessary.
  532. * Should only be called when the floating element is mounted on the DOM or
  533. * visible on the screen.
  534. * @returns cleanup function that should be invoked when the floating element is
  535. * removed from the DOM or hidden from the screen.
  536. * @see https://floating-ui.com/docs/autoUpdate
  537. */
  538. function autoUpdate(reference, floating, update, options) {
  539. if (options === void 0) {
  540. options = {};
  541. }
  542. const {
  543. ancestorScroll = true,
  544. ancestorResize = true,
  545. elementResize = typeof ResizeObserver === 'function',
  546. layoutShift = typeof IntersectionObserver === 'function',
  547. animationFrame = false
  548. } = options;
  549. const referenceEl = unwrapElement(reference);
  550. const ancestors = ancestorScroll || ancestorResize ? [...(referenceEl ? getOverflowAncestors(referenceEl) : []), ...getOverflowAncestors(floating)] : [];
  551. ancestors.forEach(ancestor => {
  552. ancestorScroll && ancestor.addEventListener('scroll', update, {
  553. passive: true
  554. });
  555. ancestorResize && ancestor.addEventListener('resize', update);
  556. });
  557. const cleanupIo = referenceEl && layoutShift ? observeMove(referenceEl, update) : null;
  558. let reobserveFrame = -1;
  559. let resizeObserver = null;
  560. if (elementResize) {
  561. resizeObserver = new ResizeObserver(_ref => {
  562. let [firstEntry] = _ref;
  563. if (firstEntry && firstEntry.target === referenceEl && resizeObserver) {
  564. // Prevent update loops when using the `size` middleware.
  565. // https://github.com/floating-ui/floating-ui/issues/1740
  566. resizeObserver.unobserve(floating);
  567. cancelAnimationFrame(reobserveFrame);
  568. reobserveFrame = requestAnimationFrame(() => {
  569. var _resizeObserver;
  570. (_resizeObserver = resizeObserver) == null || _resizeObserver.observe(floating);
  571. });
  572. }
  573. update();
  574. });
  575. if (referenceEl && !animationFrame) {
  576. resizeObserver.observe(referenceEl);
  577. }
  578. resizeObserver.observe(floating);
  579. }
  580. let frameId;
  581. let prevRefRect = animationFrame ? getBoundingClientRect(reference) : null;
  582. if (animationFrame) {
  583. frameLoop();
  584. }
  585. function frameLoop() {
  586. const nextRefRect = getBoundingClientRect(reference);
  587. if (prevRefRect && (nextRefRect.x !== prevRefRect.x || nextRefRect.y !== prevRefRect.y || nextRefRect.width !== prevRefRect.width || nextRefRect.height !== prevRefRect.height)) {
  588. update();
  589. }
  590. prevRefRect = nextRefRect;
  591. frameId = requestAnimationFrame(frameLoop);
  592. }
  593. update();
  594. return () => {
  595. var _resizeObserver2;
  596. ancestors.forEach(ancestor => {
  597. ancestorScroll && ancestor.removeEventListener('scroll', update);
  598. ancestorResize && ancestor.removeEventListener('resize', update);
  599. });
  600. cleanupIo == null || cleanupIo();
  601. (_resizeObserver2 = resizeObserver) == null || _resizeObserver2.disconnect();
  602. resizeObserver = null;
  603. if (animationFrame) {
  604. cancelAnimationFrame(frameId);
  605. }
  606. };
  607. }
  608. /**
  609. * Resolves with an object of overflow side offsets that determine how much the
  610. * element is overflowing a given clipping boundary on each side.
  611. * - positive = overflowing the boundary by that number of pixels
  612. * - negative = how many pixels left before it will overflow
  613. * - 0 = lies flush with the boundary
  614. * @see https://floating-ui.com/docs/detectOverflow
  615. */
  616. const detectOverflow = detectOverflow$1;
  617. /**
  618. * Modifies the placement by translating the floating element along the
  619. * specified axes.
  620. * A number (shorthand for `mainAxis` or distance), or an axes configuration
  621. * object may be passed.
  622. * @see https://floating-ui.com/docs/offset
  623. */
  624. const offset = offset$1;
  625. /**
  626. * Optimizes the visibility of the floating element by choosing the placement
  627. * that has the most space available automatically, without needing to specify a
  628. * preferred placement. Alternative to `flip`.
  629. * @see https://floating-ui.com/docs/autoPlacement
  630. */
  631. const autoPlacement = autoPlacement$1;
  632. /**
  633. * Optimizes the visibility of the floating element by shifting it in order to
  634. * keep it in view when it will overflow the clipping boundary.
  635. * @see https://floating-ui.com/docs/shift
  636. */
  637. const shift = shift$1;
  638. /**
  639. * Optimizes the visibility of the floating element by flipping the `placement`
  640. * in order to keep it in view when the preferred placement(s) will overflow the
  641. * clipping boundary. Alternative to `autoPlacement`.
  642. * @see https://floating-ui.com/docs/flip
  643. */
  644. const flip = flip$1;
  645. /**
  646. * Provides data that allows you to change the size of the floating element —
  647. * for instance, prevent it from overflowing the clipping boundary or match the
  648. * width of the reference element.
  649. * @see https://floating-ui.com/docs/size
  650. */
  651. const size = size$1;
  652. /**
  653. * Provides data to hide the floating element in applicable situations, such as
  654. * when it is not in the same clipping context as the reference element.
  655. * @see https://floating-ui.com/docs/hide
  656. */
  657. const hide = hide$1;
  658. /**
  659. * Provides data to position an inner element of the floating element so that it
  660. * appears centered to the reference element.
  661. * @see https://floating-ui.com/docs/arrow
  662. */
  663. const arrow = arrow$1;
  664. /**
  665. * Provides improved positioning for inline reference elements that can span
  666. * over multiple lines, such as hyperlinks or range selections.
  667. * @see https://floating-ui.com/docs/inline
  668. */
  669. const inline = inline$1;
  670. /**
  671. * Built-in `limiter` that will stop `shift()` at a certain point.
  672. */
  673. const limitShift = limitShift$1;
  674. /**
  675. * Computes the `x` and `y` coordinates that will place the floating element
  676. * next to a given reference element.
  677. */
  678. const computePosition = (reference, floating, options) => {
  679. // This caches the expensive `getClippingElementAncestors` function so that
  680. // multiple lifecycle resets re-use the same result. It only lives for a
  681. // single call. If other functions become expensive, we can add them as well.
  682. const cache = new Map();
  683. const mergedOptions = {
  684. platform,
  685. ...options
  686. };
  687. const platformWithCache = {
  688. ...mergedOptions.platform,
  689. _c: cache
  690. };
  691. return computePosition$1(reference, floating, {
  692. ...mergedOptions,
  693. platform: platformWithCache
  694. });
  695. };
  696. export { arrow, autoPlacement, autoUpdate, computePosition, detectOverflow, flip, hide, inline, limitShift, offset, platform, shift, size };