floating-ui.dom.browser.mjs 32 KB

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