-
-
Notifications
You must be signed in to change notification settings - Fork 379
fix(ui): arrow key navigation for org page #2339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tylersayshi
wants to merge
7
commits into
npmx-dev:main
Choose a base branch
from
tylersayshi:tyler-fix-org-keys
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9439b16
fix(ui): arrow key navigation for org page
tylersayshi 2e616f6
[autofix.ci] apply automated fixes
autofix-ci[bot] 59e5809
remove now unused variables that we account for in the composable
tylersayshi bf648bd
one more unused var
tylersayshi 176e126
fix tests
tylersayshi 848a1ad
one-off script wasn't meant to be committed
tylersayshi 1374d9f
fix for comment on tabIndex of BaseCard
tylersayshi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| import { useEventListener } from '@vueuse/core' | ||
|
|
||
| /** | ||
| * Composable for keyboard navigation through search results and package lists | ||
| * | ||
| * Provides arrow key navigation (ArrowUp/ArrowDown) and Enter key support | ||
| * for navigating through focusable result elements. | ||
| * | ||
| * @param options - Configuration options | ||
| * @param options.includeSuggestions - Whether to include suggestion elements (data-suggestion-index) | ||
| * @param options.onArrowUpAtStart - Optional callback when ArrowUp is pressed at the first element | ||
| */ | ||
| export function useResultsKeyboardNavigation(options?: { | ||
|
Check warning on line 13 in app/composables/useResultsKeyboardNavigation.ts
|
||
| includeSuggestions?: boolean | ||
| onArrowUpAtStart?: () => void | ||
| }) { | ||
| const keyboardShortcuts = useKeyboardShortcuts() | ||
|
|
||
| const isVisible = (el: HTMLElement) => el.getClientRects().length > 0 | ||
|
|
||
| /** | ||
| * Get all focusable result elements in DOM order | ||
| */ | ||
| function getFocusableElements(): HTMLElement[] { | ||
| const elements: HTMLElement[] = [] | ||
|
|
||
| // Include suggestions if enabled (used on search page) | ||
| if (options?.includeSuggestions) { | ||
| const suggestions = Array.from( | ||
| document.querySelectorAll<HTMLElement>('[data-suggestion-index]'), | ||
| ) | ||
| .filter(isVisible) | ||
| .sort((a, b) => { | ||
| const aIdx = Number.parseInt(a.dataset.suggestionIndex ?? '0', 10) | ||
| const bIdx = Number.parseInt(b.dataset.suggestionIndex ?? '0', 10) | ||
| return aIdx - bIdx | ||
| }) | ||
| elements.push(...suggestions) | ||
| } | ||
|
|
||
| // Always include package results | ||
| const packages = Array.from(document.querySelectorAll<HTMLElement>('[data-result-index]')) | ||
| .filter(isVisible) | ||
| .sort((a, b) => { | ||
| const aIdx = Number.parseInt(a.dataset.resultIndex ?? '0', 10) | ||
| const bIdx = Number.parseInt(b.dataset.resultIndex ?? '0', 10) | ||
| return aIdx - bIdx | ||
| }) | ||
| elements.push(...packages) | ||
|
|
||
| return elements | ||
| } | ||
|
|
||
| /** | ||
| * Focus an element and scroll it into view if needed | ||
| */ | ||
| function focusElement(el: HTMLElement) { | ||
| el.focus({ preventScroll: true }) | ||
|
|
||
| // Only scroll if element is not already in viewport | ||
| const rect = el.getBoundingClientRect() | ||
| const isInViewport = | ||
| rect.top >= 0 && | ||
| rect.left >= 0 && | ||
| rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && | ||
| rect.right <= (window.innerWidth || document.documentElement.clientWidth) | ||
|
|
||
| if (!isInViewport) { | ||
| el.scrollIntoView({ block: 'nearest', behavior: 'smooth' }) | ||
| } | ||
| } | ||
|
|
||
| function handleKeydown(e: KeyboardEvent) { | ||
| // Only handle arrow keys and Enter | ||
| if (!['ArrowDown', 'ArrowUp', 'Enter'].includes(e.key)) { | ||
| return | ||
| } | ||
|
|
||
| if (!keyboardShortcuts.value) { | ||
| return | ||
| } | ||
|
|
||
| const elements = getFocusableElements() | ||
| const currentIndex = elements.findIndex(el => el === document.activeElement) | ||
|
|
||
| if (e.key === 'ArrowDown') { | ||
| // If there are results available, handle navigation | ||
| if (elements.length > 0) { | ||
| e.preventDefault() | ||
| e.stopPropagation() | ||
|
|
||
| // If no result is focused, focus the first one | ||
| if (currentIndex < 0) { | ||
| const firstEl = elements[0] | ||
| if (firstEl) focusElement(firstEl) | ||
| return | ||
| } | ||
|
|
||
| // If a result is already focused, move to the next one | ||
| const nextIndex = Math.min(currentIndex + 1, elements.length - 1) | ||
| const el = elements[nextIndex] | ||
| if (el) focusElement(el) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if (e.key === 'ArrowUp') { | ||
| // Only intercept if a result is already focused | ||
| if (currentIndex >= 0) { | ||
| e.preventDefault() | ||
| e.stopPropagation() | ||
|
|
||
| // At first result | ||
| if (currentIndex === 0) { | ||
| // Call custom callback if provided (e.g., return focus to search input) | ||
| if (options?.onArrowUpAtStart) { | ||
| options.onArrowUpAtStart() | ||
| } | ||
| return | ||
| } | ||
| const nextIndex = currentIndex - 1 | ||
| const el = elements[nextIndex] | ||
| if (el) focusElement(el) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if (e.key === 'Enter') { | ||
| // Handle Enter on focused card - click the main link inside | ||
| if (document.activeElement && elements.includes(document.activeElement as HTMLElement)) { | ||
| const card = document.activeElement as HTMLElement | ||
| // Find the first link inside the card and click it | ||
| const link = card.querySelector('a') | ||
| if (link) { | ||
| e.preventDefault() | ||
| e.stopPropagation() | ||
| link.click() | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Register keyboard event listeners using useEventListener for better control | ||
| // Use capture phase to intercept before other handlers | ||
| useEventListener(document, 'keydown', handleKeydown, { capture: true }) | ||
|
|
||
| return { | ||
| getFocusableElements, | ||
| focusElement, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't steal ArrowUp/ArrowDown from native controls.
This handler runs at document scope and calls
preventDefault()before the page's own controls can react. On bothapp/pages/org/[org].vue:267-288andapp/pages/search.vue:700-724, pressing ArrowDown inside the toolbar's selects or text inputs will jump to the first result instead of moving the caret or opening the control. Please ignore interactive targets unless focus is already on a result element or on an explicitly allowed hand-off source.Possible direction
export function useResultsKeyboardNavigation(options?: { includeSuggestions?: boolean onArrowUpAtStart?: () => void + canStartFrom?: (target: HTMLElement | null) => boolean }) { @@ const elements = getFocusableElements() const currentIndex = elements.findIndex(el => el === document.activeElement) + const target = e.target instanceof HTMLElement ? e.target : null + const isInteractiveTarget = !!target?.closest( + 'input, textarea, select, button, [contenteditable="true"]', + ) + + if (currentIndex < 0 && isInteractiveTarget && !options?.canStartFrom?.(target)) { + return + }There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe stealing the arrow keys is desired in this instance, but anyone please tell me if I'm wrong here 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Comment image shows an organization page at localhost:3000/org/atcute in a npm-like UI. Visible elements include:
@atcute/time-ms,@atcute/bluesky) each with description, date, version, and weekly downloads.@tylersayshiI love the consistency goal, but stealing ArrowUp/ArrowDown from native controls (inputs/selects/textarea/contenteditable) at the document level will block expected keyboard behavior (caret movement, opening selects, adjusting ranges) and hurts accessibility. Right now, ArrowDown is intercepted even when focus is inside the sort select or the search box, because we call preventDefault in capture before those controls can react.A small compromise keeps the desired list navigation while preserving native control behavior:
Minimal patch to the composable
Call-site usage
Optional guard
Tests to lock behavior