-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for keyboard navigation in search dialog (#149)
* Accept searchService as prop in SearchDialog * Make hover a controlled state in SearchHits * Implement useKeyboardPress hook * Hover search hits with keyboard * Navigate to search hit page with Enter * Automatically hover first hit * Store a map of search hit refs and focus on arrow key press * Lock pointer on arrow key press and exit lock on mouse move
- Loading branch information
Showing
6 changed files
with
196 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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
58 changes: 58 additions & 0 deletions
58
packages/commons/react/react-commons/src/useKeyboardPress.ts
This file contains 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,58 @@ | ||
import { type Digit, type UppercaseLetter } from "@fern-ui/core-utils"; | ||
import { useEffect } from "react"; | ||
|
||
type Arrow = "Up" | "Down" | "Right" | "Left"; | ||
|
||
type OtherKey = "Enter"; | ||
|
||
export declare namespace useKeyboardPress { | ||
export interface Args { | ||
key: UppercaseLetter | Digit | Arrow | OtherKey; | ||
onPress: () => void | Promise<void>; | ||
preventDefault?: boolean; | ||
} | ||
|
||
export type Return = void; | ||
} | ||
|
||
function isUppercaseLetter(key: unknown): key is UppercaseLetter { | ||
return typeof key === "string" && key.length === 1 && key.charCodeAt(0) >= 65 && key.charCodeAt(0) <= 90; | ||
} | ||
|
||
function isArrow(key: unknown): key is Arrow { | ||
return typeof key === "string" && ["Up", "Down", "Right", "Left"].includes(key); | ||
} | ||
|
||
function isDigit(key: unknown): key is Digit { | ||
return typeof key === "number" && Number.isInteger(key) && key >= 0 && key <= 9; | ||
} | ||
|
||
export function useKeyboardPress(args: useKeyboardPress.Args): void { | ||
const { key, onPress, preventDefault = false } = args; | ||
|
||
useEffect(() => { | ||
async function handleSaveKeyPress(e: KeyboardEvent) { | ||
const doKeysMatch = | ||
e.code === | ||
(isUppercaseLetter(key) | ||
? `Key${key}` | ||
: isArrow(key) | ||
? `Arrow${key}` | ||
: isDigit(key) | ||
? `Digit${key}` | ||
: key); | ||
if (doKeysMatch) { | ||
if (preventDefault) { | ||
e.preventDefault(); | ||
} | ||
await onPress(); | ||
} | ||
} | ||
|
||
document.addEventListener("keydown", handleSaveKeyPress, false); | ||
|
||
return () => { | ||
document.removeEventListener("keydown", handleSaveKeyPress, false); | ||
}; | ||
}, [key, onPress, preventDefault]); | ||
} |
This file contains 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 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 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 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
eecfd5e
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.
Successfully deployed to the following URLs:
fern-dev – ./packages/ui/fe-bundle
fern-dev-git-main-buildwithfern.vercel.app
app-dev.buildwithfern.com
fern-dev-buildwithfern.vercel.app
devtest.buildwithfern.com