@lumx/react 4.3.2-alpha.9 → 4.4.0

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