@lumx/react 4.3.1 → 4.3.2-alpha.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.
@@ -0,0 +1,1447 @@
1
+ import React__default, { useContext, useEffect, useMemo, useRef, createContext } from 'react';
2
+ import { jsx, Fragment } from 'react/jsx-runtime';
3
+ import isEmpty from 'lodash/isEmpty';
4
+ import { createPortal } from 'react-dom';
5
+ import noop from 'lodash/noop';
6
+ import findLast from 'lodash/findLast';
7
+ import find from 'lodash/find';
8
+ import findLastIndex from 'lodash/findLastIndex';
9
+ import isNil from 'lodash/isNil';
10
+ import groupBy from 'lodash/groupBy';
11
+
12
+ const DisabledStateContext = /*#__PURE__*/React__default.createContext({
13
+ state: null
14
+ });
15
+ /**
16
+ * Disabled state provider.
17
+ * All nested LumX Design System components inherit this disabled state.
18
+ */
19
+ function DisabledStateProvider({
20
+ children,
21
+ ...value
22
+ }) {
23
+ return /*#__PURE__*/jsx(DisabledStateContext.Provider, {
24
+ value: value,
25
+ children: children
26
+ });
27
+ }
28
+
29
+ /**
30
+ * Get DisabledState context value
31
+ */
32
+ function useDisabledStateContext() {
33
+ return useContext(DisabledStateContext);
34
+ }
35
+
36
+ const EVENT_TYPES = ['mousedown', 'touchstart'];
37
+ function isClickAway(targets, refs) {
38
+ // The targets elements are not contained in any of the listed element references.
39
+ return !refs.some(ref => targets.some(target => ref?.current?.contains(target)));
40
+ }
41
+ /**
42
+ * Listen to clicks away from the given elements and callback the passed in function.
43
+ *
44
+ * Warning: If you need to detect click away on nested React portals, please use the `ClickAwayProvider` component.
45
+ */
46
+ function useClickAway({
47
+ callback,
48
+ childrenRefs
49
+ }) {
50
+ useEffect(() => {
51
+ const {
52
+ current: currentRefs
53
+ } = childrenRefs;
54
+ if (!callback || !currentRefs || isEmpty(currentRefs)) {
55
+ return undefined;
56
+ }
57
+ const listener = evt => {
58
+ const targets = [evt.composedPath?.()[0], evt.target];
59
+ if (isClickAway(targets, currentRefs)) {
60
+ callback(evt);
61
+ }
62
+ };
63
+ EVENT_TYPES.forEach(evtType => document.addEventListener(evtType, listener));
64
+ return () => {
65
+ EVENT_TYPES.forEach(evtType => document.removeEventListener(evtType, listener));
66
+ };
67
+ }, [callback, childrenRefs]);
68
+ }
69
+
70
+ const ClickAwayAncestorContext = /*#__PURE__*/createContext(null);
71
+ /**
72
+ * Component combining the `useClickAway` hook with a React context to hook into the React component tree and make sure
73
+ * we take into account both the DOM tree and the React tree to detect click away.
74
+ *
75
+ * @return the react component.
76
+ */
77
+ const ClickAwayProvider = ({
78
+ children,
79
+ callback,
80
+ childrenRefs,
81
+ parentRef
82
+ }) => {
83
+ const parentContext = useContext(ClickAwayAncestorContext);
84
+ const currentContext = useMemo(() => {
85
+ const context = {
86
+ childrenRefs: [],
87
+ /**
88
+ * Add element refs to the current context and propagate to the parent context.
89
+ */
90
+ addRefs(...newChildrenRefs) {
91
+ // Add element refs that should be considered as inside the click away context.
92
+ context.childrenRefs.push(...newChildrenRefs);
93
+ if (parentContext) {
94
+ // Also add then to the parent context
95
+ parentContext.addRefs(...newChildrenRefs);
96
+ if (parentRef) {
97
+ // The parent element is also considered as inside the parent click away context but not inside the current context
98
+ parentContext.addRefs(parentRef);
99
+ }
100
+ }
101
+ }
102
+ };
103
+ return context;
104
+ }, [parentContext, parentRef]);
105
+ useEffect(() => {
106
+ const {
107
+ current: currentRefs
108
+ } = childrenRefs;
109
+ if (!currentRefs) {
110
+ return;
111
+ }
112
+ currentContext.addRefs(...currentRefs);
113
+ }, [currentContext, childrenRefs]);
114
+ useClickAway({
115
+ callback,
116
+ childrenRefs: useRef(currentContext.childrenRefs)
117
+ });
118
+ return /*#__PURE__*/jsx(ClickAwayAncestorContext.Provider, {
119
+ value: currentContext,
120
+ children: children
121
+ });
122
+ };
123
+ ClickAwayProvider.displayName = 'ClickAwayProvider';
124
+
125
+ /**
126
+ * Portal initializing function.
127
+ * If it does not provide a container, the Portal children will render in classic React tree and not in a portal.
128
+ */
129
+
130
+ const PortalContext = /*#__PURE__*/React__default.createContext(() => ({
131
+ container: document.body
132
+ }));
133
+ /**
134
+ * Customize where <Portal> wrapped elements render (tooltip, popover, dialog, etc.)
135
+ */
136
+ const PortalProvider = PortalContext.Provider;
137
+
138
+ /**
139
+ * Render children in a portal outside the current DOM position
140
+ * (defaults to `document.body` but can be customized with the PortalContextProvider)
141
+ */
142
+ const Portal = ({
143
+ children,
144
+ enabled = true
145
+ }) => {
146
+ const init = React__default.useContext(PortalContext);
147
+ const context = React__default.useMemo(() => {
148
+ return enabled ? init() : null;
149
+ },
150
+ // Only update on 'enabled'
151
+ // eslint-disable-next-line react-hooks/exhaustive-deps
152
+ [enabled]);
153
+ React__default.useLayoutEffect(() => {
154
+ return context?.teardown;
155
+ }, [context?.teardown, enabled]);
156
+ if (!context?.container) {
157
+ return /*#__PURE__*/jsx(Fragment, {
158
+ children: children
159
+ });
160
+ }
161
+ return /*#__PURE__*/createPortal(children, context.container);
162
+ };
163
+
164
+ /**
165
+ * Create a map from the given tab stop to sort them by rowKey;
166
+ */
167
+ function createGridMap(tabStops) {
168
+ /** Group all tabStop by rows to easily access them by their row keys */
169
+ const tabStopsByRowKey = groupBy(tabStops, 'rowKey');
170
+ /**
171
+ * An array with each row key in the order set in the tabStops array.
172
+ * Each rowKey will only appear once.
173
+ */
174
+ const rowKeys = tabStops.reduce((acc, {
175
+ rowKey
176
+ }) => {
177
+ if (!isNil(rowKey) && !acc.includes(rowKey)) {
178
+ return [...acc, rowKey];
179
+ }
180
+ return acc;
181
+ }, []);
182
+ return {
183
+ tabStopsByRowKey,
184
+ rowKeys
185
+ };
186
+ }
187
+
188
+ /** Check if the given tab stop is enabled */
189
+ const tabStopIsEnabled = tabStop => !tabStop.disabled;
190
+
191
+ const LOOP_AROUND_TYPES = {
192
+ /**
193
+ * Will continue navigation to the next row or column and loop back to the start
194
+ * when the last tab stop is reached
195
+ */
196
+ nextLoop: 'next-loop',
197
+ /**
198
+ * Will continue navigation to the next row or column until
199
+ * the last tab stop is reached
200
+ */
201
+ nextEnd: 'next-end',
202
+ /**
203
+ * Will loop within the current row or column
204
+ */
205
+ inside: 'inside'
206
+ };
207
+ const CELL_SEARCH_DIRECTION = {
208
+ /** Look ahead of the given position */
209
+ asc: 'asc',
210
+ /** Look before the given position */
211
+ desc: 'desc'
212
+ };
213
+
214
+ /**
215
+ * Build a loopAround configuration to ensure both row and col behavior are set.
216
+ *
217
+ * Setting a boolean will set the following behaviors:
218
+ *
219
+ * * true => { row: 'next-loop', col: 'next-loop' }
220
+ * * false => { row: 'next-end', col: 'next-end' }
221
+ */
222
+ function buildLoopAroundObject(loopAround) {
223
+ if (typeof loopAround === 'boolean' || loopAround === undefined) {
224
+ const newLoopAround = loopAround ? {
225
+ row: LOOP_AROUND_TYPES.nextLoop,
226
+ col: LOOP_AROUND_TYPES.nextLoop
227
+ } : {
228
+ row: LOOP_AROUND_TYPES.nextEnd,
229
+ col: LOOP_AROUND_TYPES.nextEnd
230
+ };
231
+ return newLoopAround;
232
+ }
233
+ return loopAround;
234
+ }
235
+
236
+ /**
237
+ * Check that the given coordinate is a simple number
238
+ */
239
+ const isNumberCoords = coords => typeof coords === 'number';
240
+
241
+ /**
242
+ * Check that the given coordinate is a direction
243
+ */
244
+ function isDirectionCoords(coords) {
245
+ return Boolean(typeof coords !== 'number' && typeof coords?.from === 'number');
246
+ }
247
+
248
+ /**
249
+ * Search the given column of a grid map for a cell.
250
+ */
251
+ function findCellInCol(gridMap, col, rowCoords, cellSelector = tabStopIsEnabled) {
252
+ /** The rowIndex might not be strictly successive, so we need to use the actual row index keys. */
253
+ const {
254
+ rowKeys,
255
+ tabStopsByRowKey
256
+ } = gridMap;
257
+ const lastIndex = rowKeys.length - 1;
258
+ /**
259
+ * If the rowCoords.from is set at -1, it means we should search from the start/end.
260
+ */
261
+ let searchIndex = rowCoords.from;
262
+ if (searchIndex === -1) {
263
+ searchIndex = rowCoords.direction === CELL_SEARCH_DIRECTION.desc ? lastIndex : 0;
264
+ }
265
+ const searchCellFunc = rowCoords.direction === CELL_SEARCH_DIRECTION.desc ? findLast : find;
266
+ const rowKeyWithEnabledCell = searchCellFunc(rowKeys, (rowKey, index) => {
267
+ const row = tabStopsByRowKey[rowKey];
268
+ const cell = row[col];
269
+ const hasCell = Boolean(cell);
270
+ const cellRowIndex = index;
271
+
272
+ /** Check that the target row index is in the right direction of the search */
273
+ const correctRowIndex = rowCoords.direction === CELL_SEARCH_DIRECTION.desc ? cellRowIndex <= searchIndex : cellRowIndex >= searchIndex;
274
+ if (cell && correctRowIndex) {
275
+ return cellSelector ? hasCell && cellSelector(cell) : hasCell;
276
+ }
277
+ return false;
278
+ });
279
+ const row = rowKeyWithEnabledCell !== undefined ? tabStopsByRowKey[rowKeyWithEnabledCell] : undefined;
280
+ return row?.[col];
281
+ }
282
+
283
+ /**
284
+ * Search the given column of a grid map for a cell.
285
+ */
286
+ function findCellInRow(gridMap, row, colCoords, cellSelector = tabStopIsEnabled) {
287
+ const {
288
+ direction,
289
+ from
290
+ } = colCoords || {};
291
+ const {
292
+ rowKeys,
293
+ tabStopsByRowKey
294
+ } = gridMap;
295
+ const rowKey = rowKeys[row];
296
+ const currentRow = tabStopsByRowKey[rowKey];
297
+ if (!currentRow) {
298
+ return undefined;
299
+ }
300
+ const searchCellFunc = direction === CELL_SEARCH_DIRECTION.desc ? findLast : find;
301
+ const cell = searchCellFunc(currentRow, cellSelector, from);
302
+ return cell;
303
+ }
304
+
305
+ /**
306
+ * Parse each column of the given gridMap to find the first cell matching the selector.
307
+ * The direction and starting point of the search can be set using the coordinates attribute.
308
+ */
309
+ function parseColsForCell(/** The gridMap to search */
310
+ gridMap, /** The coordinate to search */
311
+ {
312
+ direction = CELL_SEARCH_DIRECTION.asc,
313
+ from
314
+ }, cellSelector = tabStopIsEnabled) {
315
+ if (from === undefined) {
316
+ return undefined;
317
+ }
318
+ const {
319
+ rowKeys,
320
+ tabStopsByRowKey
321
+ } = gridMap;
322
+
323
+ /** As we cannot know for certain when to stop, we need to know which column is the last column */
324
+ const maxColIndex = rowKeys.reduce((maxLength, rowIndex) => {
325
+ const rowLength = tabStopsByRowKey[rowIndex].length;
326
+ return rowLength > maxLength ? rowLength - 1 : maxLength;
327
+ }, 0);
328
+
329
+ /** If "from" is set as -1, start from the end. */
330
+ const fromIndex = from === -1 ? maxColIndex : from || 0;
331
+ for (let index = fromIndex; direction === CELL_SEARCH_DIRECTION.desc ? index > -1 : index <= maxColIndex; direction === CELL_SEARCH_DIRECTION.desc ? index -= 1 : index += 1) {
332
+ const rowWithEnabledCed = findCellInCol(gridMap, index, {
333
+ direction,
334
+ from: direction === CELL_SEARCH_DIRECTION.desc ? -1 : 0
335
+ }, cellSelector);
336
+ if (rowWithEnabledCed) {
337
+ return rowWithEnabledCed;
338
+ }
339
+ }
340
+ return undefined;
341
+ }
342
+
343
+ /**
344
+ * Search for a cell in a gridMap
345
+ *
346
+ * This allows you to
347
+ * * Select a cell at a specific coordinate
348
+ * * Search for a cell from a specific column in any direction
349
+ * * Search for a cell from a specific row in any direction
350
+ *
351
+ * If no cell is found, returns undefined
352
+ */
353
+ function getCell(/** The gridMap object to search in. */
354
+ gridMap, /** The coordinates of the cell to select */
355
+ coords,
356
+ /**
357
+ * A selector function to select the cell.
358
+ * Selects enabled cells by default.
359
+ */
360
+ cellSelector = tabStopIsEnabled) {
361
+ const {
362
+ row,
363
+ col
364
+ } = coords || {};
365
+ const {
366
+ rowKeys,
367
+ tabStopsByRowKey
368
+ } = gridMap || {};
369
+
370
+ /** Defined row and col */
371
+ if (isNumberCoords(row) && isNumberCoords(col)) {
372
+ const rowKey = rowKeys[row];
373
+ return tabStopsByRowKey[rowKey][col];
374
+ }
375
+
376
+ /** Defined row but variable col */
377
+ if (isDirectionCoords(col) && isNumberCoords(row)) {
378
+ return findCellInRow(gridMap, row, col, cellSelector);
379
+ }
380
+ if (isDirectionCoords(row)) {
381
+ if (isDirectionCoords(col)) {
382
+ return parseColsForCell(gridMap, col, cellSelector);
383
+ }
384
+ return findCellInCol(gridMap, col, row, cellSelector);
385
+ }
386
+ return undefined;
387
+ }
388
+
389
+ function getCellCoordinates(gridMap, tabStop) {
390
+ const currentRowKey = tabStop.rowKey;
391
+ if (isNil(currentRowKey)) {
392
+ return undefined;
393
+ }
394
+ const {
395
+ rowKeys,
396
+ tabStopsByRowKey
397
+ } = gridMap;
398
+ const rowIndex = rowKeys.findIndex(rowKey => rowKey === currentRowKey);
399
+ const row = tabStopsByRowKey[currentRowKey];
400
+ const columnOffset = row.findIndex(ts => ts.id === tabStop.id);
401
+ return {
402
+ rowIndex,
403
+ row,
404
+ columnOffset
405
+ };
406
+ }
407
+
408
+ /** Check whether the list should vertically loop with the given configuration */
409
+ function shouldLoopListVertically(direction, loopAround) {
410
+ return direction === 'vertical' && loopAround?.col !== 'next-end' || direction === 'both' && loopAround?.col !== 'next-end';
411
+ }
412
+
413
+ /** Check whether the list should horizontally loop with the given configuration */
414
+ function shouldLoopListHorizontally(direction, loopAround) {
415
+ return direction === 'horizontal' && loopAround?.row !== 'next-end' || direction === 'both' && loopAround?.row !== 'next-end';
416
+ }
417
+
418
+ /**
419
+ * Get the correct pointer type from the given event.
420
+ * This is used when a tab stop is selected, to check if is has been selected using a keyboard or a pointer
421
+ * (pen / mouse / touch)
422
+ */
423
+ function getPointerTypeFromEvent(event) {
424
+ return event && 'pointerType' in event && Boolean(event.pointerType) ? 'pointer' : 'keyboard';
425
+ }
426
+
427
+ // Event keys used for keyboard navigation.
428
+ const VERTICAL_NAV_KEYS = ['ArrowUp', 'ArrowDown', 'Home', 'End'];
429
+ const HORIZONTAL_NAV_KEYS = ['ArrowLeft', 'ArrowRight', 'Home', 'End'];
430
+ const KEY_NAV_KEYS = [...HORIZONTAL_NAV_KEYS, ...VERTICAL_NAV_KEYS];
431
+ const NAV_KEYS = {
432
+ both: KEY_NAV_KEYS,
433
+ vertical: VERTICAL_NAV_KEYS,
434
+ horizontal: HORIZONTAL_NAV_KEYS
435
+ };
436
+
437
+ // Event keys union type
438
+
439
+ // Handle all navigation moves resulting in a new state.
440
+ const MOVES = {
441
+ // Move to the next item.
442
+ // The grid is flatten so the item after the last of a row will be the
443
+ // first item of the next row.
444
+ NEXT(state, _, index) {
445
+ for (let i = index + 1; i < state.tabStops.length; ++i) {
446
+ const tabStop = state.tabStops[i];
447
+ if (!tabStop.disabled) {
448
+ return {
449
+ ...state,
450
+ allowFocusing: true,
451
+ selectedId: tabStop.id
452
+ };
453
+ }
454
+ }
455
+ return state;
456
+ },
457
+ // Move to the previous item.
458
+ // The grid is flatten so the item before the first of a row will be the
459
+ // last item of the previous row.
460
+ PREVIOUS(state, _, index) {
461
+ for (let i = index - 1; i >= 0; --i) {
462
+ const tabStop = state.tabStops[i];
463
+ if (!tabStop.disabled) {
464
+ return {
465
+ ...state,
466
+ allowFocusing: true,
467
+ selectedId: tabStop.id
468
+ };
469
+ }
470
+ }
471
+ return state;
472
+ },
473
+ // Moving to the next row
474
+ // We move to the next row, and we stay in the same column.
475
+ // If we are in the last row, then we move to the first not disabled item of the next column.
476
+ NEXT_ROW(state, currentTabStop) {
477
+ const currentRowKey = currentTabStop.rowKey;
478
+ if (isNil(currentRowKey)) {
479
+ return state;
480
+ }
481
+ const gridMap = state.gridMap || createGridMap(state.tabStops);
482
+ const cellCoordinates = getCellCoordinates(gridMap, currentTabStop);
483
+ if (!cellCoordinates) {
484
+ return state;
485
+ }
486
+ const {
487
+ rowIndex,
488
+ columnOffset
489
+ } = cellCoordinates;
490
+ const nextRow = rowIndex + 1;
491
+
492
+ /** First try to get the next cell in the current column */
493
+ let tabStop = getCell(gridMap, {
494
+ row: {
495
+ from: nextRow,
496
+ direction: 'asc'
497
+ },
498
+ col: columnOffset
499
+ });
500
+
501
+ // If none were found, search for the next cell depending on the loop around parameter
502
+ if (!tabStop) {
503
+ switch (state.loopAround.col) {
504
+ /**
505
+ * If columns are configured to be looped inside,
506
+ * get the first enabled cell of the current column
507
+ */
508
+ case LOOP_AROUND_TYPES.inside:
509
+ tabStop = getCell(gridMap, {
510
+ col: columnOffset,
511
+ row: {
512
+ from: 0,
513
+ direction: 'asc'
514
+ }
515
+ });
516
+ break;
517
+ /**
518
+ * If columns are configured to be go to the next,
519
+ * search for the next enabled cell from the next column
520
+ */
521
+ case LOOP_AROUND_TYPES.nextEnd:
522
+ case LOOP_AROUND_TYPES.nextLoop:
523
+ default:
524
+ tabStop = getCell(gridMap, {
525
+ row: {
526
+ from: 0,
527
+ direction: 'asc'
528
+ },
529
+ col: {
530
+ from: columnOffset + 1,
531
+ direction: 'asc'
532
+ }
533
+ });
534
+ break;
535
+ }
536
+ }
537
+
538
+ /**
539
+ * If still none is found and the columns are configured to loop
540
+ * search starting from the start
541
+ */
542
+ if (!tabStop && state.loopAround.col === LOOP_AROUND_TYPES.nextLoop) {
543
+ tabStop = getCell(gridMap, {
544
+ row: {
545
+ from: 0,
546
+ direction: 'asc'
547
+ },
548
+ col: {
549
+ from: 0,
550
+ direction: 'asc'
551
+ }
552
+ });
553
+ }
554
+ if (tabStop) {
555
+ return {
556
+ ...state,
557
+ allowFocusing: true,
558
+ selectedId: tabStop.id,
559
+ gridMap
560
+ };
561
+ }
562
+ return {
563
+ ...state,
564
+ allowFocusing: true,
565
+ gridMap
566
+ };
567
+ },
568
+ // Moving to the previous row
569
+ // We move to the previous row, and we stay in the same column.
570
+ // If we are in the first row, then we move to the last not disabled item of the previous column.
571
+ PREVIOUS_ROW(state, currentTabStop) {
572
+ const currentRowKey = currentTabStop.rowKey;
573
+ if (isNil(currentRowKey)) {
574
+ return state;
575
+ }
576
+ const gridMap = state.gridMap || createGridMap(state.tabStops);
577
+ const cellCoordinates = getCellCoordinates(gridMap, currentTabStop);
578
+ if (!cellCoordinates) {
579
+ return state;
580
+ }
581
+ const {
582
+ rowIndex,
583
+ columnOffset
584
+ } = cellCoordinates;
585
+ const previousRow = rowIndex - 1;
586
+ let tabStop;
587
+ /** Search for the previous enabled cell in the current column */
588
+ if (previousRow >= 0) {
589
+ tabStop = getCell(gridMap, {
590
+ row: {
591
+ from: previousRow,
592
+ direction: 'desc'
593
+ },
594
+ col: columnOffset
595
+ });
596
+ }
597
+
598
+ // If none were found, search for the previous cell depending on the loop around parameter
599
+ if (!tabStop) {
600
+ switch (state.loopAround.col) {
601
+ /**
602
+ * If columns are configured to be looped inside,
603
+ * get the last enabled cell of the current column
604
+ */
605
+ case LOOP_AROUND_TYPES.inside:
606
+ tabStop = getCell(gridMap, {
607
+ col: columnOffset,
608
+ row: {
609
+ from: -1,
610
+ direction: 'desc'
611
+ }
612
+ });
613
+ break;
614
+ /**
615
+ * If columns are configured to be go to the previous,
616
+ * search for the last enabled cell from the previous column
617
+ */
618
+ case LOOP_AROUND_TYPES.nextEnd:
619
+ case LOOP_AROUND_TYPES.nextLoop:
620
+ default:
621
+ if (columnOffset - 1 >= 0) {
622
+ tabStop = getCell(gridMap, {
623
+ row: {
624
+ from: -1,
625
+ direction: 'desc'
626
+ },
627
+ col: {
628
+ from: columnOffset - 1,
629
+ direction: 'desc'
630
+ }
631
+ });
632
+ break;
633
+ }
634
+ }
635
+ }
636
+ /**
637
+ * If still none is found and the columns are configured to loop
638
+ * search starting from the end
639
+ */
640
+ if (!tabStop && state.loopAround.col === LOOP_AROUND_TYPES.nextLoop) {
641
+ tabStop = getCell(gridMap, {
642
+ row: {
643
+ from: -1,
644
+ direction: 'desc'
645
+ },
646
+ col: {
647
+ from: -1,
648
+ direction: 'desc'
649
+ }
650
+ });
651
+ }
652
+ if (tabStop) {
653
+ return {
654
+ ...state,
655
+ allowFocusing: true,
656
+ selectedId: tabStop.id,
657
+ gridMap
658
+ };
659
+ }
660
+ return {
661
+ ...state,
662
+ allowFocusing: true,
663
+ gridMap
664
+ };
665
+ },
666
+ // Moving to the very first not disabled item of the list
667
+ VERY_FIRST(state) {
668
+ // The very first not disabled item' index.
669
+ const tabStop = state.tabStops.find(tabStopIsEnabled);
670
+ if (tabStop) {
671
+ return {
672
+ ...state,
673
+ allowFocusing: true,
674
+ selectedId: tabStop.id
675
+ };
676
+ }
677
+ return state;
678
+ },
679
+ // Moving to the very last not disabled item of the list
680
+ VERY_LAST(state) {
681
+ // The very last not disabled item' index.
682
+ const tabStop = findLast(state.tabStops, tabStopIsEnabled);
683
+ if (tabStop) {
684
+ return {
685
+ ...state,
686
+ allowFocusing: true,
687
+ selectedId: tabStop.id
688
+ };
689
+ }
690
+ return state;
691
+ },
692
+ NEXT_COLUMN(state, currentTabStop, index) {
693
+ const currentRowKey = currentTabStop.rowKey;
694
+ if (isNil(currentRowKey)) {
695
+ return state;
696
+ }
697
+ const gridMap = state.gridMap || createGridMap(state.tabStops);
698
+ const cellCoordinates = getCellCoordinates(gridMap, currentTabStop);
699
+ if (!cellCoordinates) {
700
+ return state;
701
+ }
702
+ const {
703
+ rowIndex,
704
+ columnOffset
705
+ } = cellCoordinates;
706
+ // Parse the current row and look for the next enabled cell
707
+ let tabStop = getCell(gridMap, {
708
+ row: rowIndex,
709
+ col: {
710
+ from: columnOffset + 1,
711
+ direction: 'asc'
712
+ }
713
+ });
714
+
715
+ // If none were found, search for the next cell depending on the loop around parameter
716
+ if (!tabStop) {
717
+ switch (state.loopAround.row) {
718
+ /**
719
+ * If rows are configured to be looped inside,
720
+ * get the first enabled cell of the current rows
721
+ */
722
+ case LOOP_AROUND_TYPES.inside:
723
+ tabStop = getCell(gridMap, {
724
+ row: rowIndex,
725
+ col: {
726
+ from: 0,
727
+ direction: 'asc'
728
+ }
729
+ });
730
+ break;
731
+ /**
732
+ * If rows are configured to be go to the next,
733
+ * search for the next enabled cell from the next row
734
+ */
735
+ case LOOP_AROUND_TYPES.nextEnd:
736
+ case LOOP_AROUND_TYPES.nextLoop:
737
+ default:
738
+ tabStop = find(state.tabStops, tabStopIsEnabled, index + 1);
739
+ break;
740
+ }
741
+ }
742
+ /**
743
+ * If still none is found and the row are configured to loop
744
+ * search starting from the start
745
+ */
746
+ if (!tabStop && state.loopAround.row === LOOP_AROUND_TYPES.nextLoop) {
747
+ tabStop = find(state.tabStops, tabStopIsEnabled);
748
+ }
749
+ return {
750
+ ...state,
751
+ allowFocusing: true,
752
+ selectedId: tabStop?.id || state.selectedId,
753
+ gridMap
754
+ };
755
+ },
756
+ PREVIOUS_COLUMN(state, currentTabStop, index) {
757
+ const currentRowKey = currentTabStop.rowKey;
758
+ if (isNil(currentRowKey)) {
759
+ return state;
760
+ }
761
+ const gridMap = state.gridMap || createGridMap(state.tabStops);
762
+ const cellCoordinates = getCellCoordinates(gridMap, currentTabStop);
763
+ if (!cellCoordinates) {
764
+ return state;
765
+ }
766
+ const {
767
+ rowIndex,
768
+ columnOffset
769
+ } = cellCoordinates;
770
+ const previousColumn = columnOffset - 1;
771
+ let tabStop;
772
+ if (previousColumn >= 0) {
773
+ // Parse the current row and look for the next enable cell
774
+ tabStop = getCell(gridMap, {
775
+ row: rowIndex,
776
+ col: {
777
+ from: previousColumn,
778
+ direction: 'desc'
779
+ }
780
+ });
781
+ }
782
+ if (!tabStop) {
783
+ switch (state.loopAround.row) {
784
+ /**
785
+ * If rows are configured to be looped inside,
786
+ * get the last enabled cell of the current row
787
+ */
788
+ case LOOP_AROUND_TYPES.inside:
789
+ tabStop = getCell(gridMap, {
790
+ row: rowIndex,
791
+ col: {
792
+ from: -1,
793
+ direction: 'desc'
794
+ }
795
+ });
796
+ break;
797
+ /**
798
+ * If rows are configured to be go to the next,
799
+ * search for the previous enabled cell from the previous row
800
+ */
801
+ case LOOP_AROUND_TYPES.nextEnd:
802
+ case LOOP_AROUND_TYPES.nextLoop:
803
+ default:
804
+ if (index - 1 >= 0) {
805
+ tabStop = findLast(state.tabStops, tabStopIsEnabled, index - 1);
806
+ }
807
+ break;
808
+ }
809
+ }
810
+ /**
811
+ * If still none is found and the rows are configured to loop
812
+ * search starting from the end
813
+ */
814
+ if (!tabStop && state.loopAround.row === LOOP_AROUND_TYPES.nextLoop) {
815
+ tabStop = findLast(state.tabStops, tabStopIsEnabled);
816
+ }
817
+ return {
818
+ ...state,
819
+ allowFocusing: true,
820
+ selectedId: tabStop?.id || state.selectedId,
821
+ gridMap
822
+ };
823
+ },
824
+ FIRST_IN_COLUMN(state, currentTabStop) {
825
+ const currentRowKey = currentTabStop.rowKey;
826
+ if (isNil(currentRowKey)) {
827
+ return state;
828
+ }
829
+ const gridMap = state.gridMap || createGridMap(state.tabStops);
830
+ const cellCoordinates = getCellCoordinates(gridMap, currentTabStop);
831
+ if (!cellCoordinates) {
832
+ return state;
833
+ }
834
+ const {
835
+ columnOffset
836
+ } = cellCoordinates;
837
+ const tabStop = getCell(gridMap, {
838
+ col: columnOffset,
839
+ row: {
840
+ from: 0,
841
+ direction: 'asc'
842
+ }
843
+ });
844
+ return {
845
+ ...state,
846
+ allowFocusing: true,
847
+ selectedId: tabStop?.id || state.selectedId,
848
+ gridMap
849
+ };
850
+ },
851
+ LAST_IN_COLUMN(state, currentTabStop) {
852
+ const currentRowKey = currentTabStop.rowKey;
853
+ if (isNil(currentRowKey)) {
854
+ return state;
855
+ }
856
+ const gridMap = state.gridMap || createGridMap(state.tabStops);
857
+ const cellCoordinates = getCellCoordinates(gridMap, currentTabStop);
858
+ if (!cellCoordinates) {
859
+ return state;
860
+ }
861
+ const {
862
+ columnOffset
863
+ } = cellCoordinates;
864
+ const tabStop = getCell(gridMap, {
865
+ col: columnOffset,
866
+ row: {
867
+ from: -1,
868
+ direction: 'desc'
869
+ }
870
+ });
871
+ return {
872
+ ...state,
873
+ allowFocusing: true,
874
+ selectedId: tabStop?.id || state.selectedId,
875
+ gridMap
876
+ };
877
+ },
878
+ // Moving to the first item in row
879
+ FIRST_IN_ROW(state, currentTabStop) {
880
+ const currentRowKey = currentTabStop.rowKey;
881
+ if (isNil(currentRowKey)) {
882
+ return state;
883
+ }
884
+ const gridMap = state.gridMap || createGridMap(state.tabStops);
885
+ const cellCoordinates = getCellCoordinates(gridMap, currentTabStop);
886
+ if (!cellCoordinates) {
887
+ return state;
888
+ }
889
+ const {
890
+ rowIndex
891
+ } = cellCoordinates;
892
+ const tabStop = getCell(gridMap, {
893
+ row: rowIndex,
894
+ col: {
895
+ from: 0,
896
+ direction: 'asc'
897
+ }
898
+ });
899
+ return {
900
+ ...state,
901
+ allowFocusing: true,
902
+ selectedId: tabStop?.id || state.selectedId,
903
+ gridMap
904
+ };
905
+ },
906
+ // Moving to the last item in row
907
+ LAST_IN_ROW(state, currentTabStop) {
908
+ const currentRowKey = currentTabStop.rowKey;
909
+ if (isNil(currentRowKey)) {
910
+ return state;
911
+ }
912
+ const gridMap = state.gridMap || createGridMap(state.tabStops);
913
+ const cellCoordinates = getCellCoordinates(gridMap, currentTabStop);
914
+ if (!cellCoordinates) {
915
+ return state;
916
+ }
917
+ const {
918
+ rowIndex
919
+ } = cellCoordinates;
920
+ const tabStop = getCell(gridMap, {
921
+ row: rowIndex,
922
+ col: {
923
+ from: -1,
924
+ direction: 'desc'
925
+ }
926
+ });
927
+ return {
928
+ ...state,
929
+ allowFocusing: true,
930
+ selectedId: tabStop?.id || state.selectedId,
931
+ gridMap
932
+ };
933
+ }
934
+ };
935
+
936
+ /** Handle `KEY_NAV` action to update */
937
+ const KEY_NAV = (state, action) => {
938
+ const {
939
+ id = state.selectedId || state.tabStops[0]?.id,
940
+ key,
941
+ ctrlKey
942
+ } = action.payload;
943
+ const index = state.tabStops.findIndex(tabStop => tabStop.id === id);
944
+ if (index === -1) {
945
+ // tab stop not registered
946
+ return state;
947
+ }
948
+ const currentTabStop = state.tabStops[index];
949
+ if (currentTabStop.disabled) {
950
+ return state;
951
+ }
952
+ const isGrid = currentTabStop.rowKey !== null;
953
+ const isFirst = index === state.tabStops.findIndex(tabStopIsEnabled);
954
+ const isLast = index === findLastIndex(state.tabStops, tabStopIsEnabled);
955
+ // Translates the user key down event info into a navigation instruction.
956
+ let navigation = null;
957
+ // eslint-disable-next-line default-case
958
+ switch (key) {
959
+ case 'ArrowLeft':
960
+ if (isGrid) {
961
+ navigation = 'PREVIOUS_COLUMN';
962
+ } else if (state.direction === 'horizontal' || state.direction === 'both') {
963
+ navigation = shouldLoopListHorizontally(state.direction, state.loopAround) && isFirst ? 'VERY_LAST' : 'PREVIOUS';
964
+ }
965
+ break;
966
+ case 'ArrowRight':
967
+ if (isGrid) {
968
+ navigation = 'NEXT_COLUMN';
969
+ } else if (state.direction === 'horizontal' || state.direction === 'both') {
970
+ navigation = shouldLoopListHorizontally(state.direction, state.loopAround) && isLast ? 'VERY_FIRST' : 'NEXT';
971
+ }
972
+ break;
973
+ case 'ArrowUp':
974
+ if (isGrid) {
975
+ navigation = 'PREVIOUS_ROW';
976
+ } else if (state.direction === 'vertical' || state.direction === 'both') {
977
+ navigation = shouldLoopListVertically(state.direction, state.loopAround) && isFirst ? 'VERY_LAST' : 'PREVIOUS';
978
+ }
979
+ break;
980
+ case 'ArrowDown':
981
+ if (isGrid) {
982
+ navigation = 'NEXT_ROW';
983
+ } else if (state.direction === 'vertical' || state.direction === 'both') {
984
+ navigation = shouldLoopListVertically(state.direction, state.loopAround) && isLast ? 'VERY_FIRST' : 'NEXT';
985
+ }
986
+ break;
987
+ case 'Home':
988
+ if (isGrid && !ctrlKey) {
989
+ navigation = state.gridJumpToShortcutDirection === 'vertical' ? 'FIRST_IN_COLUMN' : 'FIRST_IN_ROW';
990
+ } else {
991
+ navigation = 'VERY_FIRST';
992
+ }
993
+ break;
994
+ case 'End':
995
+ if (isGrid && !ctrlKey) {
996
+ navigation = state.gridJumpToShortcutDirection === 'vertical' ? 'LAST_IN_COLUMN' : 'LAST_IN_ROW';
997
+ } else {
998
+ navigation = 'VERY_LAST';
999
+ }
1000
+ break;
1001
+ }
1002
+ if (!navigation) {
1003
+ return state;
1004
+ }
1005
+ const newState = MOVES[navigation](state, currentTabStop, index);
1006
+ return {
1007
+ ...newState,
1008
+ isUsingKeyboard: true
1009
+ };
1010
+ };
1011
+
1012
+ /** Determine the updated value for selectedId: */
1013
+ const getUpdatedSelectedId = (tabStops, currentSelectedId, defaultSelectedId = null) => {
1014
+ // Get tab stop by id
1015
+ const tabStop = currentSelectedId && tabStops.find(ts => ts.id === currentSelectedId && !ts.disabled);
1016
+ if (!tabStop) {
1017
+ // Fallback to default selected id if available, or first enabled tab stop if not
1018
+ return tabStops.find(ts => ts.id === defaultSelectedId)?.id || tabStops.find(tabStopIsEnabled)?.id || null;
1019
+ }
1020
+ return tabStop?.id || defaultSelectedId;
1021
+ };
1022
+
1023
+ /** Handle `REGISTER_TAB_STOP` action registering a new tab stop. */
1024
+ const REGISTER_TAB_STOP = (state, action) => {
1025
+ const newTabStop = action.payload;
1026
+ const newTabStopElement = newTabStop.domElementRef.current;
1027
+ if (!newTabStopElement) {
1028
+ return state;
1029
+ }
1030
+
1031
+ // Find index of tab stop that
1032
+ const indexToInsertAt = findLastIndex(state.tabStops, tabStop => {
1033
+ if (tabStop.id === newTabStop.id) {
1034
+ // tab stop already registered
1035
+ return false;
1036
+ }
1037
+ const domTabStop = tabStop.domElementRef.current;
1038
+
1039
+ // New tab stop is following the current tab stop
1040
+ return domTabStop?.compareDocumentPosition(newTabStopElement) === Node.DOCUMENT_POSITION_FOLLOWING;
1041
+ });
1042
+ const insertIndex = indexToInsertAt + 1;
1043
+
1044
+ // Insert new tab stop at position `indexToInsertAt`.
1045
+ const newTabStops = [...state.tabStops];
1046
+ newTabStops.splice(insertIndex, 0, newTabStop);
1047
+
1048
+ // Handle autofocus if needed
1049
+ let {
1050
+ selectedId,
1051
+ allowFocusing
1052
+ } = state;
1053
+ if (state.autofocus === 'first' && insertIndex === 0 || state.autofocus === 'last' && insertIndex === newTabStops.length - 1 || newTabStop.autofocus) {
1054
+ allowFocusing = true;
1055
+ selectedId = newTabStop.id;
1056
+ }
1057
+ const newSelectedId = newTabStop.id === state.defaultSelectedId && !newTabStop.disabled ? newTabStop.id : getUpdatedSelectedId(newTabStops, selectedId, state.defaultSelectedId);
1058
+ return {
1059
+ ...state,
1060
+ /**
1061
+ * If the tab currently being registered is enabled and set as default selected,
1062
+ * set as selected.
1063
+ *
1064
+ */
1065
+ selectedId: newSelectedId,
1066
+ tabStops: newTabStops,
1067
+ gridMap: null,
1068
+ allowFocusing
1069
+ };
1070
+ };
1071
+
1072
+ /** Handle `UNREGISTER_TAB_STOP` action un-registering a new tab stop. */
1073
+ const UNREGISTER_TAB_STOP = (state, action) => {
1074
+ const {
1075
+ id
1076
+ } = action.payload;
1077
+ const newTabStops = state.tabStops.filter(tabStop => tabStop.id !== id);
1078
+ if (newTabStops.length === state.tabStops.length) {
1079
+ // tab stop already unregistered
1080
+ return state;
1081
+ }
1082
+
1083
+ /** Get the previous enabled tab stop */
1084
+ const previousTabStopIndex = state.tabStops.findIndex(tabStop => tabStop.id === state.selectedId && tabStopIsEnabled(tabStop));
1085
+ const newLocal = previousTabStopIndex - 1 > -1;
1086
+ const previousTabStop = newLocal ? findLast(newTabStops, tabStopIsEnabled, previousTabStopIndex - 1) : undefined;
1087
+ return {
1088
+ ...state,
1089
+ /** Set the focus on either the previous tab stop if found or the one set as default */
1090
+ selectedId: getUpdatedSelectedId(newTabStops, state.selectedId, previousTabStop?.id || state.defaultSelectedId),
1091
+ tabStops: newTabStops,
1092
+ gridMap: null
1093
+ };
1094
+ };
1095
+
1096
+ /** Handle `UPDATE_TAB_STOP` action updating properties of a tab stop. */
1097
+ const UPDATE_TAB_STOP = (state, action) => {
1098
+ const {
1099
+ id,
1100
+ rowKey,
1101
+ disabled
1102
+ } = action.payload;
1103
+ const index = state.tabStops.findIndex(tabStop => tabStop.id === id);
1104
+ if (index === -1) {
1105
+ // tab stop not registered
1106
+ return state;
1107
+ }
1108
+ const tabStop = state.tabStops[index];
1109
+ if (tabStop.disabled === disabled && tabStop.rowKey === rowKey) {
1110
+ // Nothing to do so short-circuit.
1111
+ return state;
1112
+ }
1113
+ const newTabStop = {
1114
+ ...tabStop,
1115
+ rowKey,
1116
+ disabled
1117
+ };
1118
+ const newTabStops = [...state.tabStops];
1119
+ newTabStops.splice(index, 1, newTabStop);
1120
+ return {
1121
+ ...state,
1122
+ selectedId: getUpdatedSelectedId(newTabStops, state.selectedId, state.defaultSelectedId),
1123
+ tabStops: newTabStops,
1124
+ gridMap: null
1125
+ };
1126
+ };
1127
+
1128
+ /** Handle `SELECT_TAB_STOP` action selecting a tab stop. */
1129
+ const SELECT_TAB_STOP = (state, action) => {
1130
+ const {
1131
+ id,
1132
+ type
1133
+ } = action.payload;
1134
+ const tabStop = state.tabStops.find(ts => ts.id === id);
1135
+ if (!tabStop || tabStop.disabled) {
1136
+ return state;
1137
+ }
1138
+ return {
1139
+ ...state,
1140
+ allowFocusing: true,
1141
+ selectedId: tabStop.id,
1142
+ isUsingKeyboard: type === 'keyboard'
1143
+ };
1144
+ };
1145
+ const SET_ALLOW_FOCUSING = (state, action) => {
1146
+ return {
1147
+ ...state,
1148
+ selectedId: getUpdatedSelectedId(state.tabStops, null, state.defaultSelectedId),
1149
+ allowFocusing: action.payload.allow,
1150
+ isUsingKeyboard: Boolean(action.payload.isKeyboardNavigation)
1151
+ };
1152
+ };
1153
+
1154
+ /** Handle `RESET_SELECTED_TAB_STOP` action reseting the selected tab stop. */
1155
+ const RESET_SELECTED_TAB_STOP = state => {
1156
+ return {
1157
+ ...state,
1158
+ allowFocusing: false,
1159
+ selectedId: getUpdatedSelectedId(state.tabStops, null, state.defaultSelectedId),
1160
+ defaultSelectedId: getUpdatedSelectedId(state.tabStops, null, state.defaultSelectedId),
1161
+ isUsingKeyboard: false
1162
+ };
1163
+ };
1164
+
1165
+ const INITIAL_STATE = {
1166
+ selectedId: null,
1167
+ allowFocusing: false,
1168
+ tabStops: [],
1169
+ direction: 'horizontal',
1170
+ loopAround: buildLoopAroundObject(false),
1171
+ gridMap: null,
1172
+ defaultSelectedId: null,
1173
+ autofocus: undefined,
1174
+ isUsingKeyboard: false
1175
+ };
1176
+ const OPTIONS_UPDATED = (state, action) => {
1177
+ const {
1178
+ autofocus
1179
+ } = action.payload;
1180
+ let {
1181
+ selectedId,
1182
+ allowFocusing
1183
+ } = state;
1184
+
1185
+ // Update selectedId when updating the `autofocus` option.
1186
+ if (!state.autofocus && autofocus) {
1187
+ if (autofocus === 'first') {
1188
+ selectedId = state.tabStops.find(tabStopIsEnabled)?.id || null;
1189
+ } else if (autofocus === 'last') {
1190
+ selectedId = findLast(state.tabStops, tabStopIsEnabled)?.id || null;
1191
+ }
1192
+ allowFocusing = true;
1193
+ }
1194
+ return {
1195
+ ...state,
1196
+ ...action.payload,
1197
+ selectedId,
1198
+ allowFocusing: action.payload.allowFocusing || allowFocusing,
1199
+ loopAround: buildLoopAroundObject(action.payload.loopAround)
1200
+ };
1201
+ };
1202
+
1203
+ /** Reducers for each action type: */
1204
+ const REDUCERS = {
1205
+ REGISTER_TAB_STOP,
1206
+ UNREGISTER_TAB_STOP,
1207
+ UPDATE_TAB_STOP,
1208
+ SELECT_TAB_STOP,
1209
+ OPTIONS_UPDATED,
1210
+ KEY_NAV,
1211
+ SET_ALLOW_FOCUSING,
1212
+ RESET_SELECTED_TAB_STOP
1213
+ };
1214
+
1215
+ /** Main reducer */
1216
+ const reducer = (state, action) => {
1217
+ return REDUCERS[action.type]?.(state, action) || state;
1218
+ };
1219
+
1220
+ const MovingFocusContext = /*#__PURE__*/React__default.createContext({
1221
+ state: INITIAL_STATE,
1222
+ dispatch: noop
1223
+ });
1224
+
1225
+ /**
1226
+ * Creates a roving tabindex context.
1227
+ */
1228
+ const MovingFocusProvider = ({
1229
+ children,
1230
+ options
1231
+ }) => {
1232
+ const [state, dispatch] = React__default.useReducer(reducer, INITIAL_STATE, st => ({
1233
+ ...st,
1234
+ ...options,
1235
+ direction: options?.direction || st.direction,
1236
+ loopAround: buildLoopAroundObject(options?.loopAround),
1237
+ selectedId: options?.defaultSelectedId || INITIAL_STATE.selectedId
1238
+ }));
1239
+ const isMounted = React__default.useRef(false);
1240
+
1241
+ // Update the options whenever they change:
1242
+ React__default.useEffect(() => {
1243
+ // Skip update on mount (already up to date)
1244
+ if (!isMounted.current) {
1245
+ isMounted.current = true;
1246
+ return;
1247
+ }
1248
+ dispatch({
1249
+ type: 'OPTIONS_UPDATED',
1250
+ payload: {
1251
+ direction: options?.direction || INITIAL_STATE.direction,
1252
+ loopAround: buildLoopAroundObject(options?.loopAround || INITIAL_STATE.loopAround),
1253
+ defaultSelectedId: options?.defaultSelectedId || INITIAL_STATE.defaultSelectedId,
1254
+ autofocus: options?.autofocus || INITIAL_STATE.autofocus,
1255
+ allowFocusing: options?.allowFocusing || INITIAL_STATE.allowFocusing,
1256
+ listKey: options?.listKey || INITIAL_STATE.listKey,
1257
+ firstFocusDirection: options?.firstFocusDirection || INITIAL_STATE.firstFocusDirection,
1258
+ gridJumpToShortcutDirection: options?.gridJumpToShortcutDirection || INITIAL_STATE.gridJumpToShortcutDirection
1259
+ }
1260
+ });
1261
+ }, [isMounted, options?.allowFocusing, options?.autofocus, options?.defaultSelectedId, options?.direction, options?.loopAround, options?.listKey, options?.firstFocusDirection, options?.gridJumpToShortcutDirection]);
1262
+
1263
+ // Create a cached object to use as the context value:
1264
+ const context = React__default.useMemo(() => ({
1265
+ state,
1266
+ dispatch
1267
+ }), [state]);
1268
+ return /*#__PURE__*/jsx(MovingFocusContext.Provider, {
1269
+ value: context,
1270
+ children: children
1271
+ });
1272
+ };
1273
+
1274
+ /**
1275
+ * Hook options
1276
+ */
1277
+
1278
+ /**
1279
+ * Hook to use in tab stop element of a virtual focus (ex: options of a listbox in a combobox).
1280
+ *
1281
+ * @returns true if the current tab stop has virtual focus
1282
+ */
1283
+ const useVirtualFocus = (id, domElementRef, disabled = false, rowKey = null, autofocus = false) => {
1284
+ const isMounted = React__default.useRef(false);
1285
+ const {
1286
+ state,
1287
+ dispatch
1288
+ } = React__default.useContext(MovingFocusContext);
1289
+
1290
+ // Register the tab stop on mount and unregister it on unmount:
1291
+ React__default.useEffect(() => {
1292
+ const {
1293
+ current: domElement
1294
+ } = domElementRef;
1295
+ if (!domElement) {
1296
+ return undefined;
1297
+ }
1298
+ // Select tab stop on click
1299
+ const onClick = event => {
1300
+ dispatch({
1301
+ type: 'SELECT_TAB_STOP',
1302
+ payload: {
1303
+ id,
1304
+ type: getPointerTypeFromEvent(event)
1305
+ }
1306
+ });
1307
+ };
1308
+ domElement.addEventListener('click', onClick);
1309
+
1310
+ // Register tab stop in context
1311
+ dispatch({
1312
+ type: 'REGISTER_TAB_STOP',
1313
+ payload: {
1314
+ id,
1315
+ domElementRef,
1316
+ rowKey,
1317
+ disabled,
1318
+ autofocus
1319
+ }
1320
+ });
1321
+ return () => {
1322
+ domElement.removeEventListener('click', onClick);
1323
+ dispatch({
1324
+ type: 'UNREGISTER_TAB_STOP',
1325
+ payload: {
1326
+ id
1327
+ }
1328
+ });
1329
+ };
1330
+ },
1331
+ /**
1332
+ * Pass the list key as dependency to make tab stops
1333
+ * re-register when it changes.
1334
+ */
1335
+ // eslint-disable-next-line react-hooks/exhaustive-deps
1336
+ [state.listKey]);
1337
+
1338
+ /*
1339
+ * Update the tab stop data if `rowKey` or `disabled` change.
1340
+ * The isMounted flag is used to prevent this effect running on mount, which is benign but redundant (as the
1341
+ * REGISTER_TAB_STOP action would have just been dispatched).
1342
+ */
1343
+ React__default.useEffect(() => {
1344
+ if (isMounted.current) {
1345
+ dispatch({
1346
+ type: 'UPDATE_TAB_STOP',
1347
+ payload: {
1348
+ id,
1349
+ rowKey,
1350
+ disabled
1351
+ }
1352
+ });
1353
+ } else {
1354
+ isMounted.current = true;
1355
+ }
1356
+ },
1357
+ // eslint-disable-next-line react-hooks/exhaustive-deps
1358
+ [disabled, rowKey]);
1359
+ const isActive = id === state.selectedId;
1360
+
1361
+ // Scroll element into view when highlighted
1362
+ useEffect(() => {
1363
+ const {
1364
+ current
1365
+ } = domElementRef;
1366
+ if (isActive && current && current.scrollIntoView) {
1367
+ /**
1368
+ * In some cases, the selected item is contained in a popover
1369
+ * that won't be immediately set in the correct position.
1370
+ * Setting a small timeout before scroll the item into view
1371
+ * leaves it time to settle at the correct position.
1372
+ */
1373
+ const timeout = setTimeout(() => {
1374
+ current.scrollIntoView({
1375
+ block: 'nearest'
1376
+ });
1377
+ }, 10);
1378
+ return () => {
1379
+ clearTimeout(timeout);
1380
+ };
1381
+ }
1382
+ return undefined;
1383
+ }, [domElementRef, isActive]);
1384
+ const focused = isActive && state.allowFocusing;
1385
+
1386
+ // Determine if the current tab stop is the currently active one:
1387
+ return focused;
1388
+ };
1389
+
1390
+ /**
1391
+ * Hook to use in a virtual focus parent (ex: `aria-activedescendant` on the input of a combobox).
1392
+ * * @returns the id of the currently active tab stop (virtual focus)
1393
+ */
1394
+ const useVirtualFocusParent = ref => {
1395
+ const {
1396
+ state,
1397
+ dispatch
1398
+ } = React__default.useContext(MovingFocusContext);
1399
+ React__default.useEffect(() => {
1400
+ const {
1401
+ current: element
1402
+ } = ref;
1403
+ if (!element) {
1404
+ return undefined;
1405
+ }
1406
+ function handleKeyDown(evt) {
1407
+ const eventKey = evt.key;
1408
+ if (
1409
+ // Don't move if the current direction doesn't allow key
1410
+ !NAV_KEYS[state.direction].includes(eventKey) ||
1411
+ // Don't move if alt key is pressed
1412
+ evt.altKey ||
1413
+ // Don't move the focus if it hasn't been set yet and `firstFocusDirection` doesn't allow key
1414
+ !state.allowFocusing && state.firstFocusDirection && !NAV_KEYS[state.firstFocusDirection].includes(eventKey)) {
1415
+ return;
1416
+ }
1417
+ // If focus isn't allowed yet, simply enable it to stay on first item
1418
+ if (!state.allowFocusing && eventKey === 'ArrowDown') {
1419
+ dispatch({
1420
+ type: 'SET_ALLOW_FOCUSING',
1421
+ payload: {
1422
+ allow: true,
1423
+ isKeyboardNavigation: true
1424
+ }
1425
+ });
1426
+ } else {
1427
+ dispatch({
1428
+ type: 'KEY_NAV',
1429
+ payload: {
1430
+ key: eventKey,
1431
+ ctrlKey: evt.ctrlKey
1432
+ }
1433
+ });
1434
+ }
1435
+ evt.preventDefault();
1436
+ }
1437
+ element.addEventListener('keydown', handleKeyDown);
1438
+ return () => {
1439
+ element.removeEventListener('keydown', handleKeyDown);
1440
+ };
1441
+ }, [dispatch, ref, state.allowFocusing, state.direction, state.firstFocusDirection]);
1442
+ const focused = state.allowFocusing && state.selectedId || undefined;
1443
+ return focused;
1444
+ };
1445
+
1446
+ export { ClickAwayProvider as C, DisabledStateProvider as D, MovingFocusContext as M, NAV_KEYS as N, Portal as P, useVirtualFocusParent as a, useVirtualFocus as b, MovingFocusProvider as c, PortalProvider as d, getPointerTypeFromEvent as g, useDisabledStateContext as u };
1447
+ //# sourceMappingURL=Ui3KfDoH.js.map