-
Notifications
You must be signed in to change notification settings - Fork 49
refactor(global-search): keep browser focus but allow for keyboard nav #450
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Binary file removed
BIN
-138 KB
.yarn/cache/@formatjs-ecma402-abstract-npm-2.3.4-0a75348ee7-573971ffc2.zip
Binary file not shown.
Binary file removed
BIN
-4.85 KB
.yarn/cache/@formatjs-fast-memoize-npm-2.2.7-bc909b3b5a-e7e6efc677.zip
Binary file not shown.
Binary file removed
BIN
-55.8 KB
.yarn/cache/@formatjs-icu-messageformat-parser-npm-2.11.2-eebf23abca-e919eb2a13.zip
Binary file not shown.
Binary file removed
BIN
-13.6 KB
.yarn/cache/@formatjs-icu-skeleton-parser-npm-1.8.14-149a20738f-2fbe3155c3.zip
Binary file not shown.
Binary file removed
BIN
-47.7 KB
.yarn/cache/@formatjs-intl-localematcher-npm-0.6.1-fdc7ac427d-c7b3bc8395.zip
Binary file not shown.
Binary file removed
BIN
-304 KB
.yarn/cache/@internationalized-date-npm-3.9.0-ababaf9685-d2da1425c3.zip
Binary file not shown.
Binary file removed
BIN
-28 KB
.yarn/cache/@internationalized-message-npm-3.1.8-ab5bc75ce8-8f27a31f5d.zip
Binary file not shown.
Binary file removed
BIN
-65.1 KB
.yarn/cache/@internationalized-number-npm-3.6.5-fd3358efdd-f80618842b.zip
Binary file not shown.
Binary file removed
BIN
-32.8 KB
.yarn/cache/@internationalized-string-npm-3.2.7-0f557b7dcc-38b54817cf.zip
Binary file not shown.
Binary file removed
BIN
-86.5 KB
.yarn/cache/@react-aria-breadcrumbs-npm-3.5.28-7225b91f7f-483dd9c0b6.zip
Binary file not shown.
Binary file removed
BIN
-39.4 KB
.yarn/cache/@react-aria-button-npm-3.14.1-59228ea405-2f31b88079.zip
Binary file not shown.
Binary file removed
BIN
-212 KB
.yarn/cache/@react-aria-calendar-npm-3.9.1-d95d887b88-87f0d70b70.zip
Binary file not shown.
Binary file removed
BIN
-47.5 KB
.yarn/cache/@react-aria-checkbox-npm-3.16.1-f9cdcd7779-f3f83a105c.zip
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-158 KB
.yarn/cache/@react-aria-combobox-npm-3.13.2-8f78d172f7-9f3dd28acd.zip
Binary file not shown.
Binary file removed
BIN
-238 KB
.yarn/cache/@react-aria-datepicker-npm-3.15.1-598f3bfd74-ab897cadc4.zip
Binary file not shown.
Binary file removed
BIN
-22.2 KB
.yarn/cache/@react-aria-dialog-npm-3.5.30-4b68cd48fd-a95ded7450.zip
Binary file not shown.
Binary file removed
BIN
-22.2 KB
.yarn/cache/@react-aria-disclosure-npm-3.0.8-7b2c9e7861-6f08c91f7d.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-74.6 KB
.yarn/cache/@react-aria-gridlist-npm-3.14.0-31e51b98a6-8941740df4.zip
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-279 KB
.yarn/cache/@react-aria-interactions-npm-3.25.5-e5abbe1047-9f8d319049.zip
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-47.3 KB
.yarn/cache/@react-aria-landmark-npm-3.0.6-a413082755-28ebb58dc9.zip
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-52.7 KB
.yarn/cache/@react-aria-listbox-npm-3.14.8-562ff6d97a-5ec2cda280.zip
Binary file not shown.
Binary file removed
BIN
-26.4 KB
.yarn/cache/@react-aria-live-announcer-npm-3.4.4-276b952447-058859f7c0.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-112 KB
.yarn/cache/@react-aria-numberfield-npm-3.12.1-8384f4f9ac-379a08bb8a.zip
Binary file not shown.
Binary file removed
BIN
-285 KB
.yarn/cache/@react-aria-overlays-npm-3.29.1-666fcb1ef5-78e0d606f9.zip
Binary file not shown.
Binary file removed
BIN
-19.3 KB
.yarn/cache/@react-aria-progress-npm-3.4.26-a6f0b824a7-962b79cb4d.zip
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-85.7 KB
.yarn/cache/@react-aria-searchfield-npm-3.8.8-de8506ecff-96a1ae6f0b.zip
Binary file not shown.
Binary file removed
BIN
-46.6 KB
.yarn/cache/@react-aria-select-npm-3.16.2-5a2fc9ee67-2c19e1dd60.zip
Binary file not shown.
Binary file removed
BIN
-140 KB
.yarn/cache/@react-aria-selection-npm-3.25.1-7652c84ccc-f242b7855a.zip
Binary file not shown.
Binary file removed
BIN
-17.8 KB
.yarn/cache/@react-aria-separator-npm-3.4.12-1ba4c6ad1f-fad230f209.zip
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-84.7 KB
.yarn/cache/@react-aria-spinbutton-npm-3.6.18-27c033a1b1-5ef38a2071.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-49.4 KB
.yarn/cache/@react-aria-textfield-npm-3.18.1-7716f72ce9-42ceaf7aa2.zip
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-22.9 KB
.yarn/cache/@react-aria-toggle-npm-3.12.1-ea6a3da7d5-cd201997db.zip
Binary file not shown.
Binary file removed
BIN
-24.6 KB
.yarn/cache/@react-aria-toolbar-npm-3.0.0-beta.20-3bc71fce44-721e5ff1f6.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-21.1 KB
.yarn/cache/@react-aria-visually-hidden-npm-3.8.27-91482d3c52-6c0f27ecdf.zip
Binary file not shown.
Binary file removed
BIN
-69.7 KB
.yarn/cache/@react-stately-calendar-npm-3.8.4-5aa7b190f7-d27c218ea3.zip
Binary file not shown.
Binary file removed
BIN
-21.9 KB
.yarn/cache/@react-stately-checkbox-npm-3.7.1-05596a45f5-b71bfdd090.zip
Binary file not shown.
Binary file removed
BIN
-75.5 KB
.yarn/cache/@react-stately-collections-npm-3.12.7-03ba0a1fbe-d1b3f8c977.zip
Binary file not shown.
Binary file removed
BIN
-279 KB
.yarn/cache/@react-stately-color-npm-3.9.1-bdef17ffd3-2c27475ef1.zip
Binary file not shown.
Binary file removed
BIN
-42 KB
.yarn/cache/@react-stately-combobox-npm-3.11.1-224fed4c76-ead4660d56.zip
Binary file not shown.
Binary file removed
BIN
-233 KB
.yarn/cache/@react-stately-datepicker-npm-3.15.1-743575fddf-044f7ef59b.zip
Binary file not shown.
Binary file removed
BIN
-27.4 KB
.yarn/cache/@react-stately-disclosure-npm-3.0.7-f9d40a3481-b5f250bf80.zip
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-11.3 KB
.yarn/cache/@react-stately-flags-npm-3.1.2-a23179ba53-a020c3680c.zip
Binary file not shown.
Binary file removed
BIN
-32.6 KB
.yarn/cache/@react-stately-form-npm-3.2.1-21a221e45d-2d4ab37c17.zip
Binary file not shown.
Binary file removed
BIN
-37.2 KB
.yarn/cache/@react-stately-grid-npm-3.11.5-7c3c852320-ae6da14a91.zip
Binary file not shown.
Binary file removed
BIN
-45.1 KB
.yarn/cache/@react-stately-list-npm-3.13.0-0510dba1cf-548e2205cc.zip
Binary file not shown.
Binary file removed
BIN
-29.3 KB
.yarn/cache/@react-stately-menu-npm-3.9.7-5cd513dc8b-26953c79e1.zip
Binary file not shown.
Binary file removed
BIN
-34.1 KB
.yarn/cache/@react-stately-numberfield-npm-3.10.1-5e2013bae0-ba91eea935.zip
Binary file not shown.
Binary file removed
BIN
-17.6 KB
.yarn/cache/@react-stately-overlays-npm-3.6.19-c4b08ff41a-1308646db8.zip
Binary file not shown.
Binary file removed
BIN
-20.8 KB
.yarn/cache/@react-stately-radio-npm-3.11.1-fe01447820-0e42fe059b.zip
Binary file not shown.
Binary file removed
BIN
-17 KB
.yarn/cache/@react-stately-searchfield-npm-3.5.15-43cecb9493-4a7b45bb15.zip
Binary file not shown.
Binary file removed
BIN
-20.1 KB
.yarn/cache/@react-stately-select-npm-3.7.1-7da07b1be2-1987a7ac5c.zip
Binary file not shown.
Binary file removed
BIN
-57.6 KB
.yarn/cache/@react-stately-selection-npm-3.20.5-d38116232e-c37b7df47f.zip
Binary file not shown.
Binary file removed
BIN
-30.5 KB
.yarn/cache/@react-stately-slider-npm-3.7.1-02c385e2ba-8aed161468.zip
Binary file not shown.
Binary file removed
BIN
-165 KB
.yarn/cache/@react-stately-table-npm-3.15.0-66030165f4-a7f178d499.zip
Binary file not shown.
Binary file removed
BIN
-22.6 KB
.yarn/cache/@react-stately-tabs-npm-3.8.5-14af03a520-51b2cf8303.zip
Binary file not shown.
Binary file removed
BIN
-26.2 KB
.yarn/cache/@react-stately-toast-npm-3.1.2-6660726253-771eade3a6.zip
Binary file not shown.
Binary file removed
BIN
-27.5 KB
.yarn/cache/@react-stately-toggle-npm-3.9.1-4d461aa8d2-f651a48c85.zip
Binary file not shown.
Binary file removed
BIN
-22.3 KB
.yarn/cache/@react-stately-tooltip-npm-3.5.7-07b2e4fa10-873b1e941e.zip
Binary file not shown.
Binary file removed
BIN
-30.5 KB
.yarn/cache/@react-stately-tree-npm-3.9.2-cad09e3a73-922be4237c.zip
Binary file not shown.
Binary file removed
BIN
-31.9 KB
.yarn/cache/@react-stately-utils-npm-3.10.8-e77dd8ce30-7878ec47b1.zip
Binary file not shown.
Binary file removed
BIN
-6.52 KB
.yarn/cache/@react-types-breadcrumbs-npm-3.7.16-ad53903a39-1272db9cc0.zip
Binary file not shown.
Binary file removed
BIN
-7.78 KB
.yarn/cache/@react-types-button-npm-3.14.0-3c21d761b1-458211f62b.zip
Binary file not shown.
Binary file removed
BIN
-7.06 KB
.yarn/cache/@react-types-calendar-npm-3.7.4-e48f53fb7b-d6e724d51c.zip
Binary file not shown.
Binary file removed
BIN
-6.84 KB
.yarn/cache/@react-types-checkbox-npm-3.10.1-d0d68f4db6-34fcba28c2.zip
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-7.08 KB
.yarn/cache/@react-types-combobox-npm-3.13.8-7bd8b54b53-df1bd6934f.zip
Binary file not shown.
Binary file removed
BIN
-7.95 KB
.yarn/cache/@react-types-datepicker-npm-3.13.1-7c9d421e17-431b8f527e.zip
Binary file not shown.
Binary file removed
BIN
-6.97 KB
.yarn/cache/@react-types-dialog-npm-3.5.21-da479dc64e-5d665f28a1.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-6.52 KB
.yarn/cache/@react-types-listbox-npm-3.7.3-2db6a26cda-8a775922c4.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-6.42 KB
.yarn/cache/@react-types-numberfield-npm-3.8.14-d75a5244b4-cbcd9b065b.zip
Binary file not shown.
Binary file removed
BIN
-6.77 KB
.yarn/cache/@react-types-overlays-npm-3.9.1-81707f395c-994de8c236.zip
Binary file not shown.
Binary file removed
BIN
-6.6 KB
.yarn/cache/@react-types-progress-npm-3.5.15-8d252287f8-6a7f2e790a.zip
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-6.29 KB
.yarn/cache/@react-types-searchfield-npm-3.6.5-d32ed08a2f-1eea5feeed.zip
Binary file not shown.
Binary file removed
BIN
-6.67 KB
.yarn/cache/@react-types-select-npm-3.10.1-c160d00c1a-ec19a911f8.zip
Binary file not shown.
Binary file removed
BIN
-28.8 KB
.yarn/cache/@react-types-shared-npm-3.32.0-10df6e4cbf-6dd7e2a20e.zip
Binary file not shown.
Binary file removed
BIN
-6.72 KB
.yarn/cache/@react-types-slider-npm-3.8.1-81ebe5c2b6-d4f8a2f888.zip
Binary file not shown.
Binary file removed
BIN
-6.33 KB
.yarn/cache/@react-types-switch-npm-3.5.14-c1f2e854b6-155006ba79.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-6.85 KB
.yarn/cache/@react-types-textfield-npm-3.12.5-ebced44ec4-cc19cbabda.zip
Binary file not shown.
Binary file removed
BIN
-6.51 KB
.yarn/cache/@react-types-tooltip-npm-3.4.20-c90dd9ade0-d09fddfd2e.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-50.4 KB
.yarn/cache/intl-messageformat-npm-10.7.16-87c5593481-c19b77c5e4.zip
Binary file not shown.
Binary file not shown.
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
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
89 changes: 89 additions & 0 deletions
89
plugins/global-search/src/utils/selection/SelectionProvider.tsx
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,89 @@ | ||
| import React, { useCallback, useLayoutEffect, useMemo, useRef, useState } from "react" | ||
| import { isHeader, NavigationDirection } from "./constants" | ||
| import { SelectionContext } from "./context" | ||
|
|
||
| export function SelectionProvider({ children }: { children: React.ReactNode }) { | ||
| const [activeId, setActiveId] = useState<string | null>(null) | ||
| const idsRef = useRef<readonly string[]>([]) | ||
| const inputRef = useRef<HTMLInputElement | null>(null) | ||
|
|
||
| // scroll to active id | ||
| useLayoutEffect(() => { | ||
| if (!activeId) return | ||
| document.getElementById(activeId)?.scrollIntoView({ behavior: "smooth", block: "center" }) | ||
| }, [activeId]) | ||
|
|
||
| const setItems = useCallback( | ||
| (ids: readonly string[]) => { | ||
| idsRef.current = ids | ||
| if (ids.length === 0) setActiveId(null) | ||
| // if active id is not in the new ids, clear the active id | ||
| // either the group is collapsed or the item is no longer in the results | ||
| if (activeId && !ids.includes(activeId)) setActiveId(null) | ||
| }, | ||
| [activeId] | ||
| ) | ||
|
|
||
| const nextIndex = useCallback((fromIndex: number | null, direction: NavigationDirection): number | null => { | ||
| const itemCount = idsRef.current.length | ||
| const start = fromIndex ?? (direction === NavigationDirection.Down ? -1 : itemCount) | ||
| const idx = start + direction | ||
| return idx < 0 || idx >= itemCount | ||
| ? null // out of bounds and clear | ||
| : idx | ||
| }, []) | ||
|
|
||
| const findNextSelectable = useCallback( | ||
| (fromIndex: number | null, direction: NavigationDirection): string | null => { | ||
| const idx = nextIndex(fromIndex, direction) | ||
| if (idx === null) return null | ||
| const id = idsRef.current[idx] | ||
| // skip headers and continue searching | ||
| if (isHeader(id)) return findNextSelectable(idx, direction) | ||
|
|
||
| return id ?? null | ||
| }, | ||
| [nextIndex] | ||
| ) | ||
|
|
||
| const handleKeyDown = useCallback( | ||
| (event: React.KeyboardEvent<HTMLElement>) => { | ||
| if (event.key === "ArrowDown" || event.key === "ArrowUp") { | ||
| event.preventDefault() | ||
| // Ensure the search input retains focus while navigating, prevents other elements from being focused | ||
| inputRef.current?.focus() | ||
| const currentIndex = activeId ? idsRef.current.indexOf(activeId) : null | ||
| const direction = event.key === "ArrowDown" ? NavigationDirection.Down : NavigationDirection.Up | ||
| const nextId = findNextSelectable(currentIndex, direction) | ||
| // end on the last or first item when repeating | ||
| if (event.repeat && nextId === null) return | ||
| setActiveId(nextId) | ||
|
elmarburke marked this conversation as resolved.
|
||
| } else if (event.key === "Enter" && activeId && !isHeader(activeId)) { | ||
| document.getElementById(activeId)?.click() | ||
| event.preventDefault() | ||
| } | ||
| }, | ||
| [activeId, findNextSelectable] | ||
| ) | ||
|
|
||
| const value = useMemo( | ||
| () => ({ | ||
| activeId, | ||
| setItems, | ||
| getFocusProps: (id: string) => ({ | ||
| id, | ||
| onFocus: () => { | ||
| setActiveId(id) | ||
| }, | ||
| onKeyDown: handleKeyDown, | ||
| }), | ||
| inputFocusProps: { onKeyDown: handleKeyDown }, | ||
| inputRef: (el: HTMLInputElement | null) => { | ||
| inputRef.current = el | ||
| }, | ||
| }), | ||
| [activeId, setItems, handleKeyDown] | ||
| ) | ||
|
|
||
| return <SelectionContext.Provider value={value}>{children}</SelectionContext.Provider> | ||
| } | ||
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,9 @@ | ||
| const HDR_PREFIX = "hdr:" | ||
|
|
||
| export const isHeader = (id: string | undefined) => id?.startsWith(HDR_PREFIX) | ||
| export const headerId = (id: string) => `${HDR_PREFIX}${id}` | ||
|
|
||
| export enum NavigationDirection { | ||
| Down = 1, | ||
| Up = -1, | ||
| } |
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.
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.
Bug: Group Headers Incorrectly Activate on Click
Group headers become active when clicked, which isn't the intended behavior and creates an inconsistency with keyboard navigation that correctly skips them. The
getFocusPropsfunction inSelectionProvidersets any focused item as active, including headers.Additional Locations (1)
plugins/global-search/src/utils/selection/SelectionProvider.tsx#L69-L75There 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.
That's very much intended here!