floating-ui.core.browser.mjs 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. /**
  2. * Custom positioning reference element.
  3. * @see https://floating-ui.com/docs/virtual-elements
  4. */
  5. const sides = ['top', 'right', 'bottom', 'left'];
  6. const alignments = ['start', 'end'];
  7. const placements = /*#__PURE__*/sides.reduce((acc, side) => acc.concat(side, side + "-" + alignments[0], side + "-" + alignments[1]), []);
  8. const min = Math.min;
  9. const max = Math.max;
  10. const oppositeSideMap = {
  11. left: 'right',
  12. right: 'left',
  13. bottom: 'top',
  14. top: 'bottom'
  15. };
  16. const oppositeAlignmentMap = {
  17. start: 'end',
  18. end: 'start'
  19. };
  20. function clamp(start, value, end) {
  21. return max(start, min(value, end));
  22. }
  23. function evaluate(value, param) {
  24. return typeof value === 'function' ? value(param) : value;
  25. }
  26. function getSide(placement) {
  27. return placement.split('-')[0];
  28. }
  29. function getAlignment(placement) {
  30. return placement.split('-')[1];
  31. }
  32. function getOppositeAxis(axis) {
  33. return axis === 'x' ? 'y' : 'x';
  34. }
  35. function getAxisLength(axis) {
  36. return axis === 'y' ? 'height' : 'width';
  37. }
  38. function getSideAxis(placement) {
  39. return ['top', 'bottom'].includes(getSide(placement)) ? 'y' : 'x';
  40. }
  41. function getAlignmentAxis(placement) {
  42. return getOppositeAxis(getSideAxis(placement));
  43. }
  44. function getAlignmentSides(placement, rects, rtl) {
  45. if (rtl === void 0) {
  46. rtl = false;
  47. }
  48. const alignment = getAlignment(placement);
  49. const alignmentAxis = getAlignmentAxis(placement);
  50. const length = getAxisLength(alignmentAxis);
  51. let mainAlignmentSide = alignmentAxis === 'x' ? alignment === (rtl ? 'end' : 'start') ? 'right' : 'left' : alignment === 'start' ? 'bottom' : 'top';
  52. if (rects.reference[length] > rects.floating[length]) {
  53. mainAlignmentSide = getOppositePlacement(mainAlignmentSide);
  54. }
  55. return [mainAlignmentSide, getOppositePlacement(mainAlignmentSide)];
  56. }
  57. function getExpandedPlacements(placement) {
  58. const oppositePlacement = getOppositePlacement(placement);
  59. return [getOppositeAlignmentPlacement(placement), oppositePlacement, getOppositeAlignmentPlacement(oppositePlacement)];
  60. }
  61. function getOppositeAlignmentPlacement(placement) {
  62. return placement.replace(/start|end/g, alignment => oppositeAlignmentMap[alignment]);
  63. }
  64. function getSideList(side, isStart, rtl) {
  65. const lr = ['left', 'right'];
  66. const rl = ['right', 'left'];
  67. const tb = ['top', 'bottom'];
  68. const bt = ['bottom', 'top'];
  69. switch (side) {
  70. case 'top':
  71. case 'bottom':
  72. if (rtl) return isStart ? rl : lr;
  73. return isStart ? lr : rl;
  74. case 'left':
  75. case 'right':
  76. return isStart ? tb : bt;
  77. default:
  78. return [];
  79. }
  80. }
  81. function getOppositeAxisPlacements(placement, flipAlignment, direction, rtl) {
  82. const alignment = getAlignment(placement);
  83. let list = getSideList(getSide(placement), direction === 'start', rtl);
  84. if (alignment) {
  85. list = list.map(side => side + "-" + alignment);
  86. if (flipAlignment) {
  87. list = list.concat(list.map(getOppositeAlignmentPlacement));
  88. }
  89. }
  90. return list;
  91. }
  92. function getOppositePlacement(placement) {
  93. return placement.replace(/left|right|bottom|top/g, side => oppositeSideMap[side]);
  94. }
  95. function expandPaddingObject(padding) {
  96. return {
  97. top: 0,
  98. right: 0,
  99. bottom: 0,
  100. left: 0,
  101. ...padding
  102. };
  103. }
  104. function getPaddingObject(padding) {
  105. return typeof padding !== 'number' ? expandPaddingObject(padding) : {
  106. top: padding,
  107. right: padding,
  108. bottom: padding,
  109. left: padding
  110. };
  111. }
  112. function rectToClientRect(rect) {
  113. const {
  114. x,
  115. y,
  116. width,
  117. height
  118. } = rect;
  119. return {
  120. width,
  121. height,
  122. top: y,
  123. left: x,
  124. right: x + width,
  125. bottom: y + height,
  126. x,
  127. y
  128. };
  129. }
  130. function computeCoordsFromPlacement(_ref, placement, rtl) {
  131. let {
  132. reference,
  133. floating
  134. } = _ref;
  135. const sideAxis = getSideAxis(placement);
  136. const alignmentAxis = getAlignmentAxis(placement);
  137. const alignLength = getAxisLength(alignmentAxis);
  138. const side = getSide(placement);
  139. const isVertical = sideAxis === 'y';
  140. const commonX = reference.x + reference.width / 2 - floating.width / 2;
  141. const commonY = reference.y + reference.height / 2 - floating.height / 2;
  142. const commonAlign = reference[alignLength] / 2 - floating[alignLength] / 2;
  143. let coords;
  144. switch (side) {
  145. case 'top':
  146. coords = {
  147. x: commonX,
  148. y: reference.y - floating.height
  149. };
  150. break;
  151. case 'bottom':
  152. coords = {
  153. x: commonX,
  154. y: reference.y + reference.height
  155. };
  156. break;
  157. case 'right':
  158. coords = {
  159. x: reference.x + reference.width,
  160. y: commonY
  161. };
  162. break;
  163. case 'left':
  164. coords = {
  165. x: reference.x - floating.width,
  166. y: commonY
  167. };
  168. break;
  169. default:
  170. coords = {
  171. x: reference.x,
  172. y: reference.y
  173. };
  174. }
  175. switch (getAlignment(placement)) {
  176. case 'start':
  177. coords[alignmentAxis] -= commonAlign * (rtl && isVertical ? -1 : 1);
  178. break;
  179. case 'end':
  180. coords[alignmentAxis] += commonAlign * (rtl && isVertical ? -1 : 1);
  181. break;
  182. }
  183. return coords;
  184. }
  185. /**
  186. * Computes the `x` and `y` coordinates that will place the floating element
  187. * next to a given reference element.
  188. *
  189. * This export does not have any `platform` interface logic. You will need to
  190. * write one for the platform you are using Floating UI with.
  191. */
  192. const computePosition = async (reference, floating, config) => {
  193. const {
  194. placement = 'bottom',
  195. strategy = 'absolute',
  196. middleware = [],
  197. platform
  198. } = config;
  199. const validMiddleware = middleware.filter(Boolean);
  200. const rtl = await (platform.isRTL == null ? void 0 : platform.isRTL(floating));
  201. let rects = await platform.getElementRects({
  202. reference,
  203. floating,
  204. strategy
  205. });
  206. let {
  207. x,
  208. y
  209. } = computeCoordsFromPlacement(rects, placement, rtl);
  210. let statefulPlacement = placement;
  211. let middlewareData = {};
  212. let resetCount = 0;
  213. for (let i = 0; i < validMiddleware.length; i++) {
  214. const {
  215. name,
  216. fn
  217. } = validMiddleware[i];
  218. const {
  219. x: nextX,
  220. y: nextY,
  221. data,
  222. reset
  223. } = await fn({
  224. x,
  225. y,
  226. initialPlacement: placement,
  227. placement: statefulPlacement,
  228. strategy,
  229. middlewareData,
  230. rects,
  231. platform,
  232. elements: {
  233. reference,
  234. floating
  235. }
  236. });
  237. x = nextX != null ? nextX : x;
  238. y = nextY != null ? nextY : y;
  239. middlewareData = {
  240. ...middlewareData,
  241. [name]: {
  242. ...middlewareData[name],
  243. ...data
  244. }
  245. };
  246. if (reset && resetCount <= 50) {
  247. resetCount++;
  248. if (typeof reset === 'object') {
  249. if (reset.placement) {
  250. statefulPlacement = reset.placement;
  251. }
  252. if (reset.rects) {
  253. rects = reset.rects === true ? await platform.getElementRects({
  254. reference,
  255. floating,
  256. strategy
  257. }) : reset.rects;
  258. }
  259. ({
  260. x,
  261. y
  262. } = computeCoordsFromPlacement(rects, statefulPlacement, rtl));
  263. }
  264. i = -1;
  265. }
  266. }
  267. return {
  268. x,
  269. y,
  270. placement: statefulPlacement,
  271. strategy,
  272. middlewareData
  273. };
  274. };
  275. /**
  276. * Resolves with an object of overflow side offsets that determine how much the
  277. * element is overflowing a given clipping boundary on each side.
  278. * - positive = overflowing the boundary by that number of pixels
  279. * - negative = how many pixels left before it will overflow
  280. * - 0 = lies flush with the boundary
  281. * @see https://floating-ui.com/docs/detectOverflow
  282. */
  283. async function detectOverflow(state, options) {
  284. var _await$platform$isEle;
  285. if (options === void 0) {
  286. options = {};
  287. }
  288. const {
  289. x,
  290. y,
  291. platform,
  292. rects,
  293. elements,
  294. strategy
  295. } = state;
  296. const {
  297. boundary = 'clippingAncestors',
  298. rootBoundary = 'viewport',
  299. elementContext = 'floating',
  300. altBoundary = false,
  301. padding = 0
  302. } = evaluate(options, state);
  303. const paddingObject = getPaddingObject(padding);
  304. const altContext = elementContext === 'floating' ? 'reference' : 'floating';
  305. const element = elements[altBoundary ? altContext : elementContext];
  306. const clippingClientRect = rectToClientRect(await platform.getClippingRect({
  307. 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))),
  308. boundary,
  309. rootBoundary,
  310. strategy
  311. }));
  312. const rect = elementContext === 'floating' ? {
  313. x,
  314. y,
  315. width: rects.floating.width,
  316. height: rects.floating.height
  317. } : rects.reference;
  318. const offsetParent = await (platform.getOffsetParent == null ? void 0 : platform.getOffsetParent(elements.floating));
  319. const offsetScale = (await (platform.isElement == null ? void 0 : platform.isElement(offsetParent))) ? (await (platform.getScale == null ? void 0 : platform.getScale(offsetParent))) || {
  320. x: 1,
  321. y: 1
  322. } : {
  323. x: 1,
  324. y: 1
  325. };
  326. const elementClientRect = rectToClientRect(platform.convertOffsetParentRelativeRectToViewportRelativeRect ? await platform.convertOffsetParentRelativeRectToViewportRelativeRect({
  327. elements,
  328. rect,
  329. offsetParent,
  330. strategy
  331. }) : rect);
  332. return {
  333. top: (clippingClientRect.top - elementClientRect.top + paddingObject.top) / offsetScale.y,
  334. bottom: (elementClientRect.bottom - clippingClientRect.bottom + paddingObject.bottom) / offsetScale.y,
  335. left: (clippingClientRect.left - elementClientRect.left + paddingObject.left) / offsetScale.x,
  336. right: (elementClientRect.right - clippingClientRect.right + paddingObject.right) / offsetScale.x
  337. };
  338. }
  339. /**
  340. * Provides data to position an inner element of the floating element so that it
  341. * appears centered to the reference element.
  342. * @see https://floating-ui.com/docs/arrow
  343. */
  344. const arrow = options => ({
  345. name: 'arrow',
  346. options,
  347. async fn(state) {
  348. const {
  349. x,
  350. y,
  351. placement,
  352. rects,
  353. platform,
  354. elements,
  355. middlewareData
  356. } = state;
  357. // Since `element` is required, we don't Partial<> the type.
  358. const {
  359. element,
  360. padding = 0
  361. } = evaluate(options, state) || {};
  362. if (element == null) {
  363. return {};
  364. }
  365. const paddingObject = getPaddingObject(padding);
  366. const coords = {
  367. x,
  368. y
  369. };
  370. const axis = getAlignmentAxis(placement);
  371. const length = getAxisLength(axis);
  372. const arrowDimensions = await platform.getDimensions(element);
  373. const isYAxis = axis === 'y';
  374. const minProp = isYAxis ? 'top' : 'left';
  375. const maxProp = isYAxis ? 'bottom' : 'right';
  376. const clientProp = isYAxis ? 'clientHeight' : 'clientWidth';
  377. const endDiff = rects.reference[length] + rects.reference[axis] - coords[axis] - rects.floating[length];
  378. const startDiff = coords[axis] - rects.reference[axis];
  379. const arrowOffsetParent = await (platform.getOffsetParent == null ? void 0 : platform.getOffsetParent(element));
  380. let clientSize = arrowOffsetParent ? arrowOffsetParent[clientProp] : 0;
  381. // DOM platform can return `window` as the `offsetParent`.
  382. if (!clientSize || !(await (platform.isElement == null ? void 0 : platform.isElement(arrowOffsetParent)))) {
  383. clientSize = elements.floating[clientProp] || rects.floating[length];
  384. }
  385. const centerToReference = endDiff / 2 - startDiff / 2;
  386. // If the padding is large enough that it causes the arrow to no longer be
  387. // centered, modify the padding so that it is centered.
  388. const largestPossiblePadding = clientSize / 2 - arrowDimensions[length] / 2 - 1;
  389. const minPadding = min(paddingObject[minProp], largestPossiblePadding);
  390. const maxPadding = min(paddingObject[maxProp], largestPossiblePadding);
  391. // Make sure the arrow doesn't overflow the floating element if the center
  392. // point is outside the floating element's bounds.
  393. const min$1 = minPadding;
  394. const max = clientSize - arrowDimensions[length] - maxPadding;
  395. const center = clientSize / 2 - arrowDimensions[length] / 2 + centerToReference;
  396. const offset = clamp(min$1, center, max);
  397. // If the reference is small enough that the arrow's padding causes it to
  398. // to point to nothing for an aligned placement, adjust the offset of the
  399. // floating element itself. To ensure `shift()` continues to take action,
  400. // a single reset is performed when this is true.
  401. const shouldAddOffset = !middlewareData.arrow && getAlignment(placement) != null && center !== offset && rects.reference[length] / 2 - (center < min$1 ? minPadding : maxPadding) - arrowDimensions[length] / 2 < 0;
  402. const alignmentOffset = shouldAddOffset ? center < min$1 ? center - min$1 : center - max : 0;
  403. return {
  404. [axis]: coords[axis] + alignmentOffset,
  405. data: {
  406. [axis]: offset,
  407. centerOffset: center - offset - alignmentOffset,
  408. ...(shouldAddOffset && {
  409. alignmentOffset
  410. })
  411. },
  412. reset: shouldAddOffset
  413. };
  414. }
  415. });
  416. function getPlacementList(alignment, autoAlignment, allowedPlacements) {
  417. const allowedPlacementsSortedByAlignment = alignment ? [...allowedPlacements.filter(placement => getAlignment(placement) === alignment), ...allowedPlacements.filter(placement => getAlignment(placement) !== alignment)] : allowedPlacements.filter(placement => getSide(placement) === placement);
  418. return allowedPlacementsSortedByAlignment.filter(placement => {
  419. if (alignment) {
  420. return getAlignment(placement) === alignment || (autoAlignment ? getOppositeAlignmentPlacement(placement) !== placement : false);
  421. }
  422. return true;
  423. });
  424. }
  425. /**
  426. * Optimizes the visibility of the floating element by choosing the placement
  427. * that has the most space available automatically, without needing to specify a
  428. * preferred placement. Alternative to `flip`.
  429. * @see https://floating-ui.com/docs/autoPlacement
  430. */
  431. const autoPlacement = function (options) {
  432. if (options === void 0) {
  433. options = {};
  434. }
  435. return {
  436. name: 'autoPlacement',
  437. options,
  438. async fn(state) {
  439. var _middlewareData$autoP, _middlewareData$autoP2, _placementsThatFitOnE;
  440. const {
  441. rects,
  442. middlewareData,
  443. placement,
  444. platform,
  445. elements
  446. } = state;
  447. const {
  448. crossAxis = false,
  449. alignment,
  450. allowedPlacements = placements,
  451. autoAlignment = true,
  452. ...detectOverflowOptions
  453. } = evaluate(options, state);
  454. const placements$1 = alignment !== undefined || allowedPlacements === placements ? getPlacementList(alignment || null, autoAlignment, allowedPlacements) : allowedPlacements;
  455. const overflow = await detectOverflow(state, detectOverflowOptions);
  456. const currentIndex = ((_middlewareData$autoP = middlewareData.autoPlacement) == null ? void 0 : _middlewareData$autoP.index) || 0;
  457. const currentPlacement = placements$1[currentIndex];
  458. if (currentPlacement == null) {
  459. return {};
  460. }
  461. const alignmentSides = getAlignmentSides(currentPlacement, rects, await (platform.isRTL == null ? void 0 : platform.isRTL(elements.floating)));
  462. // Make `computeCoords` start from the right place.
  463. if (placement !== currentPlacement) {
  464. return {
  465. reset: {
  466. placement: placements$1[0]
  467. }
  468. };
  469. }
  470. const currentOverflows = [overflow[getSide(currentPlacement)], overflow[alignmentSides[0]], overflow[alignmentSides[1]]];
  471. const allOverflows = [...(((_middlewareData$autoP2 = middlewareData.autoPlacement) == null ? void 0 : _middlewareData$autoP2.overflows) || []), {
  472. placement: currentPlacement,
  473. overflows: currentOverflows
  474. }];
  475. const nextPlacement = placements$1[currentIndex + 1];
  476. // There are more placements to check.
  477. if (nextPlacement) {
  478. return {
  479. data: {
  480. index: currentIndex + 1,
  481. overflows: allOverflows
  482. },
  483. reset: {
  484. placement: nextPlacement
  485. }
  486. };
  487. }
  488. const placementsSortedByMostSpace = allOverflows.map(d => {
  489. const alignment = getAlignment(d.placement);
  490. return [d.placement, alignment && crossAxis ?
  491. // Check along the mainAxis and main crossAxis side.
  492. d.overflows.slice(0, 2).reduce((acc, v) => acc + v, 0) :
  493. // Check only the mainAxis.
  494. d.overflows[0], d.overflows];
  495. }).sort((a, b) => a[1] - b[1]);
  496. const placementsThatFitOnEachSide = placementsSortedByMostSpace.filter(d => d[2].slice(0,
  497. // Aligned placements should not check their opposite crossAxis
  498. // side.
  499. getAlignment(d[0]) ? 2 : 3).every(v => v <= 0));
  500. const resetPlacement = ((_placementsThatFitOnE = placementsThatFitOnEachSide[0]) == null ? void 0 : _placementsThatFitOnE[0]) || placementsSortedByMostSpace[0][0];
  501. if (resetPlacement !== placement) {
  502. return {
  503. data: {
  504. index: currentIndex + 1,
  505. overflows: allOverflows
  506. },
  507. reset: {
  508. placement: resetPlacement
  509. }
  510. };
  511. }
  512. return {};
  513. }
  514. };
  515. };
  516. /**
  517. * Optimizes the visibility of the floating element by flipping the `placement`
  518. * in order to keep it in view when the preferred placement(s) will overflow the
  519. * clipping boundary. Alternative to `autoPlacement`.
  520. * @see https://floating-ui.com/docs/flip
  521. */
  522. const flip = function (options) {
  523. if (options === void 0) {
  524. options = {};
  525. }
  526. return {
  527. name: 'flip',
  528. options,
  529. async fn(state) {
  530. var _middlewareData$arrow, _middlewareData$flip;
  531. const {
  532. placement,
  533. middlewareData,
  534. rects,
  535. initialPlacement,
  536. platform,
  537. elements
  538. } = state;
  539. const {
  540. mainAxis: checkMainAxis = true,
  541. crossAxis: checkCrossAxis = true,
  542. fallbackPlacements: specifiedFallbackPlacements,
  543. fallbackStrategy = 'bestFit',
  544. fallbackAxisSideDirection = 'none',
  545. flipAlignment = true,
  546. ...detectOverflowOptions
  547. } = evaluate(options, state);
  548. // If a reset by the arrow was caused due to an alignment offset being
  549. // added, we should skip any logic now since `flip()` has already done its
  550. // work.
  551. // https://github.com/floating-ui/floating-ui/issues/2549#issuecomment-1719601643
  552. if ((_middlewareData$arrow = middlewareData.arrow) != null && _middlewareData$arrow.alignmentOffset) {
  553. return {};
  554. }
  555. const side = getSide(placement);
  556. const initialSideAxis = getSideAxis(initialPlacement);
  557. const isBasePlacement = getSide(initialPlacement) === initialPlacement;
  558. const rtl = await (platform.isRTL == null ? void 0 : platform.isRTL(elements.floating));
  559. const fallbackPlacements = specifiedFallbackPlacements || (isBasePlacement || !flipAlignment ? [getOppositePlacement(initialPlacement)] : getExpandedPlacements(initialPlacement));
  560. const hasFallbackAxisSideDirection = fallbackAxisSideDirection !== 'none';
  561. if (!specifiedFallbackPlacements && hasFallbackAxisSideDirection) {
  562. fallbackPlacements.push(...getOppositeAxisPlacements(initialPlacement, flipAlignment, fallbackAxisSideDirection, rtl));
  563. }
  564. const placements = [initialPlacement, ...fallbackPlacements];
  565. const overflow = await detectOverflow(state, detectOverflowOptions);
  566. const overflows = [];
  567. let overflowsData = ((_middlewareData$flip = middlewareData.flip) == null ? void 0 : _middlewareData$flip.overflows) || [];
  568. if (checkMainAxis) {
  569. overflows.push(overflow[side]);
  570. }
  571. if (checkCrossAxis) {
  572. const sides = getAlignmentSides(placement, rects, rtl);
  573. overflows.push(overflow[sides[0]], overflow[sides[1]]);
  574. }
  575. overflowsData = [...overflowsData, {
  576. placement,
  577. overflows
  578. }];
  579. // One or more sides is overflowing.
  580. if (!overflows.every(side => side <= 0)) {
  581. var _middlewareData$flip2, _overflowsData$filter;
  582. const nextIndex = (((_middlewareData$flip2 = middlewareData.flip) == null ? void 0 : _middlewareData$flip2.index) || 0) + 1;
  583. const nextPlacement = placements[nextIndex];
  584. if (nextPlacement) {
  585. var _overflowsData$;
  586. const ignoreCrossAxisOverflow = checkCrossAxis === 'alignment' ? initialSideAxis !== getSideAxis(nextPlacement) : false;
  587. const hasInitialMainAxisOverflow = ((_overflowsData$ = overflowsData[0]) == null ? void 0 : _overflowsData$.overflows[0]) > 0;
  588. if (!ignoreCrossAxisOverflow || hasInitialMainAxisOverflow) {
  589. // Try next placement and re-run the lifecycle.
  590. return {
  591. data: {
  592. index: nextIndex,
  593. overflows: overflowsData
  594. },
  595. reset: {
  596. placement: nextPlacement
  597. }
  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. export { arrow, autoPlacement, computePosition, detectOverflow, flip, hide, inline, limitShift, offset, rectToClientRect, shift, size };