|
| 1 | +import { MaybeRefOrGetter, onScopeDispose, toRef, watch } from 'vue'; |
| 2 | + |
| 3 | +type UseKeyboardNavigationOptions = { |
| 4 | + inputs: HTMLInputElement[]; |
| 5 | + rows: number; |
| 6 | + cols: number; |
| 7 | +}; |
| 8 | + |
| 9 | +const arrowEvents = ['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight']; |
| 10 | + |
| 11 | +export const useKeyboardNavigation = (options: MaybeRefOrGetter<UseKeyboardNavigationOptions>) => { |
| 12 | + const rOptions = toRef(options); |
| 13 | + const controllers = new Set<AbortController>(); |
| 14 | + |
| 15 | + const unbindEvents = () => { |
| 16 | + controllers.forEach((controller) => controller.abort()); |
| 17 | + controllers.clear(); |
| 18 | + }; |
| 19 | + |
| 20 | + const resolveNextIndex = (currentIndex: number, key: string, cols: number, rows: number) => { |
| 21 | + const currentRow = Math.floor(currentIndex / cols); |
| 22 | + const currentCol = currentIndex % cols; |
| 23 | + |
| 24 | + switch (key) { |
| 25 | + case 'ArrowUp': |
| 26 | + return currentRow > 0 ? (currentRow - 1) * cols + currentCol : (rows - 1) * cols + currentCol; |
| 27 | + case 'ArrowDown': |
| 28 | + return currentRow < rows - 1 ? (currentRow + 1) * cols + currentCol : currentCol; |
| 29 | + case 'ArrowLeft': |
| 30 | + return currentCol > 0 ? currentRow * cols + (currentCol - 1) : currentRow * cols + (cols - 1); |
| 31 | + case 'ArrowRight': |
| 32 | + return currentCol < cols - 1 ? currentRow * cols + (currentCol + 1) : currentRow * cols; |
| 33 | + default: |
| 34 | + return currentIndex; // Ignore other keys |
| 35 | + } |
| 36 | + }; |
| 37 | + |
| 38 | + const onKeyDown = (event: KeyboardEvent, index: number, options: UseKeyboardNavigationOptions) => { |
| 39 | + const newIndex = resolveNextIndex(index, event.key, options.cols, options.rows); |
| 40 | + |
| 41 | + if (newIndex !== index) { |
| 42 | + event.preventDefault(); |
| 43 | + options.inputs[index].blur(); |
| 44 | + options.inputs[newIndex].focus(); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + watch(rOptions, (options) => { |
| 49 | + unbindEvents(); |
| 50 | + |
| 51 | + options.inputs.forEach((input) => { |
| 52 | + const controller = new AbortController(); |
| 53 | + controllers.add(controller); |
| 54 | + |
| 55 | + input.addEventListener( |
| 56 | + 'keydown', |
| 57 | + (event) => { |
| 58 | + if (!arrowEvents.includes(event.key)) return; |
| 59 | + |
| 60 | + const sortedInputs = options.inputs.toSorted((a, b) => { |
| 61 | + const position = a.compareDocumentPosition(b); |
| 62 | + |
| 63 | + if (position & Node.DOCUMENT_POSITION_FOLLOWING) { |
| 64 | + return -1; |
| 65 | + } else if (position & Node.DOCUMENT_POSITION_PRECEDING) { |
| 66 | + return 1; |
| 67 | + } |
| 68 | + |
| 69 | + return 0; |
| 70 | + }); |
| 71 | + |
| 72 | + const index = sortedInputs.indexOf(input); |
| 73 | + |
| 74 | + if (index !== -1) { |
| 75 | + onKeyDown(event, index, { ...options, inputs: sortedInputs }); |
| 76 | + } |
| 77 | + }, |
| 78 | + { signal: controller.signal } |
| 79 | + ); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + onScopeDispose(unbindEvents); |
| 84 | +}; |
0 commit comments