-
Notifications
You must be signed in to change notification settings - Fork 138
Improve keyboard navigation for profile selector #368
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,6 +58,28 @@ export const ProfilePicker = ({ | |
| { value: 'motorcycle', label: 'Motorcycle' }, | ||
| ]; | ||
|
|
||
| const handleKeyDown = (event: React.KeyboardEvent, index: number) => { | ||
| if (event.key === 'ArrowRight') { | ||
| event.preventDefault(); | ||
| const next = (index + 1) % profiles.length; | ||
| const nextProfile = profiles[next]; | ||
|
|
||
| if (nextProfile) { | ||
| handleUpdateProfile(nextProfile.value as Profile); | ||
| } | ||
| } | ||
|
|
||
| if (event.key === 'ArrowLeft') { | ||
| event.preventDefault(); | ||
| const prev = (index - 1 + profiles.length) % profiles.length; | ||
| const prevProfile = profiles[prev]; | ||
|
|
||
| if (prevProfile) { | ||
| handleUpdateProfile(prevProfile.value as Profile); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="flex flex-col gap-2"> | ||
| <TooltipProvider> | ||
|
|
@@ -79,6 +101,7 @@ export const ProfilePicker = ({ | |
| aria-label={`Select ${profile.label} profile`} | ||
| data-testid={`profile-button-` + profile.value} | ||
| data-state={profile.value === activeProfile ? 'on' : 'off'} | ||
| onKeyDown={(e) => handleKeyDown(e, i)} | ||
| > | ||
|
Comment on lines
103
to
105
|
||
| {profile.value === activeProfile && loading ? ( | ||
| <Loader2 className="size-4 animate-spin" /> | ||
|
|
||
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.
handleKeyDownchanges the selected profile but does not move focus to the newly selectedToggleGroupItem. Because the handler’sindexis bound to the originally focused button, subsequent ArrowLeft/ArrowRight presses can keep computing the next/prev from the old index and repeatedly re-select the same profile. Consider implementing roving-focus behavior by programmatically focusing the next/prev item (e.g., keep refs to items and call.focus()on the target), or compute next/prev from the currently selectedactiveProfileand also guard against no-op updates.