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