|
| 1 | +import { useEventListener } from '@vueuse/core' |
| 2 | + |
| 3 | +/** |
| 4 | + * Composable for keyboard navigation through search results and package lists |
| 5 | + * |
| 6 | + * Provides arrow key navigation (ArrowUp/ArrowDown) and Enter key support |
| 7 | + * for navigating through focusable result elements. |
| 8 | + * |
| 9 | + * @param options - Configuration options |
| 10 | + * @param options.includeSuggestions - Whether to include suggestion elements (data-suggestion-index) |
| 11 | + * @param options.onArrowUpAtStart - Optional callback when ArrowUp is pressed at the first element |
| 12 | + */ |
| 13 | +export function useResultsKeyboardNavigation(options?: { |
| 14 | + includeSuggestions?: boolean |
| 15 | + onArrowUpAtStart?: () => void |
| 16 | +}) { |
| 17 | + const keyboardShortcuts = useKeyboardShortcuts() |
| 18 | + |
| 19 | + const isVisible = (el: HTMLElement) => el.getClientRects().length > 0 |
| 20 | + |
| 21 | + /** |
| 22 | + * Get all focusable result elements in DOM order |
| 23 | + */ |
| 24 | + function getFocusableElements(): HTMLElement[] { |
| 25 | + const elements: HTMLElement[] = [] |
| 26 | + |
| 27 | + // Include suggestions if enabled (used on search page) |
| 28 | + if (options?.includeSuggestions) { |
| 29 | + const suggestions = Array.from( |
| 30 | + document.querySelectorAll<HTMLElement>('[data-suggestion-index]'), |
| 31 | + ) |
| 32 | + .filter(isVisible) |
| 33 | + .sort((a, b) => { |
| 34 | + const aIdx = Number.parseInt(a.dataset.suggestionIndex ?? '0', 10) |
| 35 | + const bIdx = Number.parseInt(b.dataset.suggestionIndex ?? '0', 10) |
| 36 | + return aIdx - bIdx |
| 37 | + }) |
| 38 | + elements.push(...suggestions) |
| 39 | + } |
| 40 | + |
| 41 | + // Always include package results |
| 42 | + const packages = Array.from(document.querySelectorAll<HTMLElement>('[data-result-index]')) |
| 43 | + .filter(isVisible) |
| 44 | + .sort((a, b) => { |
| 45 | + const aIdx = Number.parseInt(a.dataset.resultIndex ?? '0', 10) |
| 46 | + const bIdx = Number.parseInt(b.dataset.resultIndex ?? '0', 10) |
| 47 | + return aIdx - bIdx |
| 48 | + }) |
| 49 | + elements.push(...packages) |
| 50 | + |
| 51 | + return elements |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Focus an element and scroll it into view if needed |
| 56 | + */ |
| 57 | + function focusElement(el: HTMLElement) { |
| 58 | + el.focus({ preventScroll: true }) |
| 59 | + |
| 60 | + // Only scroll if element is not already in viewport |
| 61 | + const rect = el.getBoundingClientRect() |
| 62 | + const isInViewport = ( |
| 63 | + rect.top >= 0 && |
| 64 | + rect.left >= 0 && |
| 65 | + rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && |
| 66 | + rect.right <= (window.innerWidth || document.documentElement.clientWidth) |
| 67 | + ) |
| 68 | + |
| 69 | + if (!isInViewport) { |
| 70 | + el.scrollIntoView({ block: 'nearest', behavior: 'smooth' }) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + function handleKeydown(e: KeyboardEvent) { |
| 75 | + // Only handle arrow keys and Enter |
| 76 | + if (!['ArrowDown', 'ArrowUp', 'Enter'].includes(e.key)) { |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + if (!keyboardShortcuts.value) { |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + const elements = getFocusableElements() |
| 85 | + const currentIndex = elements.findIndex(el => el === document.activeElement) |
| 86 | + |
| 87 | + if (e.key === 'ArrowDown') { |
| 88 | + // If there are results available, handle navigation |
| 89 | + if (elements.length > 0) { |
| 90 | + e.preventDefault() |
| 91 | + e.stopPropagation() |
| 92 | + |
| 93 | + // If no result is focused, focus the first one |
| 94 | + if (currentIndex < 0) { |
| 95 | + const firstEl = elements[0] |
| 96 | + if (firstEl) focusElement(firstEl) |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + // If a result is already focused, move to the next one |
| 101 | + const nextIndex = Math.min(currentIndex + 1, elements.length - 1) |
| 102 | + const el = elements[nextIndex] |
| 103 | + if (el) focusElement(el) |
| 104 | + } |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + if (e.key === 'ArrowUp') { |
| 109 | + // Only intercept if a result is already focused |
| 110 | + if (currentIndex >= 0) { |
| 111 | + e.preventDefault() |
| 112 | + e.stopPropagation() |
| 113 | + |
| 114 | + // At first result |
| 115 | + if (currentIndex === 0) { |
| 116 | + // Call custom callback if provided (e.g., return focus to search input) |
| 117 | + if (options?.onArrowUpAtStart) { |
| 118 | + options.onArrowUpAtStart() |
| 119 | + } |
| 120 | + return |
| 121 | + } |
| 122 | + const nextIndex = currentIndex - 1 |
| 123 | + const el = elements[nextIndex] |
| 124 | + if (el) focusElement(el) |
| 125 | + } |
| 126 | + return |
| 127 | + } |
| 128 | + |
| 129 | + if (e.key === 'Enter') { |
| 130 | + // Handle Enter on focused card - click the main link inside |
| 131 | + if (document.activeElement && elements.includes(document.activeElement as HTMLElement)) { |
| 132 | + const card = document.activeElement as HTMLElement |
| 133 | + // Find the first link inside the card and click it |
| 134 | + const link = card.querySelector('a') |
| 135 | + if (link) { |
| 136 | + e.preventDefault() |
| 137 | + e.stopPropagation() |
| 138 | + link.click() |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + // Register keyboard event listeners using useEventListener for better control |
| 145 | + // Use capture phase to intercept before other handlers |
| 146 | + useEventListener(document, 'keydown', handleKeydown, { capture: true }) |
| 147 | + |
| 148 | + return { |
| 149 | + getFocusableElements, |
| 150 | + focusElement, |
| 151 | + } |
| 152 | +} |
0 commit comments