@jobber/components 6.85.2 → 6.86.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.
@@ -230,6 +230,13 @@ function isTypeableCombobox(element) {
230
230
  return element.getAttribute('role') === 'combobox' && isTypeableElement(element);
231
231
  }
232
232
 
233
+ /**
234
+ * Custom positioning reference element.
235
+ * @see https://floating-ui.com/docs/virtual-elements
236
+ */
237
+
238
+ const floor$1 = Math.floor;
239
+
233
240
  var jsxRuntime = {exports: {}};
234
241
 
235
242
  var reactJsxRuntime_production_min = {};
@@ -4155,6 +4162,247 @@ function useEffectEvent(callback) {
4155
4162
  }, []);
4156
4163
  }
4157
4164
 
4165
+ const ARROW_UP = 'ArrowUp';
4166
+ const ARROW_DOWN = 'ArrowDown';
4167
+ const ARROW_LEFT = 'ArrowLeft';
4168
+ const ARROW_RIGHT = 'ArrowRight';
4169
+ function isDifferentRow(index, cols, prevRow) {
4170
+ return Math.floor(index / cols) !== prevRow;
4171
+ }
4172
+ function isIndexOutOfBounds(listRef, index) {
4173
+ return index < 0 || index >= listRef.current.length;
4174
+ }
4175
+ function getMinIndex(listRef, disabledIndices) {
4176
+ return findNonDisabledIndex(listRef, {
4177
+ disabledIndices
4178
+ });
4179
+ }
4180
+ function getMaxIndex(listRef, disabledIndices) {
4181
+ return findNonDisabledIndex(listRef, {
4182
+ decrement: true,
4183
+ startingIndex: listRef.current.length,
4184
+ disabledIndices
4185
+ });
4186
+ }
4187
+ function findNonDisabledIndex(listRef, _temp) {
4188
+ let {
4189
+ startingIndex = -1,
4190
+ decrement = false,
4191
+ disabledIndices,
4192
+ amount = 1
4193
+ } = _temp === void 0 ? {} : _temp;
4194
+ const list = listRef.current;
4195
+ let index = startingIndex;
4196
+ do {
4197
+ index += decrement ? -amount : amount;
4198
+ } while (index >= 0 && index <= list.length - 1 && isDisabled(list, index, disabledIndices));
4199
+ return index;
4200
+ }
4201
+ function getGridNavigatedIndex(elementsRef, _ref) {
4202
+ let {
4203
+ event,
4204
+ orientation,
4205
+ loop,
4206
+ rtl,
4207
+ cols,
4208
+ disabledIndices,
4209
+ minIndex,
4210
+ maxIndex,
4211
+ prevIndex,
4212
+ stopEvent: stop = false
4213
+ } = _ref;
4214
+ let nextIndex = prevIndex;
4215
+ if (event.key === ARROW_UP) {
4216
+ stop && stopEvent(event);
4217
+ if (prevIndex === -1) {
4218
+ nextIndex = maxIndex;
4219
+ } else {
4220
+ nextIndex = findNonDisabledIndex(elementsRef, {
4221
+ startingIndex: nextIndex,
4222
+ amount: cols,
4223
+ decrement: true,
4224
+ disabledIndices
4225
+ });
4226
+ if (loop && (prevIndex - cols < minIndex || nextIndex < 0)) {
4227
+ const col = prevIndex % cols;
4228
+ const maxCol = maxIndex % cols;
4229
+ const offset = maxIndex - (maxCol - col);
4230
+ if (maxCol === col) {
4231
+ nextIndex = maxIndex;
4232
+ } else {
4233
+ nextIndex = maxCol > col ? offset : offset - cols;
4234
+ }
4235
+ }
4236
+ }
4237
+ if (isIndexOutOfBounds(elementsRef, nextIndex)) {
4238
+ nextIndex = prevIndex;
4239
+ }
4240
+ }
4241
+ if (event.key === ARROW_DOWN) {
4242
+ stop && stopEvent(event);
4243
+ if (prevIndex === -1) {
4244
+ nextIndex = minIndex;
4245
+ } else {
4246
+ nextIndex = findNonDisabledIndex(elementsRef, {
4247
+ startingIndex: prevIndex,
4248
+ amount: cols,
4249
+ disabledIndices
4250
+ });
4251
+ if (loop && prevIndex + cols > maxIndex) {
4252
+ nextIndex = findNonDisabledIndex(elementsRef, {
4253
+ startingIndex: prevIndex % cols - cols,
4254
+ amount: cols,
4255
+ disabledIndices
4256
+ });
4257
+ }
4258
+ }
4259
+ if (isIndexOutOfBounds(elementsRef, nextIndex)) {
4260
+ nextIndex = prevIndex;
4261
+ }
4262
+ }
4263
+
4264
+ // Remains on the same row/column.
4265
+ if (orientation === 'both') {
4266
+ const prevRow = floor$1(prevIndex / cols);
4267
+ if (event.key === (rtl ? ARROW_LEFT : ARROW_RIGHT)) {
4268
+ stop && stopEvent(event);
4269
+ if (prevIndex % cols !== cols - 1) {
4270
+ nextIndex = findNonDisabledIndex(elementsRef, {
4271
+ startingIndex: prevIndex,
4272
+ disabledIndices
4273
+ });
4274
+ if (loop && isDifferentRow(nextIndex, cols, prevRow)) {
4275
+ nextIndex = findNonDisabledIndex(elementsRef, {
4276
+ startingIndex: prevIndex - prevIndex % cols - 1,
4277
+ disabledIndices
4278
+ });
4279
+ }
4280
+ } else if (loop) {
4281
+ nextIndex = findNonDisabledIndex(elementsRef, {
4282
+ startingIndex: prevIndex - prevIndex % cols - 1,
4283
+ disabledIndices
4284
+ });
4285
+ }
4286
+ if (isDifferentRow(nextIndex, cols, prevRow)) {
4287
+ nextIndex = prevIndex;
4288
+ }
4289
+ }
4290
+ if (event.key === (rtl ? ARROW_RIGHT : ARROW_LEFT)) {
4291
+ stop && stopEvent(event);
4292
+ if (prevIndex % cols !== 0) {
4293
+ nextIndex = findNonDisabledIndex(elementsRef, {
4294
+ startingIndex: prevIndex,
4295
+ decrement: true,
4296
+ disabledIndices
4297
+ });
4298
+ if (loop && isDifferentRow(nextIndex, cols, prevRow)) {
4299
+ nextIndex = findNonDisabledIndex(elementsRef, {
4300
+ startingIndex: prevIndex + (cols - prevIndex % cols),
4301
+ decrement: true,
4302
+ disabledIndices
4303
+ });
4304
+ }
4305
+ } else if (loop) {
4306
+ nextIndex = findNonDisabledIndex(elementsRef, {
4307
+ startingIndex: prevIndex + (cols - prevIndex % cols),
4308
+ decrement: true,
4309
+ disabledIndices
4310
+ });
4311
+ }
4312
+ if (isDifferentRow(nextIndex, cols, prevRow)) {
4313
+ nextIndex = prevIndex;
4314
+ }
4315
+ }
4316
+ const lastRow = floor$1(maxIndex / cols) === prevRow;
4317
+ if (isIndexOutOfBounds(elementsRef, nextIndex)) {
4318
+ if (loop && lastRow) {
4319
+ nextIndex = event.key === (rtl ? ARROW_RIGHT : ARROW_LEFT) ? maxIndex : findNonDisabledIndex(elementsRef, {
4320
+ startingIndex: prevIndex - prevIndex % cols - 1,
4321
+ disabledIndices
4322
+ });
4323
+ } else {
4324
+ nextIndex = prevIndex;
4325
+ }
4326
+ }
4327
+ }
4328
+ return nextIndex;
4329
+ }
4330
+
4331
+ /** For each cell index, gets the item index that occupies that cell */
4332
+ function buildCellMap(sizes, cols, dense) {
4333
+ const cellMap = [];
4334
+ let startIndex = 0;
4335
+ sizes.forEach((_ref2, index) => {
4336
+ let {
4337
+ width,
4338
+ height
4339
+ } = _ref2;
4340
+ if (width > cols) {
4341
+ if (process.env.NODE_ENV !== "production") {
4342
+ throw new Error("[Floating UI]: Invalid grid - item width at index " + index + " is greater than grid columns");
4343
+ }
4344
+ }
4345
+ let itemPlaced = false;
4346
+ if (dense) {
4347
+ startIndex = 0;
4348
+ }
4349
+ while (!itemPlaced) {
4350
+ const targetCells = [];
4351
+ for (let i = 0; i < width; i++) {
4352
+ for (let j = 0; j < height; j++) {
4353
+ targetCells.push(startIndex + i + j * cols);
4354
+ }
4355
+ }
4356
+ if (startIndex % cols + width <= cols && targetCells.every(cell => cellMap[cell] == null)) {
4357
+ targetCells.forEach(cell => {
4358
+ cellMap[cell] = index;
4359
+ });
4360
+ itemPlaced = true;
4361
+ } else {
4362
+ startIndex++;
4363
+ }
4364
+ }
4365
+ });
4366
+
4367
+ // convert into a non-sparse array
4368
+ return [...cellMap];
4369
+ }
4370
+
4371
+ /** Gets cell index of an item's corner or -1 when index is -1. */
4372
+ function getCellIndexOfCorner(index, sizes, cellMap, cols, corner) {
4373
+ if (index === -1) return -1;
4374
+ const firstCellIndex = cellMap.indexOf(index);
4375
+ const sizeItem = sizes[index];
4376
+ switch (corner) {
4377
+ case 'tl':
4378
+ return firstCellIndex;
4379
+ case 'tr':
4380
+ if (!sizeItem) {
4381
+ return firstCellIndex;
4382
+ }
4383
+ return firstCellIndex + sizeItem.width - 1;
4384
+ case 'bl':
4385
+ if (!sizeItem) {
4386
+ return firstCellIndex;
4387
+ }
4388
+ return firstCellIndex + (sizeItem.height - 1) * cols;
4389
+ case 'br':
4390
+ return cellMap.lastIndexOf(index);
4391
+ }
4392
+ }
4393
+
4394
+ /** Gets all cell indices that correspond to the specified indices */
4395
+ function getCellIndices(indices, cellMap) {
4396
+ return cellMap.flatMap((index, cellIndex) => indices.includes(index) ? [cellIndex] : []);
4397
+ }
4398
+ function isDisabled(list, index, disabledIndices) {
4399
+ if (disabledIndices) {
4400
+ return disabledIndices.includes(index);
4401
+ }
4402
+ const element = list[index];
4403
+ return element == null || element.hasAttribute('disabled') || element.getAttribute('aria-disabled') === 'true';
4404
+ }
4405
+
4158
4406
  var index = typeof document !== 'undefined' ? React.useLayoutEffect : React.useEffect;
4159
4407
 
4160
4408
  let serverHandoffComplete = false;
@@ -4189,6 +4437,18 @@ let devMessageSet;
4189
4437
  if (process.env.NODE_ENV !== "production") {
4190
4438
  devMessageSet = /*#__PURE__*/new Set();
4191
4439
  }
4440
+ function warn() {
4441
+ var _devMessageSet;
4442
+ for (var _len = arguments.length, messages = new Array(_len), _key = 0; _key < _len; _key++) {
4443
+ messages[_key] = arguments[_key];
4444
+ }
4445
+ const message = "Floating UI: " + messages.join(' ');
4446
+ if (!((_devMessageSet = devMessageSet) != null && _devMessageSet.has(message))) {
4447
+ var _devMessageSet2;
4448
+ (_devMessageSet2 = devMessageSet) == null || _devMessageSet2.add(message);
4449
+ console.warn(message);
4450
+ }
4451
+ }
4192
4452
  function error() {
4193
4453
  var _devMessageSet3;
4194
4454
  for (var _len2 = arguments.length, messages = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
@@ -4374,6 +4634,22 @@ function getChildren(nodes, id) {
4374
4634
  }
4375
4635
  return allChildren;
4376
4636
  }
4637
+ function getDeepestNode(nodes, id) {
4638
+ let deepestNodeId;
4639
+ let maxDepth = -1;
4640
+ function findDeepest(nodeId, depth) {
4641
+ if (depth > maxDepth) {
4642
+ deepestNodeId = nodeId;
4643
+ maxDepth = depth;
4644
+ }
4645
+ const children = getChildren(nodes, nodeId);
4646
+ children.forEach(child => {
4647
+ findDeepest(child.id, depth + 1);
4648
+ });
4649
+ }
4650
+ findDeepest(id, 0);
4651
+ return nodes.find(node => node.id === deepestNodeId);
4652
+ }
4377
4653
 
4378
4654
  // Modified to add conditional `aria-hidden` support:
4379
4655
  // https://github.com/theKashey/aria-hidden/blob/9220c8f4a4fd35f63bee5510a9f41a37264382d4/src/index.ts
@@ -5942,6 +6218,574 @@ function useInteractions(propsList) {
5942
6218
  }), [getReferenceProps, getFloatingProps, getItemProps]);
5943
6219
  }
5944
6220
 
6221
+ const ESCAPE = 'Escape';
6222
+ function doSwitch(orientation, vertical, horizontal) {
6223
+ switch (orientation) {
6224
+ case 'vertical':
6225
+ return vertical;
6226
+ case 'horizontal':
6227
+ return horizontal;
6228
+ default:
6229
+ return vertical || horizontal;
6230
+ }
6231
+ }
6232
+ function isMainOrientationKey(key, orientation) {
6233
+ const vertical = key === ARROW_UP || key === ARROW_DOWN;
6234
+ const horizontal = key === ARROW_LEFT || key === ARROW_RIGHT;
6235
+ return doSwitch(orientation, vertical, horizontal);
6236
+ }
6237
+ function isMainOrientationToEndKey(key, orientation, rtl) {
6238
+ const vertical = key === ARROW_DOWN;
6239
+ const horizontal = rtl ? key === ARROW_LEFT : key === ARROW_RIGHT;
6240
+ return doSwitch(orientation, vertical, horizontal) || key === 'Enter' || key === ' ' || key === '';
6241
+ }
6242
+ function isCrossOrientationOpenKey(key, orientation, rtl) {
6243
+ const vertical = rtl ? key === ARROW_LEFT : key === ARROW_RIGHT;
6244
+ const horizontal = key === ARROW_DOWN;
6245
+ return doSwitch(orientation, vertical, horizontal);
6246
+ }
6247
+ function isCrossOrientationCloseKey(key, orientation, rtl, cols) {
6248
+ const vertical = rtl ? key === ARROW_RIGHT : key === ARROW_LEFT;
6249
+ const horizontal = key === ARROW_UP;
6250
+ if (orientation === 'both' || orientation === 'horizontal' && cols && cols > 1) {
6251
+ return key === ESCAPE;
6252
+ }
6253
+ return doSwitch(orientation, vertical, horizontal);
6254
+ }
6255
+ /**
6256
+ * Adds arrow key-based navigation of a list of items, either using real DOM
6257
+ * focus or virtual focus.
6258
+ * @see https://floating-ui.com/docs/useListNavigation
6259
+ */
6260
+ function useListNavigation(context, props) {
6261
+ const {
6262
+ open,
6263
+ onOpenChange,
6264
+ elements
6265
+ } = context;
6266
+ const {
6267
+ listRef,
6268
+ activeIndex,
6269
+ onNavigate: unstable_onNavigate = () => {},
6270
+ enabled = true,
6271
+ selectedIndex = null,
6272
+ allowEscape = false,
6273
+ loop = false,
6274
+ nested = false,
6275
+ rtl = false,
6276
+ virtual = false,
6277
+ focusItemOnOpen = 'auto',
6278
+ focusItemOnHover = true,
6279
+ openOnArrowKeyDown = true,
6280
+ disabledIndices = undefined,
6281
+ orientation = 'vertical',
6282
+ cols = 1,
6283
+ scrollItemIntoView = true,
6284
+ virtualItemRef,
6285
+ itemSizes,
6286
+ dense = false
6287
+ } = props;
6288
+ if (process.env.NODE_ENV !== "production") {
6289
+ if (allowEscape) {
6290
+ if (!loop) {
6291
+ warn('`useListNavigation` looping must be enabled to allow escaping.');
6292
+ }
6293
+ if (!virtual) {
6294
+ warn('`useListNavigation` must be virtual to allow escaping.');
6295
+ }
6296
+ }
6297
+ if (orientation === 'vertical' && cols > 1) {
6298
+ warn('In grid list navigation mode (`cols` > 1), the `orientation` should', 'be either "horizontal" or "both".');
6299
+ }
6300
+ }
6301
+ const floatingFocusElement = getFloatingFocusElement(elements.floating);
6302
+ const floatingFocusElementRef = useLatestRef(floatingFocusElement);
6303
+ const parentId = useFloatingParentNodeId();
6304
+ const tree = useFloatingTree();
6305
+ index(() => {
6306
+ context.dataRef.current.orientation = orientation;
6307
+ }, [context, orientation]);
6308
+ const onNavigate = useEffectEvent(() => {
6309
+ unstable_onNavigate(indexRef.current === -1 ? null : indexRef.current);
6310
+ });
6311
+ const typeableComboboxReference = isTypeableCombobox(elements.domReference);
6312
+ const focusItemOnOpenRef = React__namespace.useRef(focusItemOnOpen);
6313
+ const indexRef = React__namespace.useRef(selectedIndex != null ? selectedIndex : -1);
6314
+ const keyRef = React__namespace.useRef(null);
6315
+ const isPointerModalityRef = React__namespace.useRef(true);
6316
+ const previousOnNavigateRef = React__namespace.useRef(onNavigate);
6317
+ const previousMountedRef = React__namespace.useRef(!!elements.floating);
6318
+ const previousOpenRef = React__namespace.useRef(open);
6319
+ const forceSyncFocusRef = React__namespace.useRef(false);
6320
+ const forceScrollIntoViewRef = React__namespace.useRef(false);
6321
+ const disabledIndicesRef = useLatestRef(disabledIndices);
6322
+ const latestOpenRef = useLatestRef(open);
6323
+ const scrollItemIntoViewRef = useLatestRef(scrollItemIntoView);
6324
+ const selectedIndexRef = useLatestRef(selectedIndex);
6325
+ const [activeId, setActiveId] = React__namespace.useState();
6326
+ const [virtualId, setVirtualId] = React__namespace.useState();
6327
+ const focusItem = useEffectEvent(() => {
6328
+ function runFocus(item) {
6329
+ if (virtual) {
6330
+ setActiveId(item.id);
6331
+ tree == null || tree.events.emit('virtualfocus', item);
6332
+ if (virtualItemRef) {
6333
+ virtualItemRef.current = item;
6334
+ }
6335
+ } else {
6336
+ enqueueFocus(item, {
6337
+ sync: forceSyncFocusRef.current,
6338
+ preventScroll: true
6339
+ });
6340
+ }
6341
+ }
6342
+ const initialItem = listRef.current[indexRef.current];
6343
+ if (initialItem) {
6344
+ runFocus(initialItem);
6345
+ }
6346
+ const scheduler = forceSyncFocusRef.current ? v => v() : requestAnimationFrame;
6347
+ scheduler(() => {
6348
+ const waitedItem = listRef.current[indexRef.current] || initialItem;
6349
+ if (!waitedItem) return;
6350
+ if (!initialItem) {
6351
+ runFocus(waitedItem);
6352
+ }
6353
+ const scrollIntoViewOptions = scrollItemIntoViewRef.current;
6354
+ const shouldScrollIntoView = scrollIntoViewOptions && item && (forceScrollIntoViewRef.current || !isPointerModalityRef.current);
6355
+ if (shouldScrollIntoView) {
6356
+ // JSDOM doesn't support `.scrollIntoView()` but it's widely supported
6357
+ // by all browsers.
6358
+ waitedItem.scrollIntoView == null || waitedItem.scrollIntoView(typeof scrollIntoViewOptions === 'boolean' ? {
6359
+ block: 'nearest',
6360
+ inline: 'nearest'
6361
+ } : scrollIntoViewOptions);
6362
+ }
6363
+ });
6364
+ });
6365
+
6366
+ // Sync `selectedIndex` to be the `activeIndex` upon opening the floating
6367
+ // element. Also, reset `activeIndex` upon closing the floating element.
6368
+ index(() => {
6369
+ if (!enabled) return;
6370
+ if (open && elements.floating) {
6371
+ if (focusItemOnOpenRef.current && selectedIndex != null) {
6372
+ // Regardless of the pointer modality, we want to ensure the selected
6373
+ // item comes into view when the floating element is opened.
6374
+ forceScrollIntoViewRef.current = true;
6375
+ indexRef.current = selectedIndex;
6376
+ onNavigate();
6377
+ }
6378
+ } else if (previousMountedRef.current) {
6379
+ // Since the user can specify `onNavigate` conditionally
6380
+ // (onNavigate: open ? setActiveIndex : setSelectedIndex),
6381
+ // we store and call the previous function.
6382
+ indexRef.current = -1;
6383
+ previousOnNavigateRef.current();
6384
+ }
6385
+ }, [enabled, open, elements.floating, selectedIndex, onNavigate]);
6386
+
6387
+ // Sync `activeIndex` to be the focused item while the floating element is
6388
+ // open.
6389
+ index(() => {
6390
+ if (!enabled) return;
6391
+ if (!open) return;
6392
+ if (!elements.floating) return;
6393
+ if (activeIndex == null) {
6394
+ forceSyncFocusRef.current = false;
6395
+ if (selectedIndexRef.current != null) {
6396
+ return;
6397
+ }
6398
+
6399
+ // Reset while the floating element was open (e.g. the list changed).
6400
+ if (previousMountedRef.current) {
6401
+ indexRef.current = -1;
6402
+ focusItem();
6403
+ }
6404
+
6405
+ // Initial sync.
6406
+ if ((!previousOpenRef.current || !previousMountedRef.current) && focusItemOnOpenRef.current && (keyRef.current != null || focusItemOnOpenRef.current === true && keyRef.current == null)) {
6407
+ let runs = 0;
6408
+ const waitForListPopulated = () => {
6409
+ if (listRef.current[0] == null) {
6410
+ // Avoid letting the browser paint if possible on the first try,
6411
+ // otherwise use rAF. Don't try more than twice, since something
6412
+ // is wrong otherwise.
6413
+ if (runs < 2) {
6414
+ const scheduler = runs ? requestAnimationFrame : queueMicrotask;
6415
+ scheduler(waitForListPopulated);
6416
+ }
6417
+ runs++;
6418
+ } else {
6419
+ indexRef.current = keyRef.current == null || isMainOrientationToEndKey(keyRef.current, orientation, rtl) || nested ? getMinIndex(listRef, disabledIndicesRef.current) : getMaxIndex(listRef, disabledIndicesRef.current);
6420
+ keyRef.current = null;
6421
+ onNavigate();
6422
+ }
6423
+ };
6424
+ waitForListPopulated();
6425
+ }
6426
+ } else if (!isIndexOutOfBounds(listRef, activeIndex)) {
6427
+ indexRef.current = activeIndex;
6428
+ focusItem();
6429
+ forceScrollIntoViewRef.current = false;
6430
+ }
6431
+ }, [enabled, open, elements.floating, activeIndex, selectedIndexRef, nested, listRef, orientation, rtl, onNavigate, focusItem, disabledIndicesRef]);
6432
+
6433
+ // Ensure the parent floating element has focus when a nested child closes
6434
+ // to allow arrow key navigation to work after the pointer leaves the child.
6435
+ index(() => {
6436
+ var _nodes$find;
6437
+ if (!enabled || elements.floating || !tree || virtual || !previousMountedRef.current) {
6438
+ return;
6439
+ }
6440
+ const nodes = tree.nodesRef.current;
6441
+ const parent = (_nodes$find = nodes.find(node => node.id === parentId)) == null || (_nodes$find = _nodes$find.context) == null ? void 0 : _nodes$find.elements.floating;
6442
+ const activeEl = activeElement(getDocument(elements.floating));
6443
+ const treeContainsActiveEl = nodes.some(node => node.context && contains(node.context.elements.floating, activeEl));
6444
+ if (parent && !treeContainsActiveEl && isPointerModalityRef.current) {
6445
+ parent.focus({
6446
+ preventScroll: true
6447
+ });
6448
+ }
6449
+ }, [enabled, elements.floating, tree, parentId, virtual]);
6450
+ index(() => {
6451
+ if (!enabled) return;
6452
+ if (!tree) return;
6453
+ if (!virtual) return;
6454
+ if (parentId) return;
6455
+ function handleVirtualFocus(item) {
6456
+ setVirtualId(item.id);
6457
+ if (virtualItemRef) {
6458
+ virtualItemRef.current = item;
6459
+ }
6460
+ }
6461
+ tree.events.on('virtualfocus', handleVirtualFocus);
6462
+ return () => {
6463
+ tree.events.off('virtualfocus', handleVirtualFocus);
6464
+ };
6465
+ }, [enabled, tree, virtual, parentId, virtualItemRef]);
6466
+ index(() => {
6467
+ previousOnNavigateRef.current = onNavigate;
6468
+ previousOpenRef.current = open;
6469
+ previousMountedRef.current = !!elements.floating;
6470
+ });
6471
+ index(() => {
6472
+ if (!open) {
6473
+ keyRef.current = null;
6474
+ }
6475
+ }, [open]);
6476
+ const hasActiveIndex = activeIndex != null;
6477
+ const item = React__namespace.useMemo(() => {
6478
+ function syncCurrentTarget(currentTarget) {
6479
+ if (!open) return;
6480
+ const index = listRef.current.indexOf(currentTarget);
6481
+ if (index !== -1 && indexRef.current !== index) {
6482
+ indexRef.current = index;
6483
+ onNavigate();
6484
+ }
6485
+ }
6486
+ const props = {
6487
+ onFocus(_ref) {
6488
+ let {
6489
+ currentTarget
6490
+ } = _ref;
6491
+ forceSyncFocusRef.current = true;
6492
+ syncCurrentTarget(currentTarget);
6493
+ },
6494
+ onClick: _ref2 => {
6495
+ let {
6496
+ currentTarget
6497
+ } = _ref2;
6498
+ return currentTarget.focus({
6499
+ preventScroll: true
6500
+ });
6501
+ },
6502
+ // Safari
6503
+ ...(focusItemOnHover && {
6504
+ onMouseMove(_ref3) {
6505
+ let {
6506
+ currentTarget
6507
+ } = _ref3;
6508
+ forceSyncFocusRef.current = true;
6509
+ forceScrollIntoViewRef.current = false;
6510
+ syncCurrentTarget(currentTarget);
6511
+ },
6512
+ onPointerLeave(_ref4) {
6513
+ let {
6514
+ pointerType
6515
+ } = _ref4;
6516
+ if (!isPointerModalityRef.current || pointerType === 'touch') {
6517
+ return;
6518
+ }
6519
+ forceSyncFocusRef.current = true;
6520
+ indexRef.current = -1;
6521
+ onNavigate();
6522
+ if (!virtual) {
6523
+ var _floatingFocusElement;
6524
+ (_floatingFocusElement = floatingFocusElementRef.current) == null || _floatingFocusElement.focus({
6525
+ preventScroll: true
6526
+ });
6527
+ }
6528
+ }
6529
+ })
6530
+ };
6531
+ return props;
6532
+ }, [open, floatingFocusElementRef, focusItemOnHover, listRef, onNavigate, virtual]);
6533
+ const commonOnKeyDown = useEffectEvent(event => {
6534
+ isPointerModalityRef.current = false;
6535
+ forceSyncFocusRef.current = true;
6536
+
6537
+ // When composing a character, Chrome fires ArrowDown twice. Firefox/Safari
6538
+ // don't appear to suffer from this. `event.isComposing` is avoided due to
6539
+ // Safari not supporting it properly (although it's not needed in the first
6540
+ // place for Safari, just avoiding any possible issues).
6541
+ if (event.which === 229) {
6542
+ return;
6543
+ }
6544
+
6545
+ // If the floating element is animating out, ignore navigation. Otherwise,
6546
+ // the `activeIndex` gets set to 0 despite not being open so the next time
6547
+ // the user ArrowDowns, the first item won't be focused.
6548
+ if (!latestOpenRef.current && event.currentTarget === floatingFocusElementRef.current) {
6549
+ return;
6550
+ }
6551
+ if (nested && isCrossOrientationCloseKey(event.key, orientation, rtl, cols)) {
6552
+ stopEvent(event);
6553
+ onOpenChange(false, event.nativeEvent, 'list-navigation');
6554
+ if (isHTMLElement$1(elements.domReference)) {
6555
+ if (virtual) {
6556
+ tree == null || tree.events.emit('virtualfocus', elements.domReference);
6557
+ } else {
6558
+ elements.domReference.focus();
6559
+ }
6560
+ }
6561
+ return;
6562
+ }
6563
+ const currentIndex = indexRef.current;
6564
+ const minIndex = getMinIndex(listRef, disabledIndices);
6565
+ const maxIndex = getMaxIndex(listRef, disabledIndices);
6566
+ if (!typeableComboboxReference) {
6567
+ if (event.key === 'Home') {
6568
+ stopEvent(event);
6569
+ indexRef.current = minIndex;
6570
+ onNavigate();
6571
+ }
6572
+ if (event.key === 'End') {
6573
+ stopEvent(event);
6574
+ indexRef.current = maxIndex;
6575
+ onNavigate();
6576
+ }
6577
+ }
6578
+
6579
+ // Grid navigation.
6580
+ if (cols > 1) {
6581
+ const sizes = itemSizes || Array.from({
6582
+ length: listRef.current.length
6583
+ }, () => ({
6584
+ width: 1,
6585
+ height: 1
6586
+ }));
6587
+ // To calculate movements on the grid, we use hypothetical cell indices
6588
+ // as if every item was 1x1, then convert back to real indices.
6589
+ const cellMap = buildCellMap(sizes, cols, dense);
6590
+ const minGridIndex = cellMap.findIndex(index => index != null && !isDisabled(listRef.current, index, disabledIndices));
6591
+ // last enabled index
6592
+ const maxGridIndex = cellMap.reduce((foundIndex, index, cellIndex) => index != null && !isDisabled(listRef.current, index, disabledIndices) ? cellIndex : foundIndex, -1);
6593
+ const index = cellMap[getGridNavigatedIndex({
6594
+ current: cellMap.map(itemIndex => itemIndex != null ? listRef.current[itemIndex] : null)
6595
+ }, {
6596
+ event,
6597
+ orientation,
6598
+ loop,
6599
+ rtl,
6600
+ cols,
6601
+ // treat undefined (empty grid spaces) as disabled indices so we
6602
+ // don't end up in them
6603
+ disabledIndices: getCellIndices([...(disabledIndices || listRef.current.map((_, index) => isDisabled(listRef.current, index) ? index : undefined)), undefined], cellMap),
6604
+ minIndex: minGridIndex,
6605
+ maxIndex: maxGridIndex,
6606
+ prevIndex: getCellIndexOfCorner(indexRef.current > maxIndex ? minIndex : indexRef.current, sizes, cellMap, cols,
6607
+ // use a corner matching the edge closest to the direction
6608
+ // we're moving in so we don't end up in the same item. Prefer
6609
+ // top/left over bottom/right.
6610
+ event.key === ARROW_DOWN ? 'bl' : event.key === (rtl ? ARROW_LEFT : ARROW_RIGHT) ? 'tr' : 'tl'),
6611
+ stopEvent: true
6612
+ })];
6613
+ if (index != null) {
6614
+ indexRef.current = index;
6615
+ onNavigate();
6616
+ }
6617
+ if (orientation === 'both') {
6618
+ return;
6619
+ }
6620
+ }
6621
+ if (isMainOrientationKey(event.key, orientation)) {
6622
+ stopEvent(event);
6623
+
6624
+ // Reset the index if no item is focused.
6625
+ if (open && !virtual && activeElement(event.currentTarget.ownerDocument) === event.currentTarget) {
6626
+ indexRef.current = isMainOrientationToEndKey(event.key, orientation, rtl) ? minIndex : maxIndex;
6627
+ onNavigate();
6628
+ return;
6629
+ }
6630
+ if (isMainOrientationToEndKey(event.key, orientation, rtl)) {
6631
+ if (loop) {
6632
+ indexRef.current = currentIndex >= maxIndex ? allowEscape && currentIndex !== listRef.current.length ? -1 : minIndex : findNonDisabledIndex(listRef, {
6633
+ startingIndex: currentIndex,
6634
+ disabledIndices
6635
+ });
6636
+ } else {
6637
+ indexRef.current = Math.min(maxIndex, findNonDisabledIndex(listRef, {
6638
+ startingIndex: currentIndex,
6639
+ disabledIndices
6640
+ }));
6641
+ }
6642
+ } else {
6643
+ if (loop) {
6644
+ indexRef.current = currentIndex <= minIndex ? allowEscape && currentIndex !== -1 ? listRef.current.length : maxIndex : findNonDisabledIndex(listRef, {
6645
+ startingIndex: currentIndex,
6646
+ decrement: true,
6647
+ disabledIndices
6648
+ });
6649
+ } else {
6650
+ indexRef.current = Math.max(minIndex, findNonDisabledIndex(listRef, {
6651
+ startingIndex: currentIndex,
6652
+ decrement: true,
6653
+ disabledIndices
6654
+ }));
6655
+ }
6656
+ }
6657
+ if (isIndexOutOfBounds(listRef, indexRef.current)) {
6658
+ indexRef.current = -1;
6659
+ }
6660
+ onNavigate();
6661
+ }
6662
+ });
6663
+ const ariaActiveDescendantProp = React__namespace.useMemo(() => {
6664
+ return virtual && open && hasActiveIndex && {
6665
+ 'aria-activedescendant': virtualId || activeId
6666
+ };
6667
+ }, [virtual, open, hasActiveIndex, virtualId, activeId]);
6668
+ const floating = React__namespace.useMemo(() => {
6669
+ return {
6670
+ 'aria-orientation': orientation === 'both' ? undefined : orientation,
6671
+ ...(!typeableComboboxReference ? ariaActiveDescendantProp : {}),
6672
+ onKeyDown: commonOnKeyDown,
6673
+ onPointerMove() {
6674
+ isPointerModalityRef.current = true;
6675
+ }
6676
+ };
6677
+ }, [ariaActiveDescendantProp, commonOnKeyDown, orientation, typeableComboboxReference]);
6678
+ const reference = React__namespace.useMemo(() => {
6679
+ function checkVirtualMouse(event) {
6680
+ if (focusItemOnOpen === 'auto' && isVirtualClick(event.nativeEvent)) {
6681
+ focusItemOnOpenRef.current = true;
6682
+ }
6683
+ }
6684
+ function checkVirtualPointer(event) {
6685
+ // `pointerdown` fires first, reset the state then perform the checks.
6686
+ focusItemOnOpenRef.current = focusItemOnOpen;
6687
+ if (focusItemOnOpen === 'auto' && isVirtualPointerEvent(event.nativeEvent)) {
6688
+ focusItemOnOpenRef.current = true;
6689
+ }
6690
+ }
6691
+ return {
6692
+ ...ariaActiveDescendantProp,
6693
+ onKeyDown(event) {
6694
+ var _tree$nodesRef$curren;
6695
+ isPointerModalityRef.current = false;
6696
+ const isArrowKey = event.key.startsWith('Arrow');
6697
+ const isHomeOrEndKey = ['Home', 'End'].includes(event.key);
6698
+ const isMoveKey = isArrowKey || isHomeOrEndKey;
6699
+ const parentOrientation = tree == null || (_tree$nodesRef$curren = tree.nodesRef.current.find(node => node.id === parentId)) == null || (_tree$nodesRef$curren = _tree$nodesRef$curren.context) == null || (_tree$nodesRef$curren = _tree$nodesRef$curren.dataRef) == null ? void 0 : _tree$nodesRef$curren.current.orientation;
6700
+ const isCrossOpenKey = isCrossOrientationOpenKey(event.key, orientation, rtl);
6701
+ const isCrossCloseKey = isCrossOrientationCloseKey(event.key, orientation, rtl, cols);
6702
+ const isParentCrossOpenKey = isCrossOrientationOpenKey(event.key, parentOrientation, rtl);
6703
+ const isMainKey = isMainOrientationKey(event.key, orientation);
6704
+ const isNavigationKey = (nested ? isParentCrossOpenKey : isMainKey) || event.key === 'Enter' || event.key.trim() === '';
6705
+ if (virtual && open) {
6706
+ const rootNode = tree == null ? void 0 : tree.nodesRef.current.find(node => node.parentId == null);
6707
+ const deepestNode = tree && rootNode ? getDeepestNode(tree.nodesRef.current, rootNode.id) : null;
6708
+ if (isMoveKey && deepestNode && virtualItemRef) {
6709
+ const eventObject = new KeyboardEvent('keydown', {
6710
+ key: event.key,
6711
+ bubbles: true
6712
+ });
6713
+ if (isCrossOpenKey || isCrossCloseKey) {
6714
+ var _deepestNode$context, _deepestNode$context2;
6715
+ const isCurrentTarget = ((_deepestNode$context = deepestNode.context) == null ? void 0 : _deepestNode$context.elements.domReference) === event.currentTarget;
6716
+ const dispatchItem = isCrossCloseKey && !isCurrentTarget ? (_deepestNode$context2 = deepestNode.context) == null ? void 0 : _deepestNode$context2.elements.domReference : isCrossOpenKey ? listRef.current.find(item => (item == null ? void 0 : item.id) === activeId) : null;
6717
+ if (dispatchItem) {
6718
+ stopEvent(event);
6719
+ dispatchItem.dispatchEvent(eventObject);
6720
+ setVirtualId(undefined);
6721
+ }
6722
+ }
6723
+ if ((isMainKey || isHomeOrEndKey) && deepestNode.context) {
6724
+ if (deepestNode.context.open && deepestNode.parentId && event.currentTarget !== deepestNode.context.elements.domReference) {
6725
+ var _deepestNode$context$;
6726
+ stopEvent(event);
6727
+ (_deepestNode$context$ = deepestNode.context.elements.domReference) == null || _deepestNode$context$.dispatchEvent(eventObject);
6728
+ return;
6729
+ }
6730
+ }
6731
+ }
6732
+ return commonOnKeyDown(event);
6733
+ }
6734
+ // If a floating element should not open on arrow key down, avoid
6735
+ // setting `activeIndex` while it's closed.
6736
+ if (!open && !openOnArrowKeyDown && isArrowKey) {
6737
+ return;
6738
+ }
6739
+ if (isNavigationKey) {
6740
+ const isParentMainKey = isMainOrientationKey(event.key, parentOrientation);
6741
+ keyRef.current = nested && isParentMainKey ? null : event.key;
6742
+ }
6743
+ if (nested) {
6744
+ if (isParentCrossOpenKey) {
6745
+ stopEvent(event);
6746
+ if (open) {
6747
+ indexRef.current = getMinIndex(listRef, disabledIndicesRef.current);
6748
+ onNavigate();
6749
+ } else {
6750
+ onOpenChange(true, event.nativeEvent, 'list-navigation');
6751
+ }
6752
+ }
6753
+ return;
6754
+ }
6755
+ if (isMainKey) {
6756
+ if (selectedIndex != null) {
6757
+ indexRef.current = selectedIndex;
6758
+ }
6759
+ stopEvent(event);
6760
+ if (!open && openOnArrowKeyDown) {
6761
+ onOpenChange(true, event.nativeEvent, 'list-navigation');
6762
+ } else {
6763
+ commonOnKeyDown(event);
6764
+ }
6765
+ if (open) {
6766
+ onNavigate();
6767
+ }
6768
+ }
6769
+ },
6770
+ onFocus() {
6771
+ if (open && !virtual) {
6772
+ indexRef.current = -1;
6773
+ onNavigate();
6774
+ }
6775
+ },
6776
+ onPointerDown: checkVirtualPointer,
6777
+ onPointerEnter: checkVirtualPointer,
6778
+ onMouseDown: checkVirtualMouse,
6779
+ onClick: checkVirtualMouse
6780
+ };
6781
+ }, [activeId, ariaActiveDescendantProp, cols, commonOnKeyDown, disabledIndicesRef, focusItemOnOpen, listRef, nested, onNavigate, onOpenChange, open, openOnArrowKeyDown, orientation, parentId, rtl, selectedIndex, tree, virtual, virtualItemRef]);
6782
+ return React__namespace.useMemo(() => enabled ? {
6783
+ reference,
6784
+ floating,
6785
+ item
6786
+ } : {}, [enabled, reference, floating, item]);
6787
+ }
6788
+
5945
6789
  const componentRoleToAriaRoleMap = /*#__PURE__*/new Map([['select', 'listbox'], ['combobox', 'listbox'], ['label', false]]);
5946
6790
 
5947
6791
  /**
@@ -6050,6 +6894,148 @@ function useRole(context, props) {
6050
6894
  } : {}, [enabled, reference, floating, item]);
6051
6895
  }
6052
6896
 
6897
+ // Converts a JS style key like `backgroundColor` to a CSS transition-property
6898
+ // like `background-color`.
6899
+ const camelCaseToKebabCase = str => str.replace(/[A-Z]+(?![a-z])|[A-Z]/g, ($, ofs) => (ofs ? '-' : '') + $.toLowerCase());
6900
+ function execWithArgsOrReturn(valueOrFn, args) {
6901
+ return typeof valueOrFn === 'function' ? valueOrFn(args) : valueOrFn;
6902
+ }
6903
+ function useDelayUnmount(open, durationMs) {
6904
+ const [isMounted, setIsMounted] = React__namespace.useState(open);
6905
+ if (open && !isMounted) {
6906
+ setIsMounted(true);
6907
+ }
6908
+ React__namespace.useEffect(() => {
6909
+ if (!open && isMounted) {
6910
+ const timeout = setTimeout(() => setIsMounted(false), durationMs);
6911
+ return () => clearTimeout(timeout);
6912
+ }
6913
+ }, [open, isMounted, durationMs]);
6914
+ return isMounted;
6915
+ }
6916
+ /**
6917
+ * Provides a status string to apply CSS transitions to a floating element,
6918
+ * correctly handling placement-aware transitions.
6919
+ * @see https://floating-ui.com/docs/useTransition#usetransitionstatus
6920
+ */
6921
+ function useTransitionStatus(context, props) {
6922
+ if (props === void 0) {
6923
+ props = {};
6924
+ }
6925
+ const {
6926
+ open,
6927
+ elements: {
6928
+ floating
6929
+ }
6930
+ } = context;
6931
+ const {
6932
+ duration = 250
6933
+ } = props;
6934
+ const isNumberDuration = typeof duration === 'number';
6935
+ const closeDuration = (isNumberDuration ? duration : duration.close) || 0;
6936
+ const [status, setStatus] = React__namespace.useState('unmounted');
6937
+ const isMounted = useDelayUnmount(open, closeDuration);
6938
+ if (!isMounted && status === 'close') {
6939
+ setStatus('unmounted');
6940
+ }
6941
+ index(() => {
6942
+ if (!floating) return;
6943
+ if (open) {
6944
+ setStatus('initial');
6945
+ const frame = requestAnimationFrame(() => {
6946
+ setStatus('open');
6947
+ });
6948
+ return () => {
6949
+ cancelAnimationFrame(frame);
6950
+ };
6951
+ }
6952
+ setStatus('close');
6953
+ }, [open, floating]);
6954
+ return {
6955
+ isMounted,
6956
+ status
6957
+ };
6958
+ }
6959
+ /**
6960
+ * Provides styles to apply CSS transitions to a floating element, correctly
6961
+ * handling placement-aware transitions. Wrapper around `useTransitionStatus`.
6962
+ * @see https://floating-ui.com/docs/useTransition#usetransitionstyles
6963
+ */
6964
+ function useTransitionStyles(context, props) {
6965
+ if (props === void 0) {
6966
+ props = {};
6967
+ }
6968
+ const {
6969
+ initial: unstable_initial = {
6970
+ opacity: 0
6971
+ },
6972
+ open: unstable_open,
6973
+ close: unstable_close,
6974
+ common: unstable_common,
6975
+ duration = 250
6976
+ } = props;
6977
+ const placement = context.placement;
6978
+ const side = placement.split('-')[0];
6979
+ const fnArgs = React__namespace.useMemo(() => ({
6980
+ side,
6981
+ placement
6982
+ }), [side, placement]);
6983
+ const isNumberDuration = typeof duration === 'number';
6984
+ const openDuration = (isNumberDuration ? duration : duration.open) || 0;
6985
+ const closeDuration = (isNumberDuration ? duration : duration.close) || 0;
6986
+ const [styles, setStyles] = React__namespace.useState(() => ({
6987
+ ...execWithArgsOrReturn(unstable_common, fnArgs),
6988
+ ...execWithArgsOrReturn(unstable_initial, fnArgs)
6989
+ }));
6990
+ const {
6991
+ isMounted,
6992
+ status
6993
+ } = useTransitionStatus(context, {
6994
+ duration
6995
+ });
6996
+ const initialRef = useLatestRef(unstable_initial);
6997
+ const openRef = useLatestRef(unstable_open);
6998
+ const closeRef = useLatestRef(unstable_close);
6999
+ const commonRef = useLatestRef(unstable_common);
7000
+ index(() => {
7001
+ const initialStyles = execWithArgsOrReturn(initialRef.current, fnArgs);
7002
+ const closeStyles = execWithArgsOrReturn(closeRef.current, fnArgs);
7003
+ const commonStyles = execWithArgsOrReturn(commonRef.current, fnArgs);
7004
+ const openStyles = execWithArgsOrReturn(openRef.current, fnArgs) || Object.keys(initialStyles).reduce((acc, key) => {
7005
+ acc[key] = '';
7006
+ return acc;
7007
+ }, {});
7008
+ if (status === 'initial') {
7009
+ setStyles(styles => ({
7010
+ transitionProperty: styles.transitionProperty,
7011
+ ...commonStyles,
7012
+ ...initialStyles
7013
+ }));
7014
+ }
7015
+ if (status === 'open') {
7016
+ setStyles({
7017
+ transitionProperty: Object.keys(openStyles).map(camelCaseToKebabCase).join(','),
7018
+ transitionDuration: openDuration + "ms",
7019
+ ...commonStyles,
7020
+ ...openStyles
7021
+ });
7022
+ }
7023
+ if (status === 'close') {
7024
+ const styles = closeStyles || initialStyles;
7025
+ setStyles({
7026
+ transitionProperty: Object.keys(styles).map(camelCaseToKebabCase).join(','),
7027
+ transitionDuration: closeDuration + "ms",
7028
+ ...commonStyles,
7029
+ ...styles
7030
+ });
7031
+ }
7032
+ }, [closeDuration, closeRef, initialRef, openRef, commonRef, openDuration, status, fnArgs]);
7033
+ return {
7034
+ isMounted,
7035
+ styles
7036
+ };
7037
+ }
7038
+
6053
7039
  exports.FloatingFocusManager = FloatingFocusManager;
6054
7040
  exports.FloatingNode = FloatingNode;
6055
7041
  exports.FloatingOverlay = FloatingOverlay;
@@ -6069,4 +7055,6 @@ exports.useFloating = useFloating;
6069
7055
  exports.useFloatingNodeId = useFloatingNodeId;
6070
7056
  exports.useFloatingParentNodeId = useFloatingParentNodeId;
6071
7057
  exports.useInteractions = useInteractions;
7058
+ exports.useListNavigation = useListNavigation;
6072
7059
  exports.useRole = useRole;
7060
+ exports.useTransitionStyles = useTransitionStyles;