版博士V2.0程序
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

floating-ui.dom.browser.mjs 22 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. import { rectToClientRect, computePosition as computePosition$1 } from '@floating-ui/core';
  2. export { arrow, autoPlacement, detectOverflow, flip, hide, inline, limitShift, offset, shift, size } from '@floating-ui/core';
  3. function getWindow(node) {
  4. var _node$ownerDocument;
  5. return ((_node$ownerDocument = node.ownerDocument) == null ? void 0 : _node$ownerDocument.defaultView) || window;
  6. }
  7. function getComputedStyle$1(element) {
  8. return getWindow(element).getComputedStyle(element);
  9. }
  10. function isNode(value) {
  11. return value instanceof getWindow(value).Node;
  12. }
  13. function getNodeName(node) {
  14. return isNode(node) ? (node.nodeName || '').toLowerCase() : '';
  15. }
  16. let uaString;
  17. function getUAString() {
  18. if (uaString) {
  19. return uaString;
  20. }
  21. const uaData = navigator.userAgentData;
  22. if (uaData && Array.isArray(uaData.brands)) {
  23. uaString = uaData.brands.map(item => item.brand + "/" + item.version).join(' ');
  24. return uaString;
  25. }
  26. return navigator.userAgent;
  27. }
  28. function isHTMLElement(value) {
  29. return value instanceof getWindow(value).HTMLElement;
  30. }
  31. function isElement(value) {
  32. return value instanceof getWindow(value).Element;
  33. }
  34. function isShadowRoot(node) {
  35. // Browsers without `ShadowRoot` support.
  36. if (typeof ShadowRoot === 'undefined') {
  37. return false;
  38. }
  39. const OwnElement = getWindow(node).ShadowRoot;
  40. return node instanceof OwnElement || node instanceof ShadowRoot;
  41. }
  42. function isOverflowElement(element) {
  43. const {
  44. overflow,
  45. overflowX,
  46. overflowY,
  47. display
  48. } = getComputedStyle$1(element);
  49. return /auto|scroll|overlay|hidden|clip/.test(overflow + overflowY + overflowX) && !['inline', 'contents'].includes(display);
  50. }
  51. function isTableElement(element) {
  52. return ['table', 'td', 'th'].includes(getNodeName(element));
  53. }
  54. function isContainingBlock(element) {
  55. // TODO: Try to use feature detection here instead.
  56. const isFirefox = /firefox/i.test(getUAString());
  57. const css = getComputedStyle$1(element);
  58. const backdropFilter = css.backdropFilter || css.WebkitBackdropFilter;
  59. // This is non-exhaustive but covers the most common CSS properties that
  60. // create a containing block.
  61. // https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block#identifying_the_containing_block
  62. return css.transform !== 'none' || css.perspective !== 'none' || (backdropFilter ? backdropFilter !== 'none' : false) || isFirefox && css.willChange === 'filter' || isFirefox && (css.filter ? css.filter !== 'none' : false) || ['transform', 'perspective'].some(value => css.willChange.includes(value)) || ['paint', 'layout', 'strict', 'content'].some(value => {
  63. // Add type check for old browsers.
  64. const contain = css.contain;
  65. return contain != null ? contain.includes(value) : false;
  66. });
  67. }
  68. /**
  69. * Determines whether or not `.getBoundingClientRect()` is affected by visual
  70. * viewport offsets. In Safari, the `x`/`y` offsets are values relative to the
  71. * visual viewport, while in other engines, they are values relative to the
  72. * layout viewport.
  73. */
  74. function isClientRectVisualViewportBased() {
  75. // TODO: Try to use feature detection here instead. Feature detection for
  76. // this can fail in various ways, making the userAgent check the most
  77. // reliable:
  78. // • Always-visible scrollbar or not
  79. // • Width of <html>
  80. // Is Safari.
  81. return /^((?!chrome|android).)*safari/i.test(getUAString());
  82. }
  83. function isLastTraversableNode(node) {
  84. return ['html', 'body', '#document'].includes(getNodeName(node));
  85. }
  86. const min = Math.min;
  87. const max = Math.max;
  88. const round = Math.round;
  89. function getCssDimensions(element) {
  90. const css = getComputedStyle$1(element);
  91. let width = parseFloat(css.width);
  92. let height = parseFloat(css.height);
  93. const hasOffset = isHTMLElement(element);
  94. const offsetWidth = hasOffset ? element.offsetWidth : width;
  95. const offsetHeight = hasOffset ? element.offsetHeight : height;
  96. const shouldFallback = round(width) !== offsetWidth || round(height) !== offsetHeight;
  97. if (shouldFallback) {
  98. width = offsetWidth;
  99. height = offsetHeight;
  100. }
  101. return {
  102. width,
  103. height,
  104. fallback: shouldFallback
  105. };
  106. }
  107. function unwrapElement(element) {
  108. return !isElement(element) ? element.contextElement : element;
  109. }
  110. const FALLBACK_SCALE = {
  111. x: 1,
  112. y: 1
  113. };
  114. function getScale(element) {
  115. const domElement = unwrapElement(element);
  116. if (!isHTMLElement(domElement)) {
  117. return FALLBACK_SCALE;
  118. }
  119. const rect = domElement.getBoundingClientRect();
  120. const {
  121. width,
  122. height,
  123. fallback
  124. } = getCssDimensions(domElement);
  125. let x = (fallback ? round(rect.width) : rect.width) / width;
  126. let y = (fallback ? round(rect.height) : rect.height) / height;
  127. // 0, NaN, or Infinity should always fallback to 1.
  128. if (!x || !Number.isFinite(x)) {
  129. x = 1;
  130. }
  131. if (!y || !Number.isFinite(y)) {
  132. y = 1;
  133. }
  134. return {
  135. x,
  136. y
  137. };
  138. }
  139. function getBoundingClientRect(element, includeScale, isFixedStrategy, offsetParent) {
  140. var _win$visualViewport, _win$visualViewport2;
  141. if (includeScale === void 0) {
  142. includeScale = false;
  143. }
  144. if (isFixedStrategy === void 0) {
  145. isFixedStrategy = false;
  146. }
  147. const clientRect = element.getBoundingClientRect();
  148. const domElement = unwrapElement(element);
  149. let scale = FALLBACK_SCALE;
  150. if (includeScale) {
  151. if (offsetParent) {
  152. if (isElement(offsetParent)) {
  153. scale = getScale(offsetParent);
  154. }
  155. } else {
  156. scale = getScale(element);
  157. }
  158. }
  159. const win = domElement ? getWindow(domElement) : window;
  160. const addVisualOffsets = isClientRectVisualViewportBased() && isFixedStrategy;
  161. let x = (clientRect.left + (addVisualOffsets ? ((_win$visualViewport = win.visualViewport) == null ? void 0 : _win$visualViewport.offsetLeft) || 0 : 0)) / scale.x;
  162. let y = (clientRect.top + (addVisualOffsets ? ((_win$visualViewport2 = win.visualViewport) == null ? void 0 : _win$visualViewport2.offsetTop) || 0 : 0)) / scale.y;
  163. let width = clientRect.width / scale.x;
  164. let height = clientRect.height / scale.y;
  165. if (domElement) {
  166. const win = getWindow(domElement);
  167. const offsetWin = offsetParent && isElement(offsetParent) ? getWindow(offsetParent) : offsetParent;
  168. let currentIFrame = win.frameElement;
  169. while (currentIFrame && offsetParent && offsetWin !== win) {
  170. const iframeScale = getScale(currentIFrame);
  171. const iframeRect = currentIFrame.getBoundingClientRect();
  172. const css = getComputedStyle(currentIFrame);
  173. iframeRect.x += (currentIFrame.clientLeft + parseFloat(css.paddingLeft)) * iframeScale.x;
  174. iframeRect.y += (currentIFrame.clientTop + parseFloat(css.paddingTop)) * iframeScale.y;
  175. x *= iframeScale.x;
  176. y *= iframeScale.y;
  177. width *= iframeScale.x;
  178. height *= iframeScale.y;
  179. x += iframeRect.x;
  180. y += iframeRect.y;
  181. currentIFrame = getWindow(currentIFrame).frameElement;
  182. }
  183. }
  184. return rectToClientRect({
  185. width,
  186. height,
  187. x,
  188. y
  189. });
  190. }
  191. function getDocumentElement(node) {
  192. return ((isNode(node) ? node.ownerDocument : node.document) || window.document).documentElement;
  193. }
  194. function getNodeScroll(element) {
  195. if (isElement(element)) {
  196. return {
  197. scrollLeft: element.scrollLeft,
  198. scrollTop: element.scrollTop
  199. };
  200. }
  201. return {
  202. scrollLeft: element.pageXOffset,
  203. scrollTop: element.pageYOffset
  204. };
  205. }
  206. function convertOffsetParentRelativeRectToViewportRelativeRect(_ref) {
  207. let {
  208. rect,
  209. offsetParent,
  210. strategy
  211. } = _ref;
  212. const isOffsetParentAnElement = isHTMLElement(offsetParent);
  213. const documentElement = getDocumentElement(offsetParent);
  214. if (offsetParent === documentElement) {
  215. return rect;
  216. }
  217. let scroll = {
  218. scrollLeft: 0,
  219. scrollTop: 0
  220. };
  221. let scale = {
  222. x: 1,
  223. y: 1
  224. };
  225. const offsets = {
  226. x: 0,
  227. y: 0
  228. };
  229. if (isOffsetParentAnElement || !isOffsetParentAnElement && strategy !== 'fixed') {
  230. if (getNodeName(offsetParent) !== 'body' || isOverflowElement(documentElement)) {
  231. scroll = getNodeScroll(offsetParent);
  232. }
  233. if (isHTMLElement(offsetParent)) {
  234. const offsetRect = getBoundingClientRect(offsetParent);
  235. scale = getScale(offsetParent);
  236. offsets.x = offsetRect.x + offsetParent.clientLeft;
  237. offsets.y = offsetRect.y + offsetParent.clientTop;
  238. }
  239. }
  240. return {
  241. width: rect.width * scale.x,
  242. height: rect.height * scale.y,
  243. x: rect.x * scale.x - scroll.scrollLeft * scale.x + offsets.x,
  244. y: rect.y * scale.y - scroll.scrollTop * scale.y + offsets.y
  245. };
  246. }
  247. function getWindowScrollBarX(element) {
  248. // If <html> has a CSS width greater than the viewport, then this will be
  249. // incorrect for RTL.
  250. return getBoundingClientRect(getDocumentElement(element)).left + getNodeScroll(element).scrollLeft;
  251. }
  252. // Gets the entire size of the scrollable document area, even extending outside
  253. // of the `<html>` and `<body>` rect bounds if horizontally scrollable.
  254. function getDocumentRect(element) {
  255. const html = getDocumentElement(element);
  256. const scroll = getNodeScroll(element);
  257. const body = element.ownerDocument.body;
  258. const width = max(html.scrollWidth, html.clientWidth, body.scrollWidth, body.clientWidth);
  259. const height = max(html.scrollHeight, html.clientHeight, body.scrollHeight, body.clientHeight);
  260. let x = -scroll.scrollLeft + getWindowScrollBarX(element);
  261. const y = -scroll.scrollTop;
  262. if (getComputedStyle$1(body).direction === 'rtl') {
  263. x += max(html.clientWidth, body.clientWidth) - width;
  264. }
  265. return {
  266. width,
  267. height,
  268. x,
  269. y
  270. };
  271. }
  272. function getParentNode(node) {
  273. if (getNodeName(node) === 'html') {
  274. return node;
  275. }
  276. const result =
  277. // Step into the shadow DOM of the parent of a slotted node.
  278. node.assignedSlot ||
  279. // DOM Element detected.
  280. node.parentNode ||
  281. // ShadowRoot detected.
  282. isShadowRoot(node) && node.host ||
  283. // Fallback.
  284. getDocumentElement(node);
  285. return isShadowRoot(result) ? result.host : result;
  286. }
  287. function getNearestOverflowAncestor(node) {
  288. const parentNode = getParentNode(node);
  289. if (isLastTraversableNode(parentNode)) {
  290. // `getParentNode` will never return a `Document` due to the fallback
  291. // check, so it's either the <html> or <body> element.
  292. return parentNode.ownerDocument.body;
  293. }
  294. if (isHTMLElement(parentNode) && isOverflowElement(parentNode)) {
  295. return parentNode;
  296. }
  297. return getNearestOverflowAncestor(parentNode);
  298. }
  299. function getOverflowAncestors(node, list) {
  300. var _node$ownerDocument;
  301. if (list === void 0) {
  302. list = [];
  303. }
  304. const scrollableAncestor = getNearestOverflowAncestor(node);
  305. const isBody = scrollableAncestor === ((_node$ownerDocument = node.ownerDocument) == null ? void 0 : _node$ownerDocument.body);
  306. const win = getWindow(scrollableAncestor);
  307. if (isBody) {
  308. return list.concat(win, win.visualViewport || [], isOverflowElement(scrollableAncestor) ? scrollableAncestor : []);
  309. }
  310. return list.concat(scrollableAncestor, getOverflowAncestors(scrollableAncestor));
  311. }
  312. function getViewportRect(element, strategy) {
  313. const win = getWindow(element);
  314. const html = getDocumentElement(element);
  315. const visualViewport = win.visualViewport;
  316. let width = html.clientWidth;
  317. let height = html.clientHeight;
  318. let x = 0;
  319. let y = 0;
  320. if (visualViewport) {
  321. width = visualViewport.width;
  322. height = visualViewport.height;
  323. const visualViewportBased = isClientRectVisualViewportBased();
  324. if (!visualViewportBased || visualViewportBased && strategy === 'fixed') {
  325. x = visualViewport.offsetLeft;
  326. y = visualViewport.offsetTop;
  327. }
  328. }
  329. return {
  330. width,
  331. height,
  332. x,
  333. y
  334. };
  335. }
  336. // Returns the inner client rect, subtracting scrollbars if present.
  337. function getInnerBoundingClientRect(element, strategy) {
  338. const clientRect = getBoundingClientRect(element, true, strategy === 'fixed');
  339. const top = clientRect.top + element.clientTop;
  340. const left = clientRect.left + element.clientLeft;
  341. const scale = isHTMLElement(element) ? getScale(element) : {
  342. x: 1,
  343. y: 1
  344. };
  345. const width = element.clientWidth * scale.x;
  346. const height = element.clientHeight * scale.y;
  347. const x = left * scale.x;
  348. const y = top * scale.y;
  349. return {
  350. width,
  351. height,
  352. x,
  353. y
  354. };
  355. }
  356. function getClientRectFromClippingAncestor(element, clippingAncestor, strategy) {
  357. let rect;
  358. if (clippingAncestor === 'viewport') {
  359. rect = getViewportRect(element, strategy);
  360. } else if (clippingAncestor === 'document') {
  361. rect = getDocumentRect(getDocumentElement(element));
  362. } else if (isElement(clippingAncestor)) {
  363. rect = getInnerBoundingClientRect(clippingAncestor, strategy);
  364. } else {
  365. const mutableRect = {
  366. ...clippingAncestor
  367. };
  368. if (isClientRectVisualViewportBased()) {
  369. var _win$visualViewport, _win$visualViewport2;
  370. const win = getWindow(element);
  371. mutableRect.x -= ((_win$visualViewport = win.visualViewport) == null ? void 0 : _win$visualViewport.offsetLeft) || 0;
  372. mutableRect.y -= ((_win$visualViewport2 = win.visualViewport) == null ? void 0 : _win$visualViewport2.offsetTop) || 0;
  373. }
  374. rect = mutableRect;
  375. }
  376. return rectToClientRect(rect);
  377. }
  378. // A "clipping ancestor" is an `overflow` element with the characteristic of
  379. // clipping (or hiding) child elements. This returns all clipping ancestors
  380. // of the given element up the tree.
  381. function getClippingElementAncestors(element, cache) {
  382. const cachedResult = cache.get(element);
  383. if (cachedResult) {
  384. return cachedResult;
  385. }
  386. let result = getOverflowAncestors(element).filter(el => isElement(el) && getNodeName(el) !== 'body');
  387. let currentContainingBlockComputedStyle = null;
  388. const elementIsFixed = getComputedStyle$1(element).position === 'fixed';
  389. let currentNode = elementIsFixed ? getParentNode(element) : element;
  390. // https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block#identifying_the_containing_block
  391. while (isElement(currentNode) && !isLastTraversableNode(currentNode)) {
  392. const computedStyle = getComputedStyle$1(currentNode);
  393. const containingBlock = isContainingBlock(currentNode);
  394. if (computedStyle.position === 'fixed') {
  395. currentContainingBlockComputedStyle = null;
  396. }
  397. const shouldDropCurrentNode = elementIsFixed ? !containingBlock && !currentContainingBlockComputedStyle : !containingBlock && computedStyle.position === 'static' && !!currentContainingBlockComputedStyle && ['absolute', 'fixed'].includes(currentContainingBlockComputedStyle.position);
  398. if (shouldDropCurrentNode) {
  399. // Drop non-containing blocks.
  400. result = result.filter(ancestor => ancestor !== currentNode);
  401. } else {
  402. // Record last containing block for next iteration.
  403. currentContainingBlockComputedStyle = computedStyle;
  404. }
  405. currentNode = getParentNode(currentNode);
  406. }
  407. cache.set(element, result);
  408. return result;
  409. }
  410. // Gets the maximum area that the element is visible in due to any number of
  411. // clipping ancestors.
  412. function getClippingRect(_ref) {
  413. let {
  414. element,
  415. boundary,
  416. rootBoundary,
  417. strategy
  418. } = _ref;
  419. const elementClippingAncestors = boundary === 'clippingAncestors' ? getClippingElementAncestors(element, this._c) : [].concat(boundary);
  420. const clippingAncestors = [...elementClippingAncestors, rootBoundary];
  421. const firstClippingAncestor = clippingAncestors[0];
  422. const clippingRect = clippingAncestors.reduce((accRect, clippingAncestor) => {
  423. const rect = getClientRectFromClippingAncestor(element, clippingAncestor, strategy);
  424. accRect.top = max(rect.top, accRect.top);
  425. accRect.right = min(rect.right, accRect.right);
  426. accRect.bottom = min(rect.bottom, accRect.bottom);
  427. accRect.left = max(rect.left, accRect.left);
  428. return accRect;
  429. }, getClientRectFromClippingAncestor(element, firstClippingAncestor, strategy));
  430. return {
  431. width: clippingRect.right - clippingRect.left,
  432. height: clippingRect.bottom - clippingRect.top,
  433. x: clippingRect.left,
  434. y: clippingRect.top
  435. };
  436. }
  437. function getDimensions(element) {
  438. return getCssDimensions(element);
  439. }
  440. function getTrueOffsetParent(element, polyfill) {
  441. if (!isHTMLElement(element) || getComputedStyle$1(element).position === 'fixed') {
  442. return null;
  443. }
  444. if (polyfill) {
  445. return polyfill(element);
  446. }
  447. return element.offsetParent;
  448. }
  449. function getContainingBlock(element) {
  450. let currentNode = getParentNode(element);
  451. while (isHTMLElement(currentNode) && !isLastTraversableNode(currentNode)) {
  452. if (isContainingBlock(currentNode)) {
  453. return currentNode;
  454. } else {
  455. currentNode = getParentNode(currentNode);
  456. }
  457. }
  458. return null;
  459. }
  460. // Gets the closest ancestor positioned element. Handles some edge cases,
  461. // such as table ancestors and cross browser bugs.
  462. function getOffsetParent(element, polyfill) {
  463. const window = getWindow(element);
  464. if (!isHTMLElement(element)) {
  465. return window;
  466. }
  467. let offsetParent = getTrueOffsetParent(element, polyfill);
  468. while (offsetParent && isTableElement(offsetParent) && getComputedStyle$1(offsetParent).position === 'static') {
  469. offsetParent = getTrueOffsetParent(offsetParent, polyfill);
  470. }
  471. if (offsetParent && (getNodeName(offsetParent) === 'html' || getNodeName(offsetParent) === 'body' && getComputedStyle$1(offsetParent).position === 'static' && !isContainingBlock(offsetParent))) {
  472. return window;
  473. }
  474. return offsetParent || getContainingBlock(element) || window;
  475. }
  476. function getRectRelativeToOffsetParent(element, offsetParent, strategy) {
  477. const isOffsetParentAnElement = isHTMLElement(offsetParent);
  478. const documentElement = getDocumentElement(offsetParent);
  479. const rect = getBoundingClientRect(element, true, strategy === 'fixed', offsetParent);
  480. let scroll = {
  481. scrollLeft: 0,
  482. scrollTop: 0
  483. };
  484. const offsets = {
  485. x: 0,
  486. y: 0
  487. };
  488. if (isOffsetParentAnElement || !isOffsetParentAnElement && strategy !== 'fixed') {
  489. if (getNodeName(offsetParent) !== 'body' || isOverflowElement(documentElement)) {
  490. scroll = getNodeScroll(offsetParent);
  491. }
  492. if (isHTMLElement(offsetParent)) {
  493. const offsetRect = getBoundingClientRect(offsetParent, true);
  494. offsets.x = offsetRect.x + offsetParent.clientLeft;
  495. offsets.y = offsetRect.y + offsetParent.clientTop;
  496. } else if (documentElement) {
  497. offsets.x = getWindowScrollBarX(documentElement);
  498. }
  499. }
  500. return {
  501. x: rect.left + scroll.scrollLeft - offsets.x,
  502. y: rect.top + scroll.scrollTop - offsets.y,
  503. width: rect.width,
  504. height: rect.height
  505. };
  506. }
  507. const platform = {
  508. getClippingRect,
  509. convertOffsetParentRelativeRectToViewportRelativeRect,
  510. isElement,
  511. getDimensions,
  512. getOffsetParent,
  513. getDocumentElement,
  514. getScale,
  515. async getElementRects(_ref) {
  516. let {
  517. reference,
  518. floating,
  519. strategy
  520. } = _ref;
  521. const getOffsetParentFn = this.getOffsetParent || getOffsetParent;
  522. const getDimensionsFn = this.getDimensions;
  523. return {
  524. reference: getRectRelativeToOffsetParent(reference, await getOffsetParentFn(floating), strategy),
  525. floating: {
  526. x: 0,
  527. y: 0,
  528. ...(await getDimensionsFn(floating))
  529. }
  530. };
  531. },
  532. getClientRects: element => Array.from(element.getClientRects()),
  533. isRTL: element => getComputedStyle$1(element).direction === 'rtl'
  534. };
  535. /**
  536. * Automatically updates the position of the floating element when necessary.
  537. * Should only be called when the floating element is mounted on the DOM or
  538. * visible on the screen.
  539. * @returns cleanup function that should be invoked when the floating element is
  540. * removed from the DOM or hidden from the screen.
  541. * @see https://floating-ui.com/docs/autoUpdate
  542. */
  543. function autoUpdate(reference, floating, update, options) {
  544. if (options === void 0) {
  545. options = {};
  546. }
  547. const {
  548. ancestorScroll: _ancestorScroll = true,
  549. ancestorResize = true,
  550. elementResize = true,
  551. animationFrame = false
  552. } = options;
  553. const ancestorScroll = _ancestorScroll && !animationFrame;
  554. const ancestors = ancestorScroll || ancestorResize ? [...(isElement(reference) ? getOverflowAncestors(reference) : reference.contextElement ? getOverflowAncestors(reference.contextElement) : []), ...getOverflowAncestors(floating)] : [];
  555. ancestors.forEach(ancestor => {
  556. ancestorScroll && ancestor.addEventListener('scroll', update, {
  557. passive: true
  558. });
  559. ancestorResize && ancestor.addEventListener('resize', update);
  560. });
  561. let observer = null;
  562. if (elementResize) {
  563. observer = new ResizeObserver(() => {
  564. update();
  565. });
  566. isElement(reference) && !animationFrame && observer.observe(reference);
  567. if (!isElement(reference) && reference.contextElement && !animationFrame) {
  568. observer.observe(reference.contextElement);
  569. }
  570. observer.observe(floating);
  571. }
  572. let frameId;
  573. let prevRefRect = animationFrame ? getBoundingClientRect(reference) : null;
  574. if (animationFrame) {
  575. frameLoop();
  576. }
  577. function frameLoop() {
  578. const nextRefRect = getBoundingClientRect(reference);
  579. if (prevRefRect && (nextRefRect.x !== prevRefRect.x || nextRefRect.y !== prevRefRect.y || nextRefRect.width !== prevRefRect.width || nextRefRect.height !== prevRefRect.height)) {
  580. update();
  581. }
  582. prevRefRect = nextRefRect;
  583. frameId = requestAnimationFrame(frameLoop);
  584. }
  585. update();
  586. return () => {
  587. var _observer;
  588. ancestors.forEach(ancestor => {
  589. ancestorScroll && ancestor.removeEventListener('scroll', update);
  590. ancestorResize && ancestor.removeEventListener('resize', update);
  591. });
  592. (_observer = observer) == null ? void 0 : _observer.disconnect();
  593. observer = null;
  594. if (animationFrame) {
  595. cancelAnimationFrame(frameId);
  596. }
  597. };
  598. }
  599. /**
  600. * Computes the `x` and `y` coordinates that will place the floating element
  601. * next to a reference element when it is given a certain CSS positioning
  602. * strategy.
  603. */
  604. const computePosition = (reference, floating, options) => {
  605. // This caches the expensive `getClippingElementAncestors` function so that
  606. // multiple lifecycle resets re-use the same result. It only lives for a
  607. // single call. If other functions become expensive, we can add them as well.
  608. const cache = new Map();
  609. const mergedOptions = {
  610. platform,
  611. ...options
  612. };
  613. const platformWithCache = {
  614. ...mergedOptions.platform,
  615. _c: cache
  616. };
  617. return computePosition$1(reference, floating, {
  618. ...mergedOptions,
  619. platform: platformWithCache
  620. });
  621. };
  622. export { autoUpdate, computePosition, getOverflowAncestors, platform };