floating-ui.utils.umd.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.FloatingUIUtils = {}));
  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 round = Math.round;
  16. const floor = Math.floor;
  17. const createCoords = v => ({
  18. x: v,
  19. y: v
  20. });
  21. const oppositeSideMap = {
  22. left: 'right',
  23. right: 'left',
  24. bottom: 'top',
  25. top: 'bottom'
  26. };
  27. const oppositeAlignmentMap = {
  28. start: 'end',
  29. end: 'start'
  30. };
  31. function clamp(start, value, end) {
  32. return max(start, min(value, end));
  33. }
  34. function evaluate(value, param) {
  35. return typeof value === 'function' ? value(param) : value;
  36. }
  37. function getSide(placement) {
  38. return placement.split('-')[0];
  39. }
  40. function getAlignment(placement) {
  41. return placement.split('-')[1];
  42. }
  43. function getOppositeAxis(axis) {
  44. return axis === 'x' ? 'y' : 'x';
  45. }
  46. function getAxisLength(axis) {
  47. return axis === 'y' ? 'height' : 'width';
  48. }
  49. function getSideAxis(placement) {
  50. return ['top', 'bottom'].includes(getSide(placement)) ? 'y' : 'x';
  51. }
  52. function getAlignmentAxis(placement) {
  53. return getOppositeAxis(getSideAxis(placement));
  54. }
  55. function getAlignmentSides(placement, rects, rtl) {
  56. if (rtl === void 0) {
  57. rtl = false;
  58. }
  59. const alignment = getAlignment(placement);
  60. const alignmentAxis = getAlignmentAxis(placement);
  61. const length = getAxisLength(alignmentAxis);
  62. let mainAlignmentSide = alignmentAxis === 'x' ? alignment === (rtl ? 'end' : 'start') ? 'right' : 'left' : alignment === 'start' ? 'bottom' : 'top';
  63. if (rects.reference[length] > rects.floating[length]) {
  64. mainAlignmentSide = getOppositePlacement(mainAlignmentSide);
  65. }
  66. return [mainAlignmentSide, getOppositePlacement(mainAlignmentSide)];
  67. }
  68. function getExpandedPlacements(placement) {
  69. const oppositePlacement = getOppositePlacement(placement);
  70. return [getOppositeAlignmentPlacement(placement), oppositePlacement, getOppositeAlignmentPlacement(oppositePlacement)];
  71. }
  72. function getOppositeAlignmentPlacement(placement) {
  73. return placement.replace(/start|end/g, alignment => oppositeAlignmentMap[alignment]);
  74. }
  75. function getSideList(side, isStart, rtl) {
  76. const lr = ['left', 'right'];
  77. const rl = ['right', 'left'];
  78. const tb = ['top', 'bottom'];
  79. const bt = ['bottom', 'top'];
  80. switch (side) {
  81. case 'top':
  82. case 'bottom':
  83. if (rtl) return isStart ? rl : lr;
  84. return isStart ? lr : rl;
  85. case 'left':
  86. case 'right':
  87. return isStart ? tb : bt;
  88. default:
  89. return [];
  90. }
  91. }
  92. function getOppositeAxisPlacements(placement, flipAlignment, direction, rtl) {
  93. const alignment = getAlignment(placement);
  94. let list = getSideList(getSide(placement), direction === 'start', rtl);
  95. if (alignment) {
  96. list = list.map(side => side + "-" + alignment);
  97. if (flipAlignment) {
  98. list = list.concat(list.map(getOppositeAlignmentPlacement));
  99. }
  100. }
  101. return list;
  102. }
  103. function getOppositePlacement(placement) {
  104. return placement.replace(/left|right|bottom|top/g, side => oppositeSideMap[side]);
  105. }
  106. function expandPaddingObject(padding) {
  107. return {
  108. top: 0,
  109. right: 0,
  110. bottom: 0,
  111. left: 0,
  112. ...padding
  113. };
  114. }
  115. function getPaddingObject(padding) {
  116. return typeof padding !== 'number' ? expandPaddingObject(padding) : {
  117. top: padding,
  118. right: padding,
  119. bottom: padding,
  120. left: padding
  121. };
  122. }
  123. function rectToClientRect(rect) {
  124. return {
  125. ...rect,
  126. top: rect.y,
  127. left: rect.x,
  128. right: rect.x + rect.width,
  129. bottom: rect.y + rect.height
  130. };
  131. }
  132. exports.alignments = alignments;
  133. exports.clamp = clamp;
  134. exports.createCoords = createCoords;
  135. exports.evaluate = evaluate;
  136. exports.expandPaddingObject = expandPaddingObject;
  137. exports.floor = floor;
  138. exports.getAlignment = getAlignment;
  139. exports.getAlignmentAxis = getAlignmentAxis;
  140. exports.getAlignmentSides = getAlignmentSides;
  141. exports.getAxisLength = getAxisLength;
  142. exports.getExpandedPlacements = getExpandedPlacements;
  143. exports.getOppositeAlignmentPlacement = getOppositeAlignmentPlacement;
  144. exports.getOppositeAxis = getOppositeAxis;
  145. exports.getOppositeAxisPlacements = getOppositeAxisPlacements;
  146. exports.getOppositePlacement = getOppositePlacement;
  147. exports.getPaddingObject = getPaddingObject;
  148. exports.getSide = getSide;
  149. exports.getSideAxis = getSideAxis;
  150. exports.max = max;
  151. exports.min = min;
  152. exports.placements = placements;
  153. exports.rectToClientRect = rectToClientRect;
  154. exports.round = round;
  155. exports.sides = sides;
  156. }));