floating-ui.core.umd.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.FloatingUICore = {}));
  5. })(this, (function (exports) { 'use strict';
  6. /**
  7. * Custom positioning reference element.
  8. * @see https://floating-ui.com/docs/virtual-elements
  9. */
  10. const sides = ['top', 'right', 'bottom', 'left'];
  11. const alignments = ['start', 'end'];
  12. const placements = /*#__PURE__*/sides.reduce((acc, side) => acc.concat(side, side + "-" + alignments[0], side + "-" + alignments[1]), []);
  13. const min = Math.min;
  14. const max = Math.max;
  15. const oppositeSideMap = {
  16. left: 'right',
  17. right: 'left',
  18. bottom: 'top',
  19. top: 'bottom'
  20. };
  21. const oppositeAlignmentMap = {
  22. start: 'end',
  23. end: 'start'
  24. };
  25. function clamp(start, value, end) {
  26. return max(start, min(value, end));
  27. }
  28. function evaluate(value, param) {
  29. return typeof value === 'function' ? value(param) : value;
  30. }
  31. function getSide(placement) {
  32. return placement.split('-')[0];
  33. }
  34. function getAlignment(placement) {
  35. return placement.split('-')[1];
  36. }
  37. function getOppositeAxis(axis) {
  38. return axis === 'x' ? 'y' : 'x';
  39. }
  40. function getAxisLength(axis) {
  41. return axis === 'y' ? 'height' : 'width';
  42. }
  43. function getSideAxis(placement) {
  44. return ['top', 'bottom'].includes(getSide(placement)) ? 'y' : 'x';
  45. }
  46. function getAlignmentAxis(placement) {
  47. return getOppositeAxis(getSideAxis(placement));
  48. }
  49. function getAlignmentSides(placement, rects, rtl) {
  50. if (rtl === void 0) {
  51. rtl = false;
  52. }
  53. const alignment = getAlignment(placement);
  54. const alignmentAxis = getAlignmentAxis(placement);
  55. const length = getAxisLength(alignmentAxis);
  56. let mainAlignmentSide = alignmentAxis === 'x' ? alignment === (rtl ? 'end' : 'start') ? 'right' : 'left' : alignment === 'start' ? 'bottom' : 'top';
  57. if (rects.reference[length] > rects.floating[length]) {
  58. mainAlignmentSide = getOppositePlacement(mainAlignmentSide);
  59. }
  60. return [mainAlignmentSide, getOppositePlacement(mainAlignmentSide)];
  61. }
  62. function getExpandedPlacements(placement) {
  63. const oppositePlacement = getOppositePlacement(placement);
  64. return [getOppositeAlignmentPlacement(placement), oppositePlacement, getOppositeAlignmentPlacement(oppositePlacement)];
  65. }
  66. function getOppositeAlignmentPlacement(placement) {
  67. return placement.replace(/start|end/g, alignment => oppositeAlignmentMap[alignment]);
  68. }
  69. function getSideList(side, isStart, rtl) {
  70. const lr = ['left', 'right'];
  71. const rl = ['right', 'left'];
  72. const tb = ['top', 'bottom'];
  73. const bt = ['bottom', 'top'];
  74. switch (side) {
  75. case 'top':
  76. case 'bottom':
  77. if (rtl) return isStart ? rl : lr;
  78. return isStart ? lr : rl;
  79. case 'left':
  80. case 'right':
  81. return isStart ? tb : bt;
  82. default:
  83. return [];
  84. }
  85. }
  86. function getOppositeAxisPlacements(placement, flipAlignment, direction, rtl) {
  87. const alignment = getAlignment(placement);
  88. let list = getSideList(getSide(placement), direction === 'start', rtl);
  89. if (alignment) {
  90. list = list.map(side => side + "-" + alignment);
  91. if (flipAlignment) {
  92. list = list.concat(list.map(getOppositeAlignmentPlacement));
  93. }
  94. }
  95. return list;
  96. }
  97. function getOppositePlacement(placement) {
  98. return placement.replace(/left|right|bottom|top/g, side => oppositeSideMap[side]);
  99. }
  100. function expandPaddingObject(padding) {
  101. return {
  102. top: 0,
  103. right: 0,
  104. bottom: 0,
  105. left: 0,
  106. ...padding
  107. };
  108. }
  109. function getPaddingObject(padding) {
  110. return typeof padding !== 'number' ? expandPaddingObject(padding) : {
  111. top: padding,
  112. right: padding,
  113. bottom: padding,
  114. left: padding
  115. };
  116. }
  117. function rectToClientRect(rect) {
  118. const {
  119. x,
  120. y,
  121. width,
  122. height
  123. } = rect;
  124. return {
  125. width,
  126. height,
  127. top: y,
  128. left: x,
  129. right: x + width,
  130. bottom: y + height,
  131. x,
  132. y
  133. };
  134. }
  135. function computeCoordsFromPlacement(_ref, placement, rtl) {
  136. let {
  137. reference,
  138. floating
  139. } = _ref;
  140. const sideAxis = getSideAxis(placement);
  141. const alignmentAxis = getAlignmentAxis(placement);
  142. const alignLength = getAxisLength(alignmentAxis);
  143. const side = getSide(placement);
  144. const isVertical = sideAxis === 'y';
  145. const commonX = reference.x + reference.width / 2 - floating.width / 2;
  146. const commonY = reference.y + reference.height / 2 - floating.height / 2;
  147. const commonAlign = reference[alignLength] / 2 - floating[alignLength] / 2;
  148. let coords;
  149. switch (side) {
  150. case 'top':
  151. coords = {
  152. x: commonX,
  153. y: reference.y - floating.height
  154. };
  155. break;
  156. case 'bottom':
  157. coords = {
  158. x: commonX,
  159. y: reference.y + reference.height
  160. };
  161. break;
  162. case 'right':
  163. coords = {
  164. x: reference.x + reference.width,
  165. y: commonY
  166. };
  167. break;
  168. case 'left':
  169. coords = {
  170. x: reference.x - floating.width,
  171. y: commonY
  172. };
  173. break;
  174. default:
  175. coords = {
  176. x: reference.x,
  177. y: reference.y
  178. };
  179. }
  180. switch (getAlignment(placement)) {
  181. case 'start':
  182. coords[alignmentAxis] -= commonAlign * (rtl && isVertical ? -1 : 1);
  183. break;
  184. case 'end':
  185. coords[alignmentAxis] += commonAlign * (rtl && isVertical ? -1 : 1);
  186. break;
  187. }
  188. return coords;
  189. }
  190. /**
  191. * Computes the `x` and `y` coordinates that will place the floating element
  192. * next to a given reference element.
  193. *
  194. * This export does not have any `platform` interface logic. You will need to
  195. * write one for the platform you are using Floating UI with.
  196. */
  197. const computePosition = async (reference, floating, config) => {
  198. const {
  199. placement = 'bottom',
  200. strategy = 'absolute',
  201. middleware = [],
  202. platform
  203. } = config;
  204. const validMiddleware = middleware.filter(Boolean);
  205. const rtl = await (platform.isRTL == null ? void 0 : platform.isRTL(floating));
  206. let rects = await platform.getElementRects({
  207. reference,
  208. floating,
  209. strategy
  210. });
  211. let {
  212. x,
  213. y
  214. } = computeCoordsFromPlacement(rects, placement, rtl);
  215. let statefulPlacement = placement;
  216. let middlewareData = {};
  217. let resetCount = 0;
  218. for (let i = 0; i < validMiddleware.length; i++) {
  219. const {
  220. name,
  221. fn
  222. } = validMiddleware[i];
  223. const {
  224. x: nextX,
  225. y: nextY,
  226. data,
  227. reset
  228. } = await fn({
  229. x,
  230. y,
  231. initialPlacement: placement,
  232. placement: statefulPlacement,
  233. strategy,
  234. middlewareData,
  235. rects,
  236. platform,
  237. elements: {
  238. reference,
  239. floating
  240. }
  241. });
  242. x = nextX != null ? nextX : x;
  243. y = nextY != null ? nextY : y;
  244. middlewareData = {
  245. ...middlewareData,
  246. [name]: {
  247. ...middlewareData[name],
  248. ...data
  249. }
  250. };
  251. if (reset && resetCount <= 50) {
  252. resetCount++;
  253. if (typeof reset === 'object') {
  254. if (reset.placement) {
  255. statefulPlacement = reset.placement;
  256. }
  257. if (reset.rects) {
  258. rects = reset.rects === true ? await platform.getElementRects({
  259. reference,
  260. floating,
  261. strategy
  262. }) : reset.rects;
  263. }
  264. ({
  265. x,
  266. y
  267. } = computeCoordsFromPlacement(rects, statefulPlacement, rtl));
  268. }
  269. i = -1;
  270. }
  271. }
  272. return {
  273. x,
  274. y,
  275. placement: statefulPlacement,
  276. strategy,
  277. middlewareData
  278. };
  279. };
  280. /**
  281. * Resolves with an object of overflow side offsets that determine how much the
  282. * element is overflowing a given clipping boundary on each side.
  283. * - positive = overflowing the boundary by that number of pixels
  284. * - negative = how many pixels left before it will overflow
  285. * - 0 = lies flush with the boundary
  286. * @see https://floating-ui.com/docs/detectOverflow
  287. */
  288. async function detectOverflow(state, options) {
  289. var _await$platform$isEle;
  290. if (options === void 0) {
  291. options = {};
  292. }
  293. const {
  294. x,
  295. y,
  296. platform,
  297. rects,
  298. elements,
  299. strategy
  300. } = state;
  301. const {
  302. boundary = 'clippingAncestors',
  303. rootBoundary = 'viewport',
  304. elementContext = 'floating',
  305. altBoundary = false,
  306. padding = 0
  307. } = evaluate(options, state);
  308. const paddingObject = getPaddingObject(padding);
  309. const altContext = elementContext === 'floating' ? 'reference' : 'floating';
  310. const element = elements[altBoundary ? altContext : elementContext];
  311. const clippingClientRect = rectToClientRect(await platform.getClippingRect({
  312. element: ((_await$platform$isEle = await (platform.isElement == null ? void 0 : platform.isElement(element))) != null ? _await$platform$isEle : true) ? element : element.contextElement || (await (platform.getDocumentElement == null ? void 0 : platform.getDocumentElement(elements.floating))),
  313. boundary,
  314. rootBoundary,
  315. strategy
  316. }));
  317. const rect = elementContext === 'floating' ? {
  318. x,
  319. y,
  320. width: rects.floating.width,
  321. height: rects.floating.height
  322. } : rects.reference;
  323. const offsetParent = await (platform.getOffsetParent == null ? void 0 : platform.getOffsetParent(elements.floating));
  324. const offsetScale = (await (platform.isElement == null ? void 0 : platform.isElement(offsetParent))) ? (await (platform.getScale == null ? void 0 : platform.getScale(offsetParent))) || {
  325. x: 1,
  326. y: 1
  327. } : {
  328. x: 1,
  329. y: 1
  330. };
  331. const elementClientRect = rectToClientRect(platform.convertOffsetParentRelativeRectToViewportRelativeRect ? await platform.convertOffsetParentRelativeRectToViewportRelativeRect({
  332. elements,
  333. rect,
  334. offsetParent,
  335. strategy
  336. }) : rect);
  337. return {
  338. top: (clippingClientRect.top - elementClientRect.top + paddingObject.top) / offsetScale.y,
  339. bottom: (elementClientRect.bottom - clippingClientRect.bottom + paddingObject.bottom) / offsetScale.y,
  340. left: (clippingClientRect.left - elementClientRect.left + paddingObject.left) / offsetScale.x,
  341. right: (elementClientRect.right - clippingClientRect.right + paddingObject.right) / offsetScale.x
  342. };
  343. }
  344. /**
  345. * Provides data to position an inner element of the floating element so that it
  346. * appears centered to the reference element.
  347. * @see https://floating-ui.com/docs/arrow
  348. */
  349. const arrow = options => ({
  350. name: 'arrow',
  351. options,
  352. async fn(state) {
  353. const {
  354. x,
  355. y,
  356. placement,
  357. rects,
  358. platform,
  359. elements,
  360. middlewareData
  361. } = state;
  362. // Since `element` is required, we don't Partial<> the type.
  363. const {
  364. element,
  365. padding = 0
  366. } = evaluate(options, state) || {};
  367. if (element == null) {
  368. return {};
  369. }
  370. const paddingObject = getPaddingObject(padding);
  371. const coords = {
  372. x,
  373. y
  374. };
  375. const axis = getAlignmentAxis(placement);
  376. const length = getAxisLength(axis);
  377. const arrowDimensions = await platform.getDimensions(element);
  378. const isYAxis = axis === 'y';
  379. const minProp = isYAxis ? 'top' : 'left';
  380. const maxProp = isYAxis ? 'bottom' : 'right';
  381. const clientProp = isYAxis ? 'clientHeight' : 'clientWidth';
  382. const endDiff = rects.reference[length] + rects.reference[axis] - coords[axis] - rects.floating[length];
  383. const startDiff = coords[axis] - rects.reference[axis];
  384. const arrowOffsetParent = await (platform.getOffsetParent == null ? void 0 : platform.getOffsetParent(element));
  385. let clientSize = arrowOffsetParent ? arrowOffsetParent[clientProp] : 0;
  386. // DOM platform can return `window` as the `offsetParent`.
  387. if (!clientSize || !(await (platform.isElement == null ? void 0 : platform.isElement(arrowOffsetParent)))) {
  388. clientSize = elements.floating[clientProp] || rects.floating[length];
  389. }
  390. const centerToReference = endDiff / 2 - startDiff / 2;
  391. // If the padding is large enough that it causes the arrow to no longer be
  392. // centered, modify the padding so that it is centered.
  393. const largestPossiblePadding = clientSize / 2 - arrowDimensions[length] / 2 - 1;
  394. const minPadding = min(paddingObject[minProp], largestPossiblePadding);
  395. const maxPadding = min(paddingObject[maxProp], largestPossiblePadding);
  396. // Make sure the arrow doesn't overflow the floating element if the center
  397. // point is outside the floating element's bounds.
  398. const min$1 = minPadding;
  399. const max = clientSize - arrowDimensions[length] - maxPadding;
  400. const center = clientSize / 2 - arrowDimensions[length] / 2 + centerToReference;
  401. const offset = clamp(min$1, center, max);
  402. // If the reference is small enough that the arrow's padding causes it to
  403. // to point to nothing for an aligned placement, adjust the offset of the
  404. // floating element itself. To ensure `shift()` continues to take action,
  405. // a single reset is performed when this is true.
  406. const shouldAddOffset = !middlewareData.arrow && getAlignment(placement) != null && center !== offset && rects.reference[length] / 2 - (center < min$1 ? minPadding : maxPadding) - arrowDimensions[length] / 2 < 0;
  407. const alignmentOffset = shouldAddOffset ? center < min$1 ? center - min$1 : center - max : 0;
  408. return {
  409. [axis]: coords[axis] + alignmentOffset,
  410. data: {
  411. [axis]: offset,
  412. centerOffset: center - offset - alignmentOffset,
  413. ...(shouldAddOffset && {
  414. alignmentOffset
  415. })
  416. },
  417. reset: shouldAddOffset
  418. };
  419. }
  420. });
  421. function getPlacementList(alignment, autoAlignment, allowedPlacements) {
  422. const allowedPlacementsSortedByAlignment = alignment ? [...allowedPlacements.filter(placement => getAlignment(placement) === alignment), ...allowedPlacements.filter(placement => getAlignment(placement) !== alignment)] : allowedPlacements.filter(placement => getSide(placement) === placement);
  423. return allowedPlacementsSortedByAlignment.filter(placement => {
  424. if (alignment) {
  425. return getAlignment(placement) === alignment || (autoAlignment ? getOppositeAlignmentPlacement(placement) !== placement : false);
  426. }
  427. return true;
  428. });
  429. }
  430. /**
  431. * Optimizes the visibility of the floating element by choosing the placement
  432. * that has the most space available automatically, without needing to specify a
  433. * preferred placement. Alternative to `flip`.
  434. * @see https://floating-ui.com/docs/autoPlacement
  435. */
  436. const autoPlacement = function (options) {
  437. if (options === void 0) {
  438. options = {};
  439. }
  440. return {
  441. name: 'autoPlacement',
  442. options,
  443. async fn(state) {
  444. var _middlewareData$autoP, _middlewareData$autoP2, _placementsThatFitOnE;
  445. const {
  446. rects,
  447. middlewareData,
  448. placement,
  449. platform,
  450. elements
  451. } = state;
  452. const {
  453. crossAxis = false,
  454. alignment,
  455. allowedPlacements = placements,
  456. autoAlignment = true,
  457. ...detectOverflowOptions
  458. } = evaluate(options, state);
  459. const placements$1 = alignment !== undefined || allowedPlacements === placements ? getPlacementList(alignment || null, autoAlignment, allowedPlacements) : allowedPlacements;
  460. const overflow = await detectOverflow(state, detectOverflowOptions);
  461. const currentIndex = ((_middlewareData$autoP = middlewareData.autoPlacement) == null ? void 0 : _middlewareData$autoP.index) || 0;
  462. const currentPlacement = placements$1[currentIndex];
  463. if (currentPlacement == null) {
  464. return {};
  465. }
  466. const alignmentSides = getAlignmentSides(currentPlacement, rects, await (platform.isRTL == null ? void 0 : platform.isRTL(elements.floating)));
  467. // Make `computeCoords` start from the right place.
  468. if (placement !== currentPlacement) {
  469. return {
  470. reset: {
  471. placement: placements$1[0]
  472. }
  473. };
  474. }
  475. const currentOverflows = [overflow[getSide(currentPlacement)], overflow[alignmentSides[0]], overflow[alignmentSides[1]]];
  476. const allOverflows = [...(((_middlewareData$autoP2 = middlewareData.autoPlacement) == null ? void 0 : _middlewareData$autoP2.overflows) || []), {
  477. placement: currentPlacement,
  478. overflows: currentOverflows
  479. }];
  480. const nextPlacement = placements$1[currentIndex + 1];
  481. // There are more placements to check.
  482. if (nextPlacement) {
  483. return {
  484. data: {
  485. index: currentIndex + 1,
  486. overflows: allOverflows
  487. },
  488. reset: {
  489. placement: nextPlacement
  490. }
  491. };
  492. }
  493. const placementsSortedByMostSpace = allOverflows.map(d => {
  494. const alignment = getAlignment(d.placement);
  495. return [d.placement, alignment && crossAxis ?
  496. // Check along the mainAxis and main crossAxis side.
  497. d.overflows.slice(0, 2).reduce((acc, v) => acc + v, 0) :
  498. // Check only the mainAxis.
  499. d.overflows[0], d.overflows];
  500. }).sort((a, b) => a[1] - b[1]);
  501. const placementsThatFitOnEachSide = placementsSortedByMostSpace.filter(d => d[2].slice(0,
  502. // Aligned placements should not check their opposite crossAxis
  503. // side.
  504. getAlignment(d[0]) ? 2 : 3).every(v => v <= 0));
  505. const resetPlacement = ((_placementsThatFitOnE = placementsThatFitOnEachSide[0]) == null ? void 0 : _placementsThatFitOnE[0]) || placementsSortedByMostSpace[0][0];
  506. if (resetPlacement !== placement) {
  507. return {
  508. data: {
  509. index: currentIndex + 1,
  510. overflows: allOverflows
  511. },
  512. reset: {
  513. placement: resetPlacement
  514. }
  515. };
  516. }
  517. return {};
  518. }
  519. };
  520. };
  521. /**
  522. * Optimizes the visibility of the floating element by flipping the `placement`
  523. * in order to keep it in view when the preferred placement(s) will overflow the
  524. * clipping boundary. Alternative to `autoPlacement`.
  525. * @see https://floating-ui.com/docs/flip
  526. */
  527. const flip = function (options) {
  528. if (options === void 0) {
  529. options = {};
  530. }
  531. return {
  532. name: 'flip',
  533. options,
  534. async fn(state) {
  535. var _middlewareData$arrow, _middlewareData$flip;
  536. const {
  537. placement,
  538. middlewareData,
  539. rects,
  540. initialPlacement,
  541. platform,
  542. elements
  543. } = state;
  544. const {
  545. mainAxis: checkMainAxis = true,
  546. crossAxis: checkCrossAxis = true,
  547. fallbackPlacements: specifiedFallbackPlacements,
  548. fallbackStrategy = 'bestFit',
  549. fallbackAxisSideDirection = 'none',
  550. flipAlignment = true,
  551. ...detectOverflowOptions
  552. } = evaluate(options, state);
  553. // If a reset by the arrow was caused due to an alignment offset being
  554. // added, we should skip any logic now since `flip()` has already done its
  555. // work.
  556. // https://github.com/floating-ui/floating-ui/issues/2549#issuecomment-1719601643
  557. if ((_middlewareData$arrow = middlewareData.arrow) != null && _middlewareData$arrow.alignmentOffset) {
  558. return {};
  559. }
  560. const side = getSide(placement);
  561. const initialSideAxis = getSideAxis(initialPlacement);
  562. const isBasePlacement = getSide(initialPlacement) === initialPlacement;
  563. const rtl = await (platform.isRTL == null ? void 0 : platform.isRTL(elements.floating));
  564. const fallbackPlacements = specifiedFallbackPlacements || (isBasePlacement || !flipAlignment ? [getOppositePlacement(initialPlacement)] : getExpandedPlacements(initialPlacement));
  565. const hasFallbackAxisSideDirection = fallbackAxisSideDirection !== 'none';
  566. if (!specifiedFallbackPlacements && hasFallbackAxisSideDirection) {
  567. fallbackPlacements.push(...getOppositeAxisPlacements(initialPlacement, flipAlignment, fallbackAxisSideDirection, rtl));
  568. }
  569. const placements = [initialPlacement, ...fallbackPlacements];
  570. const overflow = await detectOverflow(state, detectOverflowOptions);
  571. const overflows = [];
  572. let overflowsData = ((_middlewareData$flip = middlewareData.flip) == null ? void 0 : _middlewareData$flip.overflows) || [];
  573. if (checkMainAxis) {
  574. overflows.push(overflow[side]);
  575. }
  576. if (checkCrossAxis) {
  577. const sides = getAlignmentSides(placement, rects, rtl);
  578. overflows.push(overflow[sides[0]], overflow[sides[1]]);
  579. }
  580. overflowsData = [...overflowsData, {
  581. placement,
  582. overflows
  583. }];
  584. // One or more sides is overflowing.
  585. if (!overflows.every(side => side <= 0)) {
  586. var _middlewareData$flip2, _overflowsData$filter;
  587. const nextIndex = (((_middlewareData$flip2 = middlewareData.flip) == null ? void 0 : _middlewareData$flip2.index) || 0) + 1;
  588. const nextPlacement = placements[nextIndex];
  589. if (nextPlacement) {
  590. // Try next placement and re-run the lifecycle.
  591. return {
  592. data: {
  593. index: nextIndex,
  594. overflows: overflowsData
  595. },
  596. reset: {
  597. placement: nextPlacement
  598. }
  599. };
  600. }
  601. // First, find the candidates that fit on the mainAxis side of overflow,
  602. // then find the placement that fits the best on the main crossAxis side.
  603. let resetPlacement = (_overflowsData$filter = overflowsData.filter(d => d.overflows[0] <= 0).sort((a, b) => a.overflows[1] - b.overflows[1])[0]) == null ? void 0 : _overflowsData$filter.placement;
  604. // Otherwise fallback.
  605. if (!resetPlacement) {
  606. switch (fallbackStrategy) {
  607. case 'bestFit':
  608. {
  609. var _overflowsData$filter2;
  610. const placement = (_overflowsData$filter2 = overflowsData.filter(d => {
  611. if (hasFallbackAxisSideDirection) {
  612. const currentSideAxis = getSideAxis(d.placement);
  613. return currentSideAxis === initialSideAxis ||
  614. // Create a bias to the `y` side axis due to horizontal
  615. // reading directions favoring greater width.
  616. currentSideAxis === 'y';
  617. }
  618. return true;
  619. }).map(d => [d.placement, d.overflows.filter(overflow => overflow > 0).reduce((acc, overflow) => acc + overflow, 0)]).sort((a, b) => a[1] - b[1])[0]) == null ? void 0 : _overflowsData$filter2[0];
  620. if (placement) {
  621. resetPlacement = placement;
  622. }
  623. break;
  624. }
  625. case 'initialPlacement':
  626. resetPlacement = initialPlacement;
  627. break;
  628. }
  629. }
  630. if (placement !== resetPlacement) {
  631. return {
  632. reset: {
  633. placement: resetPlacement
  634. }
  635. };
  636. }
  637. }
  638. return {};
  639. }
  640. };
  641. };
  642. function getSideOffsets(overflow, rect) {
  643. return {
  644. top: overflow.top - rect.height,
  645. right: overflow.right - rect.width,
  646. bottom: overflow.bottom - rect.height,
  647. left: overflow.left - rect.width
  648. };
  649. }
  650. function isAnySideFullyClipped(overflow) {
  651. return sides.some(side => overflow[side] >= 0);
  652. }
  653. /**
  654. * Provides data to hide the floating element in applicable situations, such as
  655. * when it is not in the same clipping context as the reference element.
  656. * @see https://floating-ui.com/docs/hide
  657. */
  658. const hide = function (options) {
  659. if (options === void 0) {
  660. options = {};
  661. }
  662. return {
  663. name: 'hide',
  664. options,
  665. async fn(state) {
  666. const {
  667. rects
  668. } = state;
  669. const {
  670. strategy = 'referenceHidden',
  671. ...detectOverflowOptions
  672. } = evaluate(options, state);
  673. switch (strategy) {
  674. case 'referenceHidden':
  675. {
  676. const overflow = await detectOverflow(state, {
  677. ...detectOverflowOptions,
  678. elementContext: 'reference'
  679. });
  680. const offsets = getSideOffsets(overflow, rects.reference);
  681. return {
  682. data: {
  683. referenceHiddenOffsets: offsets,
  684. referenceHidden: isAnySideFullyClipped(offsets)
  685. }
  686. };
  687. }
  688. case 'escaped':
  689. {
  690. const overflow = await detectOverflow(state, {
  691. ...detectOverflowOptions,
  692. altBoundary: true
  693. });
  694. const offsets = getSideOffsets(overflow, rects.floating);
  695. return {
  696. data: {
  697. escapedOffsets: offsets,
  698. escaped: isAnySideFullyClipped(offsets)
  699. }
  700. };
  701. }
  702. default:
  703. {
  704. return {};
  705. }
  706. }
  707. }
  708. };
  709. };
  710. function getBoundingRect(rects) {
  711. const minX = min(...rects.map(rect => rect.left));
  712. const minY = min(...rects.map(rect => rect.top));
  713. const maxX = max(...rects.map(rect => rect.right));
  714. const maxY = max(...rects.map(rect => rect.bottom));
  715. return {
  716. x: minX,
  717. y: minY,
  718. width: maxX - minX,
  719. height: maxY - minY
  720. };
  721. }
  722. function getRectsByLine(rects) {
  723. const sortedRects = rects.slice().sort((a, b) => a.y - b.y);
  724. const groups = [];
  725. let prevRect = null;
  726. for (let i = 0; i < sortedRects.length; i++) {
  727. const rect = sortedRects[i];
  728. if (!prevRect || rect.y - prevRect.y > prevRect.height / 2) {
  729. groups.push([rect]);
  730. } else {
  731. groups[groups.length - 1].push(rect);
  732. }
  733. prevRect = rect;
  734. }
  735. return groups.map(rect => rectToClientRect(getBoundingRect(rect)));
  736. }
  737. /**
  738. * Provides improved positioning for inline reference elements that can span
  739. * over multiple lines, such as hyperlinks or range selections.
  740. * @see https://floating-ui.com/docs/inline
  741. */
  742. const inline = function (options) {
  743. if (options === void 0) {
  744. options = {};
  745. }
  746. return {
  747. name: 'inline',
  748. options,
  749. async fn(state) {
  750. const {
  751. placement,
  752. elements,
  753. rects,
  754. platform,
  755. strategy
  756. } = state;
  757. // A MouseEvent's client{X,Y} coords can be up to 2 pixels off a
  758. // ClientRect's bounds, despite the event listener being triggered. A
  759. // padding of 2 seems to handle this issue.
  760. const {
  761. padding = 2,
  762. x,
  763. y
  764. } = evaluate(options, state);
  765. const nativeClientRects = Array.from((await (platform.getClientRects == null ? void 0 : platform.getClientRects(elements.reference))) || []);
  766. const clientRects = getRectsByLine(nativeClientRects);
  767. const fallback = rectToClientRect(getBoundingRect(nativeClientRects));
  768. const paddingObject = getPaddingObject(padding);
  769. function getBoundingClientRect() {
  770. // There are two rects and they are disjoined.
  771. if (clientRects.length === 2 && clientRects[0].left > clientRects[1].right && x != null && y != null) {
  772. // Find the first rect in which the point is fully inside.
  773. return clientRects.find(rect => x > rect.left - paddingObject.left && x < rect.right + paddingObject.right && y > rect.top - paddingObject.top && y < rect.bottom + paddingObject.bottom) || fallback;
  774. }
  775. // There are 2 or more connected rects.
  776. if (clientRects.length >= 2) {
  777. if (getSideAxis(placement) === 'y') {
  778. const firstRect = clientRects[0];
  779. const lastRect = clientRects[clientRects.length - 1];
  780. const isTop = getSide(placement) === 'top';
  781. const top = firstRect.top;
  782. const bottom = lastRect.bottom;
  783. const left = isTop ? firstRect.left : lastRect.left;
  784. const right = isTop ? firstRect.right : lastRect.right;
  785. const width = right - left;
  786. const height = bottom - top;
  787. return {
  788. top,
  789. bottom,
  790. left,
  791. right,
  792. width,
  793. height,
  794. x: left,
  795. y: top
  796. };
  797. }
  798. const isLeftSide = getSide(placement) === 'left';
  799. const maxRight = max(...clientRects.map(rect => rect.right));
  800. const minLeft = min(...clientRects.map(rect => rect.left));
  801. const measureRects = clientRects.filter(rect => isLeftSide ? rect.left === minLeft : rect.right === maxRight);
  802. const top = measureRects[0].top;
  803. const bottom = measureRects[measureRects.length - 1].bottom;
  804. const left = minLeft;
  805. const right = maxRight;
  806. const width = right - left;
  807. const height = bottom - top;
  808. return {
  809. top,
  810. bottom,
  811. left,
  812. right,
  813. width,
  814. height,
  815. x: left,
  816. y: top
  817. };
  818. }
  819. return fallback;
  820. }
  821. const resetRects = await platform.getElementRects({
  822. reference: {
  823. getBoundingClientRect
  824. },
  825. floating: elements.floating,
  826. strategy
  827. });
  828. if (rects.reference.x !== resetRects.reference.x || rects.reference.y !== resetRects.reference.y || rects.reference.width !== resetRects.reference.width || rects.reference.height !== resetRects.reference.height) {
  829. return {
  830. reset: {
  831. rects: resetRects
  832. }
  833. };
  834. }
  835. return {};
  836. }
  837. };
  838. };
  839. // For type backwards-compatibility, the `OffsetOptions` type was also
  840. // Derivable.
  841. async function convertValueToCoords(state, options) {
  842. const {
  843. placement,
  844. platform,
  845. elements
  846. } = state;
  847. const rtl = await (platform.isRTL == null ? void 0 : platform.isRTL(elements.floating));
  848. const side = getSide(placement);
  849. const alignment = getAlignment(placement);
  850. const isVertical = getSideAxis(placement) === 'y';
  851. const mainAxisMulti = ['left', 'top'].includes(side) ? -1 : 1;
  852. const crossAxisMulti = rtl && isVertical ? -1 : 1;
  853. const rawValue = evaluate(options, state);
  854. // eslint-disable-next-line prefer-const
  855. let {
  856. mainAxis,
  857. crossAxis,
  858. alignmentAxis
  859. } = typeof rawValue === 'number' ? {
  860. mainAxis: rawValue,
  861. crossAxis: 0,
  862. alignmentAxis: null
  863. } : {
  864. mainAxis: rawValue.mainAxis || 0,
  865. crossAxis: rawValue.crossAxis || 0,
  866. alignmentAxis: rawValue.alignmentAxis
  867. };
  868. if (alignment && typeof alignmentAxis === 'number') {
  869. crossAxis = alignment === 'end' ? alignmentAxis * -1 : alignmentAxis;
  870. }
  871. return isVertical ? {
  872. x: crossAxis * crossAxisMulti,
  873. y: mainAxis * mainAxisMulti
  874. } : {
  875. x: mainAxis * mainAxisMulti,
  876. y: crossAxis * crossAxisMulti
  877. };
  878. }
  879. /**
  880. * Modifies the placement by translating the floating element along the
  881. * specified axes.
  882. * A number (shorthand for `mainAxis` or distance), or an axes configuration
  883. * object may be passed.
  884. * @see https://floating-ui.com/docs/offset
  885. */
  886. const offset = function (options) {
  887. if (options === void 0) {
  888. options = 0;
  889. }
  890. return {
  891. name: 'offset',
  892. options,
  893. async fn(state) {
  894. var _middlewareData$offse, _middlewareData$arrow;
  895. const {
  896. x,
  897. y,
  898. placement,
  899. middlewareData
  900. } = state;
  901. const diffCoords = await convertValueToCoords(state, options);
  902. // If the placement is the same and the arrow caused an alignment offset
  903. // then we don't need to change the positioning coordinates.
  904. if (placement === ((_middlewareData$offse = middlewareData.offset) == null ? void 0 : _middlewareData$offse.placement) && (_middlewareData$arrow = middlewareData.arrow) != null && _middlewareData$arrow.alignmentOffset) {
  905. return {};
  906. }
  907. return {
  908. x: x + diffCoords.x,
  909. y: y + diffCoords.y,
  910. data: {
  911. ...diffCoords,
  912. placement
  913. }
  914. };
  915. }
  916. };
  917. };
  918. /**
  919. * Optimizes the visibility of the floating element by shifting it in order to
  920. * keep it in view when it will overflow the clipping boundary.
  921. * @see https://floating-ui.com/docs/shift
  922. */
  923. const shift = function (options) {
  924. if (options === void 0) {
  925. options = {};
  926. }
  927. return {
  928. name: 'shift',
  929. options,
  930. async fn(state) {
  931. const {
  932. x,
  933. y,
  934. placement
  935. } = state;
  936. const {
  937. mainAxis: checkMainAxis = true,
  938. crossAxis: checkCrossAxis = false,
  939. limiter = {
  940. fn: _ref => {
  941. let {
  942. x,
  943. y
  944. } = _ref;
  945. return {
  946. x,
  947. y
  948. };
  949. }
  950. },
  951. ...detectOverflowOptions
  952. } = evaluate(options, state);
  953. const coords = {
  954. x,
  955. y
  956. };
  957. const overflow = await detectOverflow(state, detectOverflowOptions);
  958. const crossAxis = getSideAxis(getSide(placement));
  959. const mainAxis = getOppositeAxis(crossAxis);
  960. let mainAxisCoord = coords[mainAxis];
  961. let crossAxisCoord = coords[crossAxis];
  962. if (checkMainAxis) {
  963. const minSide = mainAxis === 'y' ? 'top' : 'left';
  964. const maxSide = mainAxis === 'y' ? 'bottom' : 'right';
  965. const min = mainAxisCoord + overflow[minSide];
  966. const max = mainAxisCoord - overflow[maxSide];
  967. mainAxisCoord = clamp(min, mainAxisCoord, max);
  968. }
  969. if (checkCrossAxis) {
  970. const minSide = crossAxis === 'y' ? 'top' : 'left';
  971. const maxSide = crossAxis === 'y' ? 'bottom' : 'right';
  972. const min = crossAxisCoord + overflow[minSide];
  973. const max = crossAxisCoord - overflow[maxSide];
  974. crossAxisCoord = clamp(min, crossAxisCoord, max);
  975. }
  976. const limitedCoords = limiter.fn({
  977. ...state,
  978. [mainAxis]: mainAxisCoord,
  979. [crossAxis]: crossAxisCoord
  980. });
  981. return {
  982. ...limitedCoords,
  983. data: {
  984. x: limitedCoords.x - x,
  985. y: limitedCoords.y - y,
  986. enabled: {
  987. [mainAxis]: checkMainAxis,
  988. [crossAxis]: checkCrossAxis
  989. }
  990. }
  991. };
  992. }
  993. };
  994. };
  995. /**
  996. * Built-in `limiter` that will stop `shift()` at a certain point.
  997. */
  998. const limitShift = function (options) {
  999. if (options === void 0) {
  1000. options = {};
  1001. }
  1002. return {
  1003. options,
  1004. fn(state) {
  1005. const {
  1006. x,
  1007. y,
  1008. placement,
  1009. rects,
  1010. middlewareData
  1011. } = state;
  1012. const {
  1013. offset = 0,
  1014. mainAxis: checkMainAxis = true,
  1015. crossAxis: checkCrossAxis = true
  1016. } = evaluate(options, state);
  1017. const coords = {
  1018. x,
  1019. y
  1020. };
  1021. const crossAxis = getSideAxis(placement);
  1022. const mainAxis = getOppositeAxis(crossAxis);
  1023. let mainAxisCoord = coords[mainAxis];
  1024. let crossAxisCoord = coords[crossAxis];
  1025. const rawOffset = evaluate(offset, state);
  1026. const computedOffset = typeof rawOffset === 'number' ? {
  1027. mainAxis: rawOffset,
  1028. crossAxis: 0
  1029. } : {
  1030. mainAxis: 0,
  1031. crossAxis: 0,
  1032. ...rawOffset
  1033. };
  1034. if (checkMainAxis) {
  1035. const len = mainAxis === 'y' ? 'height' : 'width';
  1036. const limitMin = rects.reference[mainAxis] - rects.floating[len] + computedOffset.mainAxis;
  1037. const limitMax = rects.reference[mainAxis] + rects.reference[len] - computedOffset.mainAxis;
  1038. if (mainAxisCoord < limitMin) {
  1039. mainAxisCoord = limitMin;
  1040. } else if (mainAxisCoord > limitMax) {
  1041. mainAxisCoord = limitMax;
  1042. }
  1043. }
  1044. if (checkCrossAxis) {
  1045. var _middlewareData$offse, _middlewareData$offse2;
  1046. const len = mainAxis === 'y' ? 'width' : 'height';
  1047. const isOriginSide = ['top', 'left'].includes(getSide(placement));
  1048. const limitMin = rects.reference[crossAxis] - rects.floating[len] + (isOriginSide ? ((_middlewareData$offse = middlewareData.offset) == null ? void 0 : _middlewareData$offse[crossAxis]) || 0 : 0) + (isOriginSide ? 0 : computedOffset.crossAxis);
  1049. const limitMax = rects.reference[crossAxis] + rects.reference[len] + (isOriginSide ? 0 : ((_middlewareData$offse2 = middlewareData.offset) == null ? void 0 : _middlewareData$offse2[crossAxis]) || 0) - (isOriginSide ? computedOffset.crossAxis : 0);
  1050. if (crossAxisCoord < limitMin) {
  1051. crossAxisCoord = limitMin;
  1052. } else if (crossAxisCoord > limitMax) {
  1053. crossAxisCoord = limitMax;
  1054. }
  1055. }
  1056. return {
  1057. [mainAxis]: mainAxisCoord,
  1058. [crossAxis]: crossAxisCoord
  1059. };
  1060. }
  1061. };
  1062. };
  1063. /**
  1064. * Provides data that allows you to change the size of the floating element —
  1065. * for instance, prevent it from overflowing the clipping boundary or match the
  1066. * width of the reference element.
  1067. * @see https://floating-ui.com/docs/size
  1068. */
  1069. const size = function (options) {
  1070. if (options === void 0) {
  1071. options = {};
  1072. }
  1073. return {
  1074. name: 'size',
  1075. options,
  1076. async fn(state) {
  1077. var _state$middlewareData, _state$middlewareData2;
  1078. const {
  1079. placement,
  1080. rects,
  1081. platform,
  1082. elements
  1083. } = state;
  1084. const {
  1085. apply = () => {},
  1086. ...detectOverflowOptions
  1087. } = evaluate(options, state);
  1088. const overflow = await detectOverflow(state, detectOverflowOptions);
  1089. const side = getSide(placement);
  1090. const alignment = getAlignment(placement);
  1091. const isYAxis = getSideAxis(placement) === 'y';
  1092. const {
  1093. width,
  1094. height
  1095. } = rects.floating;
  1096. let heightSide;
  1097. let widthSide;
  1098. if (side === 'top' || side === 'bottom') {
  1099. heightSide = side;
  1100. widthSide = alignment === ((await (platform.isRTL == null ? void 0 : platform.isRTL(elements.floating))) ? 'start' : 'end') ? 'left' : 'right';
  1101. } else {
  1102. widthSide = side;
  1103. heightSide = alignment === 'end' ? 'top' : 'bottom';
  1104. }
  1105. const maximumClippingHeight = height - overflow.top - overflow.bottom;
  1106. const maximumClippingWidth = width - overflow.left - overflow.right;
  1107. const overflowAvailableHeight = min(height - overflow[heightSide], maximumClippingHeight);
  1108. const overflowAvailableWidth = min(width - overflow[widthSide], maximumClippingWidth);
  1109. const noShift = !state.middlewareData.shift;
  1110. let availableHeight = overflowAvailableHeight;
  1111. let availableWidth = overflowAvailableWidth;
  1112. if ((_state$middlewareData = state.middlewareData.shift) != null && _state$middlewareData.enabled.x) {
  1113. availableWidth = maximumClippingWidth;
  1114. }
  1115. if ((_state$middlewareData2 = state.middlewareData.shift) != null && _state$middlewareData2.enabled.y) {
  1116. availableHeight = maximumClippingHeight;
  1117. }
  1118. if (noShift && !alignment) {
  1119. const xMin = max(overflow.left, 0);
  1120. const xMax = max(overflow.right, 0);
  1121. const yMin = max(overflow.top, 0);
  1122. const yMax = max(overflow.bottom, 0);
  1123. if (isYAxis) {
  1124. availableWidth = width - 2 * (xMin !== 0 || xMax !== 0 ? xMin + xMax : max(overflow.left, overflow.right));
  1125. } else {
  1126. availableHeight = height - 2 * (yMin !== 0 || yMax !== 0 ? yMin + yMax : max(overflow.top, overflow.bottom));
  1127. }
  1128. }
  1129. await apply({
  1130. ...state,
  1131. availableWidth,
  1132. availableHeight
  1133. });
  1134. const nextDimensions = await platform.getDimensions(elements.floating);
  1135. if (width !== nextDimensions.width || height !== nextDimensions.height) {
  1136. return {
  1137. reset: {
  1138. rects: true
  1139. }
  1140. };
  1141. }
  1142. return {};
  1143. }
  1144. };
  1145. };
  1146. exports.arrow = arrow;
  1147. exports.autoPlacement = autoPlacement;
  1148. exports.computePosition = computePosition;
  1149. exports.detectOverflow = detectOverflow;
  1150. exports.flip = flip;
  1151. exports.hide = hide;
  1152. exports.inline = inline;
  1153. exports.limitShift = limitShift;
  1154. exports.offset = offset;
  1155. exports.rectToClientRect = rectToClientRect;
  1156. exports.shift = shift;
  1157. exports.size = size;
  1158. }));