@teamturing/react-kit 2.45.4 → 2.46.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { evaluate, getSide, getOppositePlacement, getExpandedPlacements, getOppositeAxisPlacements, getAlignmentSides, getSideAxis, clamp, getPaddingObject, rectToClientRect, getAlignment, getOppositeAxis, getAlignmentAxis, getAxisLength } from '../../utils/dist/floating-ui.utils.js';
1
+ import { evaluate, getSide, getSideAxis, getOppositePlacement, getExpandedPlacements, getOppositeAxisPlacements, getAlignmentSides, clamp, getPaddingObject, rectToClientRect, getAlignment, getOppositeAxis, getAlignmentAxis, getAxisLength, min } from '../../utils/dist/floating-ui.utils.js';
2
2
 
3
3
  function computeCoordsFromPlacement(_ref, placement, rtl) {
4
4
  let {
@@ -58,7 +58,7 @@ function computeCoordsFromPlacement(_ref, placement, rtl) {
58
58
 
59
59
  /**
60
60
  * Computes the `x` and `y` coordinates that will place the floating element
61
- * next to a reference element when it is given a certain positioning strategy.
61
+ * next to a given reference element.
62
62
  *
63
63
  * This export does not have any `platform` interface logic. You will need to
64
64
  * write one for the platform you are using Floating UI with.
@@ -136,7 +136,6 @@ const computePosition = async (reference, floating, config) => {
136
136
  } = computeCoordsFromPlacement(rects, statefulPlacement, rtl));
137
137
  }
138
138
  i = -1;
139
- continue;
140
139
  }
141
140
  }
142
141
  return {
@@ -186,9 +185,10 @@ async function detectOverflow(state, options) {
186
185
  strategy
187
186
  }));
188
187
  const rect = elementContext === 'floating' ? {
189
- ...rects.floating,
190
188
  x,
191
- y
189
+ y,
190
+ width: rects.floating.width,
191
+ height: rects.floating.height
192
192
  } : rects.reference;
193
193
  const offsetParent = await (platform.getOffsetParent == null ? void 0 : platform.getOffsetParent(elements.floating));
194
194
  const offsetScale = (await (platform.isElement == null ? void 0 : platform.isElement(offsetParent))) ? (await (platform.getScale == null ? void 0 : platform.getScale(offsetParent))) || {
@@ -199,6 +199,7 @@ async function detectOverflow(state, options) {
199
199
  y: 1
200
200
  };
201
201
  const elementClientRect = rectToClientRect(platform.convertOffsetParentRelativeRectToViewportRelativeRect ? await platform.convertOffsetParentRelativeRectToViewportRelativeRect({
202
+ elements,
202
203
  rect,
203
204
  offsetParent,
204
205
  strategy
@@ -211,6 +212,88 @@ async function detectOverflow(state, options) {
211
212
  };
212
213
  }
213
214
 
215
+ /**
216
+ * Provides data to position an inner element of the floating element so that it
217
+ * appears centered to the reference element.
218
+ * @see https://floating-ui.com/docs/arrow
219
+ */
220
+ const arrow = options => ({
221
+ name: 'arrow',
222
+ options,
223
+ async fn(state) {
224
+ const {
225
+ x,
226
+ y,
227
+ placement,
228
+ rects,
229
+ platform,
230
+ elements,
231
+ middlewareData
232
+ } = state;
233
+ // Since `element` is required, we don't Partial<> the type.
234
+ const {
235
+ element,
236
+ padding = 0
237
+ } = evaluate(options, state) || {};
238
+ if (element == null) {
239
+ return {};
240
+ }
241
+ const paddingObject = getPaddingObject(padding);
242
+ const coords = {
243
+ x,
244
+ y
245
+ };
246
+ const axis = getAlignmentAxis(placement);
247
+ const length = getAxisLength(axis);
248
+ const arrowDimensions = await platform.getDimensions(element);
249
+ const isYAxis = axis === 'y';
250
+ const minProp = isYAxis ? 'top' : 'left';
251
+ const maxProp = isYAxis ? 'bottom' : 'right';
252
+ const clientProp = isYAxis ? 'clientHeight' : 'clientWidth';
253
+ const endDiff = rects.reference[length] + rects.reference[axis] - coords[axis] - rects.floating[length];
254
+ const startDiff = coords[axis] - rects.reference[axis];
255
+ const arrowOffsetParent = await (platform.getOffsetParent == null ? void 0 : platform.getOffsetParent(element));
256
+ let clientSize = arrowOffsetParent ? arrowOffsetParent[clientProp] : 0;
257
+
258
+ // DOM platform can return `window` as the `offsetParent`.
259
+ if (!clientSize || !(await (platform.isElement == null ? void 0 : platform.isElement(arrowOffsetParent)))) {
260
+ clientSize = elements.floating[clientProp] || rects.floating[length];
261
+ }
262
+ const centerToReference = endDiff / 2 - startDiff / 2;
263
+
264
+ // If the padding is large enough that it causes the arrow to no longer be
265
+ // centered, modify the padding so that it is centered.
266
+ const largestPossiblePadding = clientSize / 2 - arrowDimensions[length] / 2 - 1;
267
+ const minPadding = min(paddingObject[minProp], largestPossiblePadding);
268
+ const maxPadding = min(paddingObject[maxProp], largestPossiblePadding);
269
+
270
+ // Make sure the arrow doesn't overflow the floating element if the center
271
+ // point is outside the floating element's bounds.
272
+ const min$1 = minPadding;
273
+ const max = clientSize - arrowDimensions[length] - maxPadding;
274
+ const center = clientSize / 2 - arrowDimensions[length] / 2 + centerToReference;
275
+ const offset = clamp(min$1, center, max);
276
+
277
+ // If the reference is small enough that the arrow's padding causes it to
278
+ // to point to nothing for an aligned placement, adjust the offset of the
279
+ // floating element itself. To ensure `shift()` continues to take action,
280
+ // a single reset is performed when this is true.
281
+ const shouldAddOffset = !middlewareData.arrow && getAlignment(placement) != null && center !== offset && rects.reference[length] / 2 - (center < min$1 ? minPadding : maxPadding) - arrowDimensions[length] / 2 < 0;
282
+ const alignmentOffset = shouldAddOffset ? center < min$1 ? center - min$1 : center - max : 0;
283
+ return {
284
+ [axis]: coords[axis] + alignmentOffset,
285
+ data: {
286
+ [axis]: offset,
287
+ centerOffset: center - offset - alignmentOffset,
288
+ ...(shouldAddOffset && {
289
+ alignmentOffset
290
+ })
291
+ },
292
+ reset: shouldAddOffset
293
+ };
294
+ }
295
+ });
296
+
214
297
  /**
215
298
  * Optimizes the visibility of the floating element by flipping the `placement`
216
299
  * in order to keep it in view when the preferred placement(s) will overflow the
@@ -252,10 +335,12 @@ const flip = function (options) {
252
335
  return {};
253
336
  }
254
337
  const side = getSide(placement);
338
+ const initialSideAxis = getSideAxis(initialPlacement);
255
339
  const isBasePlacement = getSide(initialPlacement) === initialPlacement;
256
340
  const rtl = await (platform.isRTL == null ? void 0 : platform.isRTL(elements.floating));
257
341
  const fallbackPlacements = specifiedFallbackPlacements || (isBasePlacement || !flipAlignment ? [getOppositePlacement(initialPlacement)] : getExpandedPlacements(initialPlacement));
258
- if (!specifiedFallbackPlacements && fallbackAxisSideDirection !== 'none') {
342
+ const hasFallbackAxisSideDirection = fallbackAxisSideDirection !== 'none';
343
+ if (!specifiedFallbackPlacements && hasFallbackAxisSideDirection) {
259
344
  fallbackPlacements.push(...getOppositeAxisPlacements(initialPlacement, flipAlignment, fallbackAxisSideDirection, rtl));
260
345
  }
261
346
  const placements = [initialPlacement, ...fallbackPlacements];
@@ -280,16 +365,22 @@ const flip = function (options) {
280
365
  const nextIndex = (((_middlewareData$flip2 = middlewareData.flip) == null ? void 0 : _middlewareData$flip2.index) || 0) + 1;
281
366
  const nextPlacement = placements[nextIndex];
282
367
  if (nextPlacement) {
283
- // Try next placement and re-run the lifecycle.
284
- return {
285
- data: {
286
- index: nextIndex,
287
- overflows: overflowsData
288
- },
289
- reset: {
290
- placement: nextPlacement
291
- }
292
- };
368
+ const ignoreCrossAxisOverflow = checkCrossAxis === 'alignment' ? initialSideAxis !== getSideAxis(nextPlacement) : false;
369
+ if (!ignoreCrossAxisOverflow ||
370
+ // We leave the current main axis only if every placement on that axis
371
+ // overflows the main axis.
372
+ overflowsData.every(d => getSideAxis(d.placement) === initialSideAxis ? d.overflows[0] > 0 : true)) {
373
+ // Try next placement and re-run the lifecycle.
374
+ return {
375
+ data: {
376
+ index: nextIndex,
377
+ overflows: overflowsData
378
+ },
379
+ reset: {
380
+ placement: nextPlacement
381
+ }
382
+ };
383
+ }
293
384
  }
294
385
 
295
386
  // First, find the candidates that fit on the mainAxis side of overflow,
@@ -301,8 +392,17 @@ const flip = function (options) {
301
392
  switch (fallbackStrategy) {
302
393
  case 'bestFit':
303
394
  {
304
- var _overflowsData$map$so;
305
- const placement = (_overflowsData$map$so = overflowsData.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$map$so[0];
395
+ var _overflowsData$filter2;
396
+ const placement = (_overflowsData$filter2 = overflowsData.filter(d => {
397
+ if (hasFallbackAxisSideDirection) {
398
+ const currentSideAxis = getSideAxis(d.placement);
399
+ return currentSideAxis === initialSideAxis ||
400
+ // Create a bias to the `y` side axis due to horizontal
401
+ // reading directions favoring greater width.
402
+ currentSideAxis === 'y';
403
+ }
404
+ return true;
405
+ }).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];
306
406
  if (placement) {
307
407
  resetPlacement = placement;
308
408
  }
@@ -326,8 +426,11 @@ const flip = function (options) {
326
426
  };
327
427
  };
328
428
 
429
+ const originSides = /*#__PURE__*/new Set(['left', 'top']);
430
+
329
431
  // For type backwards-compatibility, the `OffsetOptions` type was also
330
432
  // Derivable.
433
+
331
434
  async function convertValueToCoords(state, options) {
332
435
  const {
333
436
  placement,
@@ -338,7 +441,7 @@ async function convertValueToCoords(state, options) {
338
441
  const side = getSide(placement);
339
442
  const alignment = getAlignment(placement);
340
443
  const isVertical = getSideAxis(placement) === 'y';
341
- const mainAxisMulti = ['left', 'top'].includes(side) ? -1 : 1;
444
+ const mainAxisMulti = originSides.has(side) ? -1 : 1;
342
445
  const crossAxisMulti = rtl && isVertical ? -1 : 1;
343
446
  const rawValue = evaluate(options, state);
344
447
 
@@ -352,10 +455,9 @@ async function convertValueToCoords(state, options) {
352
455
  crossAxis: 0,
353
456
  alignmentAxis: null
354
457
  } : {
355
- mainAxis: 0,
356
- crossAxis: 0,
357
- alignmentAxis: null,
358
- ...rawValue
458
+ mainAxis: rawValue.mainAxis || 0,
459
+ crossAxis: rawValue.crossAxis || 0,
460
+ alignmentAxis: rawValue.alignmentAxis
359
461
  };
360
462
  if (alignment && typeof alignmentAxis === 'number') {
361
463
  crossAxis = alignment === 'end' ? alignmentAxis * -1 : alignmentAxis;
@@ -384,15 +486,27 @@ const offset = function (options) {
384
486
  name: 'offset',
385
487
  options,
386
488
  async fn(state) {
489
+ var _middlewareData$offse, _middlewareData$arrow;
387
490
  const {
388
491
  x,
389
- y
492
+ y,
493
+ placement,
494
+ middlewareData
390
495
  } = state;
391
496
  const diffCoords = await convertValueToCoords(state, options);
497
+
498
+ // If the placement is the same and the arrow caused an alignment offset
499
+ // then we don't need to change the positioning coordinates.
500
+ if (placement === ((_middlewareData$offse = middlewareData.offset) == null ? void 0 : _middlewareData$offse.placement) && (_middlewareData$arrow = middlewareData.arrow) != null && _middlewareData$arrow.alignmentOffset) {
501
+ return {};
502
+ }
392
503
  return {
393
504
  x: x + diffCoords.x,
394
505
  y: y + diffCoords.y,
395
- data: diffCoords
506
+ data: {
507
+ ...diffCoords,
508
+ placement
509
+ }
396
510
  };
397
511
  }
398
512
  };
@@ -465,11 +579,15 @@ const shift = function (options) {
465
579
  ...limitedCoords,
466
580
  data: {
467
581
  x: limitedCoords.x - x,
468
- y: limitedCoords.y - y
582
+ y: limitedCoords.y - y,
583
+ enabled: {
584
+ [mainAxis]: checkMainAxis,
585
+ [crossAxis]: checkCrossAxis
586
+ }
469
587
  }
470
588
  };
471
589
  }
472
590
  };
473
591
  };
474
592
 
475
- export { computePosition, detectOverflow, flip, offset, rectToClientRect, shift };
593
+ export { arrow, computePosition, detectOverflow, flip, offset, rectToClientRect, shift };