-
-
Notifications
You must be signed in to change notification settings - Fork 470
Expand file tree
/
Copy pathuseListNavigation.ts
More file actions
930 lines (830 loc) · 28.6 KB
/
Copy pathuseListNavigation.ts
File metadata and controls
930 lines (830 loc) · 28.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
'use client';
import * as React from 'react';
import { useAnimationFrame } from '@base-ui/utils/useAnimationFrame';
import { useIsoLayoutEffect } from '@base-ui/utils/useIsoLayoutEffect';
import { ownerDocument } from '@base-ui/utils/owner';
import { useStableCallback } from '@base-ui/utils/useStableCallback';
import { useValueAsRef } from '@base-ui/utils/useValueAsRef';
import { isHTMLElement } from '@floating-ui/utils/dom';
import { createChangeEventDetails } from '../../internals/createBaseUIEventDetails';
import { REASONS } from '../../internals/reasons';
import { useFloatingParentNodeId, useFloatingTree } from '../components/FloatingTree';
import { FloatingTreeStore } from '../components/FloatingTreeStore';
import type { ElementProps, FloatingContext, FloatingRootContext } from '../types';
import {
findNonDisabledListIndex,
getMaxListIndex,
getMinListIndex,
isIndexOutOfListBounds,
} from '../utils/composite';
import type { gridNavigation } from './gridNavigation';
import { ARROW_DOWN, ARROW_LEFT, ARROW_RIGHT, ARROW_UP } from '../utils/constants';
import {
activeElement,
contains,
getFloatingFocusElement,
getTarget,
isTypeableCombobox,
} from '../utils/element';
import { enqueueFocus } from '../utils/enqueueFocus';
import { isVirtualClick, isVirtualPointerEvent, stopEvent } from '../utils/event';
export const ESCAPE = 'Escape';
function doSwitch(
orientation: UseListNavigationProps['orientation'],
vertical: boolean,
horizontal: boolean,
) {
switch (orientation) {
case 'vertical':
return vertical;
case 'horizontal':
return horizontal;
default:
return vertical || horizontal;
}
}
function isMainOrientationKey(key: string, orientation: UseListNavigationProps['orientation']) {
const vertical = key === ARROW_UP || key === ARROW_DOWN;
const horizontal = key === ARROW_LEFT || key === ARROW_RIGHT;
return doSwitch(orientation, vertical, horizontal);
}
function isMainOrientationToEndKey(
key: string,
orientation: UseListNavigationProps['orientation'],
rtl: boolean,
) {
const vertical = key === ARROW_DOWN;
const horizontal = rtl ? key === ARROW_LEFT : key === ARROW_RIGHT;
return (
doSwitch(orientation, vertical, horizontal) || key === 'Enter' || key === ' ' || key === ''
);
}
function isCrossOrientationOpenKey(
key: string,
orientation: UseListNavigationProps['orientation'],
rtl: boolean,
) {
const vertical = rtl ? key === ARROW_LEFT : key === ARROW_RIGHT;
const horizontal = key === ARROW_DOWN;
return doSwitch(orientation, vertical, horizontal);
}
function isCrossOrientationCloseKey(
key: string,
orientation: UseListNavigationProps['orientation'],
rtl: boolean,
grid: boolean,
) {
const vertical = rtl ? key === ARROW_RIGHT : key === ARROW_LEFT;
const horizontal = key === ARROW_UP;
if (orientation === 'both' || (orientation === 'horizontal' && grid)) {
return key === ESCAPE;
}
return doSwitch(orientation, vertical, horizontal);
}
export interface UseListNavigationProps {
/**
* A ref that holds an array of list items.
* @default empty list
*/
listRef: React.RefObject<Array<HTMLElement | null>>;
/**
* The index of the currently active (focused or highlighted) item, which may
* or may not be selected.
* @default null
*/
activeIndex: number | null;
/**
* A callback that is called when the user navigates to a new active item,
* passed in a new `activeIndex`.
*/
onNavigate?:
| ((activeIndex: number | null, event: React.SyntheticEvent | undefined) => void)
| undefined;
/**
* Whether the Hook is enabled, including all internal Effects and event
* handlers.
* @default true
*/
enabled?: boolean | undefined;
/**
* The currently selected item index, which may or may not be active.
* @default null
*/
selectedIndex?: number | null | undefined;
/**
* Whether to focus the item upon opening the floating element. 'auto' infers
* what to do based on the input type (keyboard vs. pointer), while a boolean
* value will force the value.
* @default 'auto'
*/
focusItemOnOpen?: boolean | 'auto' | undefined;
/**
* Whether hovering an item synchronizes the focus.
* @default true
*/
focusItemOnHover?: boolean | undefined;
/**
* Whether pressing an arrow key on the navigation's main axis opens the
* floating element.
* @default true
*/
openOnArrowKeyDown?: boolean | undefined;
/**
* By default elements with either a `disabled` or `aria-disabled` attribute
* are skipped in the list navigation — however, this requires the items to
* be rendered.
* This prop allows you to manually specify indices which should be disabled,
* overriding the default logic.
* For Windows-style select popups, where the menu does not open when
* navigating via arrow keys, specify an empty array.
* @default undefined
*/
disabledIndices?: ReadonlyArray<number> | ((index: number) => boolean) | undefined;
/**
* Determines whether focus can escape the list, such that nothing is selected
* after navigating beyond the boundary of the list. In some
* autocomplete/combobox components, this may be desired, as screen
* readers will return to the input.
* `loopFocus` must be `true`.
* @default false
*/
allowEscape?: boolean | undefined;
/**
* Determines whether focus should loop around when navigating past the first
* or last item.
* @default false
*/
loopFocus?: boolean | undefined;
/**
* If the list is nested within another one (e.g. a nested submenu), the
* navigation semantics change.
* @default false
*/
nested?: boolean | undefined;
/**
* Allows to specify the orientation of the parent list, which is used to
* determine the direction of the navigation.
* This is useful when list navigation is used within a Composite,
* as the hook can't determine the orientation of the parent list automatically.
*/
parentOrientation?: UseListNavigationProps['orientation'] | undefined;
/**
* Whether the direction of the floating element's navigation is in RTL
* layout.
* @default false
*/
rtl?: boolean | undefined;
/**
* Whether the focus is virtual (using `aria-activedescendant`).
* Use this if you need focus to remain on the reference element
* (such as an input), but allow arrow keys to navigate list items.
* This is common in autocomplete listbox components.
* Your virtually-focused list items must have a unique `id` set on them.
* @default false
*/
virtual?: boolean | undefined;
/**
* The orientation in which navigation occurs.
* @default 'vertical'
*/
orientation?: 'vertical' | 'horizontal' | 'both' | undefined;
/**
* The id of the root component.
*/
id?: string | undefined;
/**
* Whether to clear the active index when the pointer leaves an item.
* @default true
*/
resetOnPointerLeave?: boolean | undefined;
/**
* External FloatingTree to use when the one provided by context can't be used.
*/
externalTree?: FloatingTreeStore | undefined;
/**
* Computes two-dimensional list navigation for grid-capable consumers.
*/
grid?: typeof gridNavigation | null | undefined;
}
/**
* Adds arrow key-based navigation of a list of items, either using real DOM
* focus or virtual focus.
* @see https://floating-ui.com/docs/useListNavigation
*/
export function useListNavigation(
context: FloatingRootContext | FloatingContext,
props: UseListNavigationProps,
): ElementProps {
const {
listRef,
activeIndex,
onNavigate: onNavigateProp = () => {},
enabled = true,
selectedIndex = null,
allowEscape = false,
loopFocus = false,
nested = false,
rtl = false,
virtual = false,
focusItemOnOpen = 'auto',
focusItemOnHover = true,
openOnArrowKeyDown = true,
disabledIndices = undefined,
orientation = 'vertical',
parentOrientation,
id,
resetOnPointerLeave = true,
externalTree,
grid: navigateGrid,
} = props;
const isGrid = navigateGrid != null;
if (process.env.NODE_ENV !== 'production') {
if (allowEscape) {
if (!loopFocus) {
console.warn('`useListNavigation` looping must be enabled to allow escaping.');
}
if (!virtual) {
console.warn('`useListNavigation` must be virtual to allow escaping.');
}
}
if (orientation === 'vertical' && isGrid) {
console.warn(
'In grid list navigation mode, the `orientation` should',
'be either "horizontal" or "both".',
);
}
}
const store = 'rootStore' in context ? context.rootStore : context;
const open = store.useState('open');
const floatingElement = store.useState('floatingElement');
const domReferenceElement = store.useState('domReferenceElement');
const dataRef = store.context.dataRef;
const floatingFocusElement = getFloatingFocusElement(floatingElement);
const typeableComboboxReference = isTypeableCombobox(domReferenceElement);
const floatingFocusElementRef = useValueAsRef(floatingFocusElement);
const parentId = useFloatingParentNodeId();
const tree = useFloatingTree(externalTree);
const focusItemOnOpenRef = React.useRef(focusItemOnOpen);
const indexRef = React.useRef(selectedIndex ?? -1);
const keyRef = React.useRef<null | string>(null);
const isPointerModalityRef = React.useRef(true);
const onNavigate = useStableCallback((event?: React.SyntheticEvent) => {
onNavigateProp(indexRef.current === -1 ? null : indexRef.current, event);
});
const previousMountedRef = React.useRef(!!floatingElement);
const previousOpenRef = React.useRef(open);
const forceSyncFocusRef = React.useRef(false);
const forceScrollIntoViewRef = React.useRef(false);
const cancelQueuedFocusRef = React.useRef<(() => void) | null>(null);
const disabledIndicesRef = useValueAsRef(disabledIndices);
const latestOpenRef = useValueAsRef(open);
const selectedIndexRef = useValueAsRef(selectedIndex);
const resetOnPointerLeaveRef = useValueAsRef(resetOnPointerLeave);
const focusFrame = useAnimationFrame();
const waitForListPopulatedFrame = useAnimationFrame();
const focusItem = useStableCallback(() => {
function runFocus(item: HTMLElement) {
if (virtual) {
tree?.events.emit('virtualfocus', item);
} else {
cancelQueuedFocusRef.current = enqueueFocus(item, {
sync: forceSyncFocusRef.current,
preventScroll: true,
});
}
}
const initialItem = listRef.current[indexRef.current];
const forceScrollIntoView = forceScrollIntoViewRef.current;
if (initialItem) {
runFocus(initialItem);
}
const scheduler = forceSyncFocusRef.current
? (callback: () => void) => callback()
: (callback: () => void) => focusFrame.request(callback);
scheduler(() => {
const waitedItem = listRef.current[indexRef.current] || initialItem;
if (!waitedItem) {
return;
}
if (!initialItem) {
runFocus(waitedItem);
}
const shouldScrollIntoView =
// eslint-disable-next-line @typescript-eslint/no-use-before-define
item && (forceScrollIntoView || !isPointerModalityRef.current);
if (shouldScrollIntoView) {
// JSDOM doesn't support `.scrollIntoView()` but it's widely supported
// by all browsers.
waitedItem.scrollIntoView?.({ block: 'nearest', inline: 'nearest' });
}
});
});
useIsoLayoutEffect(() => {
dataRef.current.orientation = orientation;
}, [dataRef, orientation]);
// Sync `selectedIndex` to be the `activeIndex` upon opening the floating
// element. Also, reset `activeIndex` upon closing the floating element.
useIsoLayoutEffect(() => {
if (!enabled) {
return;
}
if (open && floatingElement) {
indexRef.current = selectedIndex ?? -1;
if (focusItemOnOpenRef.current && selectedIndex != null) {
// Regardless of the pointer modality, we want to ensure the selected
// item comes into view when the floating element is opened.
forceScrollIntoViewRef.current = true;
onNavigate();
}
} else if (previousMountedRef.current) {
// Reset the active index when the list is no longer open and mounted (closing or
// unmounting). `onNavigate` is a stable callback that always forwards to the latest
// `onNavigate` prop.
indexRef.current = -1;
onNavigate();
}
}, [enabled, open, floatingElement, selectedIndex, onNavigate]);
// Sync `activeIndex` to be the focused item while the floating element is
// open.
useIsoLayoutEffect(() => {
if (!enabled) {
return;
}
if (!open) {
forceSyncFocusRef.current = false;
return;
}
if (!floatingElement) {
return;
}
if (activeIndex == null) {
forceSyncFocusRef.current = false;
if (selectedIndexRef.current != null) {
return;
}
// Reset while the floating element was open (e.g. the list changed).
if (previousMountedRef.current) {
indexRef.current = -1;
focusItem();
}
// Initial sync.
if (
(!previousOpenRef.current || !previousMountedRef.current) &&
focusItemOnOpenRef.current &&
(keyRef.current != null || (focusItemOnOpenRef.current === true && keyRef.current == null))
) {
let runs = 0;
const waitForListPopulated = () => {
if (listRef.current[0] == null) {
// Avoid letting the browser paint if possible on the first try,
// otherwise use rAF. Don't try more than twice, since something
// is wrong otherwise.
if (runs < 2) {
const scheduler = runs
? (callback: () => void) => waitForListPopulatedFrame.request(callback)
: queueMicrotask;
scheduler(waitForListPopulated);
}
runs += 1;
} else {
// Initially focus the first non-disabled item. `disabledIndices` is deliberately
// omitted here so attribute-disabled items (`disabled`/`aria-disabled`) are skipped
// on open even when the consumer passes an empty `disabledIndices` array. Passing it
// would regress that behavior (see mui/base-ui#2604).
indexRef.current =
keyRef.current == null ||
isMainOrientationToEndKey(keyRef.current, orientation, rtl) ||
nested
? getMinListIndex(listRef)
: getMaxListIndex(listRef);
keyRef.current = null;
onNavigate();
}
};
waitForListPopulated();
}
} else if (!isIndexOutOfListBounds(listRef.current, activeIndex)) {
indexRef.current = activeIndex;
focusItem();
forceScrollIntoViewRef.current = false;
}
}, [
enabled,
open,
floatingElement,
activeIndex,
selectedIndexRef,
nested,
listRef,
orientation,
rtl,
onNavigate,
focusItem,
waitForListPopulatedFrame,
]);
// Ensure the parent floating element has focus when a nested child closes
// to allow arrow key navigation to work after the pointer leaves the child.
useIsoLayoutEffect(() => {
if (!enabled || floatingElement || !tree || virtual || !previousMountedRef.current) {
return;
}
const nodes = tree.nodesRef.current;
const parent = nodes.find((node) => node.id === parentId)?.context?.elements.floating;
// `floatingElement` is null here (see the guard above), so resolve the owner document from an
// in-DOM element for realm-safety (shadow DOM/iframes): the reference element, falling back to
// the parent floating element when the reference is virtual (`domReferenceElement` is null).
const activeEl = activeElement(ownerDocument(domReferenceElement ?? parent ?? null));
const treeContainsActiveEl = nodes.some(
(node) => node.context && contains(node.context.elements.floating, activeEl),
);
if (parent && !treeContainsActiveEl && isPointerModalityRef.current) {
parent.focus({ preventScroll: true });
}
}, [enabled, floatingElement, domReferenceElement, tree, parentId, virtual]);
useIsoLayoutEffect(() => {
previousOpenRef.current = open;
previousMountedRef.current = !!floatingElement;
});
useIsoLayoutEffect(() => {
if (!open) {
keyRef.current = null;
focusItemOnOpenRef.current = focusItemOnOpen;
}
}, [open, focusItemOnOpen]);
const hasActiveIndex = activeIndex != null;
const syncCurrentTarget = useStableCallback((event: React.SyntheticEvent<any>) => {
if (!latestOpenRef.current) {
return;
}
const index = listRef.current.indexOf(event.currentTarget);
if (index !== -1 && (indexRef.current !== index || activeIndex !== index)) {
indexRef.current = index;
onNavigate(event);
}
});
const getParentOrientation = useStableCallback(() => {
return (
parentOrientation ??
(tree?.nodesRef.current.find((node) => node.id === parentId)?.context?.dataRef?.current
.orientation as UseListNavigationProps['orientation'])
);
});
const getMinEnabledIndex = useStableCallback(() => {
return getMinListIndex(listRef, disabledIndicesRef.current);
});
const commonOnKeyDown = useStableCallback((event: React.KeyboardEvent) => {
isPointerModalityRef.current = false;
forceSyncFocusRef.current = true;
// When composing a character, Chrome fires ArrowDown twice. Firefox/Safari
// don't appear to suffer from this. `event.isComposing` is avoided due to
// Safari not supporting it properly (although it's not needed in the first
// place for Safari, just avoiding any possible issues).
if (event.which === 229) {
return;
}
// If the floating element is animating out, ignore navigation. Otherwise,
// the `activeIndex` gets set to 0 despite not being open so the next time
// the user ArrowDowns, the first item won't be focused.
if (!latestOpenRef.current && event.currentTarget === floatingFocusElementRef.current) {
return;
}
if (nested && isCrossOrientationCloseKey(event.key, orientation, rtl, isGrid)) {
// If the nested list's close key is also the parent navigation key,
// let the parent navigate. Otherwise, stop propagating the event.
if (!isMainOrientationKey(event.key, getParentOrientation())) {
stopEvent(event);
}
store.setOpen(false, createChangeEventDetails(REASONS.listNavigation, event.nativeEvent));
if (isHTMLElement(domReferenceElement)) {
if (virtual) {
tree?.events.emit('virtualfocus', domReferenceElement);
} else {
domReferenceElement.focus();
}
}
return;
}
const currentIndex = indexRef.current;
const minIndex = getMinListIndex(listRef, disabledIndices);
const maxIndex = getMaxListIndex(listRef, disabledIndices);
if (!typeableComboboxReference) {
if (event.key === 'Home') {
stopEvent(event);
indexRef.current = minIndex;
onNavigate(event);
}
if (event.key === 'End') {
stopEvent(event);
indexRef.current = maxIndex;
onNavigate(event);
}
}
// Grid navigation is injected by grid-capable consumers so non-grid
// consumers (menu, select) tree-shake the grid helpers out.
if (navigateGrid != null) {
const index = navigateGrid(
event,
indexRef.current,
listRef,
orientation,
loopFocus,
rtl,
disabledIndices,
minIndex,
maxIndex,
);
if (index != null) {
indexRef.current = index;
onNavigate(event);
}
if (orientation === 'both') {
return;
}
}
if (isMainOrientationKey(event.key, orientation)) {
stopEvent(event);
// Reset the index if no item is focused.
if (
open &&
!virtual &&
activeElement(event.currentTarget.ownerDocument) === event.currentTarget
) {
indexRef.current = isMainOrientationToEndKey(event.key, orientation, rtl)
? minIndex
: maxIndex;
onNavigate(event);
return;
}
if (isMainOrientationToEndKey(event.key, orientation, rtl)) {
if (loopFocus) {
if (currentIndex >= maxIndex) {
if (allowEscape && currentIndex !== listRef.current.length) {
indexRef.current = -1;
} else {
// Give time for virtualizers to update the listRef.
forceSyncFocusRef.current = false;
indexRef.current = minIndex;
}
} else {
indexRef.current = findNonDisabledListIndex(listRef.current, {
startingIndex: currentIndex,
disabledIndices,
});
}
} else {
indexRef.current = Math.min(
maxIndex,
findNonDisabledListIndex(listRef.current, {
startingIndex: currentIndex,
disabledIndices,
}),
);
}
} else if (loopFocus) {
if (currentIndex <= minIndex) {
if (allowEscape && currentIndex !== -1) {
indexRef.current = listRef.current.length;
} else {
// Give time for virtualizers to update the listRef.
forceSyncFocusRef.current = false;
indexRef.current = maxIndex;
}
} else {
indexRef.current = findNonDisabledListIndex(listRef.current, {
startingIndex: currentIndex,
decrement: true,
disabledIndices,
});
}
} else {
indexRef.current = Math.max(
minIndex,
findNonDisabledListIndex(listRef.current, {
startingIndex: currentIndex,
decrement: true,
disabledIndices,
}),
);
}
if (isIndexOutOfListBounds(listRef.current, indexRef.current)) {
indexRef.current = -1;
}
onNavigate(event);
}
});
const item = React.useMemo(() => {
const itemProps: ElementProps['item'] = {
onFocus(event) {
forceSyncFocusRef.current = true;
syncCurrentTarget(event);
},
onClick: ({ currentTarget }) => currentTarget.focus({ preventScroll: true }), // Safari
onMouseMove(event) {
forceSyncFocusRef.current = true;
forceScrollIntoViewRef.current = false;
if (focusItemOnHover) {
syncCurrentTarget(event);
}
},
onPointerLeave(event) {
if (
!latestOpenRef.current ||
!isPointerModalityRef.current ||
event.pointerType === 'touch'
) {
return;
}
forceSyncFocusRef.current = true;
const relatedTarget = event.relatedTarget as HTMLElement | null;
if (!focusItemOnHover || listRef.current.includes(relatedTarget)) {
return;
}
if (!resetOnPointerLeaveRef.current) {
return;
}
cancelQueuedFocusRef.current?.();
cancelQueuedFocusRef.current = null;
indexRef.current = -1;
onNavigate(event);
if (!virtual) {
const floatingFocusEl = floatingFocusElementRef.current;
const activeEl = activeElement(ownerDocument(floatingFocusEl));
if (floatingFocusEl && contains(floatingFocusEl, activeEl)) {
floatingFocusEl.focus({ preventScroll: true });
}
}
},
};
return itemProps;
}, [
syncCurrentTarget,
latestOpenRef,
floatingFocusElementRef,
focusItemOnHover,
listRef,
onNavigate,
resetOnPointerLeaveRef,
virtual,
]);
const ariaActiveDescendantProp = React.useMemo(() => {
return (
virtual &&
open &&
hasActiveIndex && {
'aria-activedescendant': `${id}-${activeIndex}`,
}
);
}, [virtual, open, hasActiveIndex, id, activeIndex]);
const floating: ElementProps['floating'] = React.useMemo(() => {
return {
'aria-orientation': orientation === 'both' ? undefined : orientation,
...(!typeableComboboxReference ? ariaActiveDescendantProp : {}),
onKeyDown(event: React.KeyboardEvent) {
// Close submenu on Shift+Tab
if (event.key === 'Tab' && event.shiftKey && open && !virtual) {
// If the event originated from within a nested element (e.g., a Dialog opened from
// within the menu), don't close the menu. The nested element has its own focus
// management and should handle the Tab key.
const target = getTarget(event.nativeEvent) as Element | null;
if (target && !contains(floatingFocusElementRef.current, target)) {
return;
}
stopEvent(event);
store.setOpen(false, createChangeEventDetails(REASONS.focusOut, event.nativeEvent));
if (isHTMLElement(domReferenceElement)) {
domReferenceElement.focus();
}
return;
}
commonOnKeyDown(event);
},
onPointerMove() {
isPointerModalityRef.current = true;
},
};
}, [
ariaActiveDescendantProp,
commonOnKeyDown,
floatingFocusElementRef,
orientation,
typeableComboboxReference,
store,
open,
virtual,
domReferenceElement,
]);
const trigger: ElementProps['trigger'] = React.useMemo(() => {
function openOnNavigationKeyDown(event: React.KeyboardEvent) {
store.setOpen(
true,
createChangeEventDetails(
REASONS.listNavigation,
event.nativeEvent,
event.currentTarget as HTMLElement,
),
);
}
function checkVirtualMouse(event: React.PointerEvent) {
if (focusItemOnOpen === 'auto' && isVirtualClick(event.nativeEvent)) {
focusItemOnOpenRef.current = !virtual;
}
}
function checkVirtualPointer(event: React.PointerEvent) {
// `pointerdown` fires first, reset the state then perform the checks.
focusItemOnOpenRef.current = focusItemOnOpen;
if (focusItemOnOpen === 'auto' && isVirtualPointerEvent(event.nativeEvent)) {
focusItemOnOpenRef.current = true;
}
}
return {
onKeyDown(event) {
// non-reactive open state (to prevent re-creation of the handler)
const currentOpen = store.select('open');
isPointerModalityRef.current = false;
const isArrowKey = event.key.startsWith('Arrow');
const isParentCrossOpenKey = isCrossOrientationOpenKey(
event.key,
getParentOrientation(),
rtl,
);
const isMainKey = isMainOrientationKey(event.key, orientation);
const isNavigationKey =
(nested ? isParentCrossOpenKey : isMainKey) ||
event.key === 'Enter' ||
event.key.trim() === '';
if (virtual && currentOpen) {
return commonOnKeyDown(event);
}
// If a floating element should not open on arrow key down, avoid
// setting `activeIndex` while it's closed.
if (!currentOpen && !openOnArrowKeyDown && isArrowKey) {
return undefined;
}
if (isNavigationKey) {
const isParentMainKey = isMainOrientationKey(event.key, getParentOrientation());
keyRef.current = nested && isParentMainKey ? null : event.key;
}
if (nested) {
if (isParentCrossOpenKey) {
stopEvent(event);
if (currentOpen) {
indexRef.current = getMinEnabledIndex();
onNavigate(event);
} else {
openOnNavigationKeyDown(event);
}
}
return undefined;
}
if (isMainKey) {
if (selectedIndexRef.current != null) {
indexRef.current = selectedIndexRef.current;
}
stopEvent(event);
if (!currentOpen && openOnArrowKeyDown) {
openOnNavigationKeyDown(event);
} else {
commonOnKeyDown(event);
}
if (currentOpen) {
onNavigate(event);
}
}
return undefined;
},
onFocus(event) {
if (store.select('open') && !virtual) {
indexRef.current = -1;
onNavigate(event);
}
},
onPointerDown: checkVirtualPointer,
onPointerEnter: checkVirtualPointer,
onMouseDown: checkVirtualMouse,
onClick: checkVirtualMouse,
};
}, [
commonOnKeyDown,
focusItemOnOpen,
getMinEnabledIndex,
nested,
onNavigate,
store,
openOnArrowKeyDown,
orientation,
getParentOrientation,
rtl,
selectedIndexRef,
virtual,
]);
const reference: ElementProps['reference'] = React.useMemo(() => {
return {
...ariaActiveDescendantProp,
...trigger,
};
}, [ariaActiveDescendantProp, trigger]);
return React.useMemo(
() => (enabled ? { reference, floating, item, trigger } : {}),
[enabled, reference, floating, trigger, item],
);
}