Skip to content
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

feat: keyboard navigation in search #4651

Merged
merged 6 commits into from
Sep 11, 2023
Merged

Conversation

kwasniew
Copy link
Contributor

@kwasniew kwasniew commented Sep 11, 2023

About the changes

Screenshot 2023-09-11 at 11 45 51
  • tab navigation inside search suggestions and outside search suggestions
  • when tab moves away from the search suggestions, the suggestions are hidden
  • also supports escape key from the search suggestions
  • added onUseBlur reusable hook that handles element loosing focus
  • fix search and filter to filter our filters if they don't exists (previously it was undefined)

Important files

Discussion points

For now the elements we tab between are spans even though button would be more correct. Buttons add too many styles that we'd need to remove.

@vercel
Copy link

vercel bot commented Sep 11, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
unleash-monorepo-frontend ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 11, 2023 10:36am
1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
unleash-docs ⬜️ Ignored (Inspect) Visit Preview Sep 11, 2023 10:36am

@@ -111,14 +112,15 @@ export const Search = ({
}
);
useKeyboardShortcut({ key: 'Escape' }, () => {
if (document.activeElement === searchInputRef.current) {
if (searchContainerRef.current?.contains(document.activeElement)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

escape anywhere in the search area should hide suggestions

@@ -57,6 +61,10 @@ export const SearchInstructions: VFC<ISearchInstructionsProps> = ({
condition={filter.options.length > 0}
show={
<StyledCode
tabIndex={0}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previously we only supported clicks, now we support on enter when tab index is selected

).toBeInTheDocument();

screen.getByText(/Title A/i).click();
expect(recordedSuggestion).toBe('Title A');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previously undefined from filters was added

? ''
: filters.map(
filter => `${filter.name}:${filter.suggestedOption}`
)[0];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

select index only when filters are present

filter => `${filter.name}:${filter.suggestedOption}`
)[0];

const onFilter = (suggestion: string) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extracted existing methods since we use them in click and key down handlers

@@ -0,0 +1,31 @@
import { useEffect } from 'react';

export const useOnBlur = (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reusable hook for navigation away from the element

Copy link
Member

@Tymek Tymek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kwasniew kwasniew merged commit ba73d9a into main Sep 11, 2023
15 checks passed
@kwasniew kwasniew deleted the search-keyboard-navigation branch September 11, 2023 10:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

2 participants