floating-ui.dom.umd.js 34 KB

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