|
1
|
"use client" |
|
2
|
|
|
3
|
import { useCallback, useEffect, useMemo } from "react" |
|
4
|
import { useSearch } from "../../providers/Search" |
|
5
|
import { findNextSibling, findPrevSibling } from "../../utils/dom-utils" |
|
6
|
import { |
|
7
|
useKeyboardShortcut, |
|
8
|
type useKeyboardShortcutOptions, |
|
9
|
} from "../use-keyboard-shortcut" |
|
10
|
|
|
11
|
export type useSearchNavigationProps = { |
|
12
|
getInputElm: () => HTMLInputElement | null |
|
13
|
focusInput: () => void |
|
14
|
handleSubmit?: () => void |
|
15
|
keyboardProps?: Partial<useKeyboardShortcutOptions> |
|
16
|
} |
|
17
|
|
|
18
|
export const useSearchNavigation = ({ |
|
19
|
getInputElm, |
|
20
|
focusInput, |
|
21
|
handleSubmit, |
|
22
|
keyboardProps, |
|
23
|
}: useSearchNavigationProps) => { |
|
24
|
const shortcutKeys = useMemo(() => ["ArrowUp", "ArrowDown", "Enter"], []) |
|
25
|
const { modalRef, isOpen } = useSearch() |
|
26
|
|
|
27
|
const handleKeyAction = (e: KeyboardEvent) => { |
|
28
|
if (!isOpen) { |
|
29
|
return |
|
30
|
} |
|
31
|
e.preventDefault() |
|
32
|
const focusedItem = modalRef.current?.querySelector(":focus") as HTMLElement |
|
33
|
if (!focusedItem) { |
|
34
|
// focus the first data-hit |
|
35
|
const nextItem = modalRef.current?.querySelector( |
|
36
|
"[data-hit]" |
|
37
|
) as HTMLElement |
|
38
|
nextItem?.focus() |
|
39
|
return |
|
40
|
} |
|
41
|
|
|
42
|
const isHit = focusedItem.hasAttribute("data-hit") |
|
43
|
const isInput = focusedItem.tagName.toLowerCase() === "input" |
|
44
|
|
|
45
|
if (!isHit && !isInput) { |
|
46
|
// ignore if focused items aren't input/data-hit |
|
47
|
return |
|
48
|
} |
|
49
|
|
|
50
|
const lowerPressedKey = e.key.toLowerCase() |
|
51
|
|
|
52
|
if (lowerPressedKey === "enter") { |
|
53
|
if (isHit) { |
|
54
|
// trigger click event of the focused element |
|
55
|
focusedItem.click() |
|
56
|
} else { |
|
57
|
handleSubmit?.() |
|
58
|
} |
|
59
|
return |
|
60
|
} |
|
61
|
|
|
62
|
if (lowerPressedKey === "arrowup") { |
|
63
|
// only hit items has action on arrow up |
|
64
|
if (isHit) { |
|
65
|
// find if there's a data-hit item before this one |
|
66
|
const beforeItem = findPrevSibling(focusedItem, "[data-hit]") |
|
67
|
if (!beforeItem) { |
|
68
|
// check if there's a data-group before this one |
|
69
|
const beforeGroup = focusedItem.parentElement |
|
70
|
? (findPrevSibling(focusedItem.parentElement, "[data-group]") |
|
71
|
?.lastElementChild as HTMLElement) |
|
72
|
: null |
|
73
|
if (beforeGroup) { |
|
74
|
// focus the last item in the previous group |
|
75
|
beforeGroup.focus() |
|
76
|
} else { |
|
77
|
// focus the input |
|
78
|
focusInput() |
|
79
|
} |
|
80
|
} else { |
|
81
|
// focus the previous item |
|
82
|
beforeItem.focus() |
|
83
|
} |
|
84
|
} |
|
85
|
} else if (lowerPressedKey === "arrowdown") { |
|
86
|
// check if item is input or hit |
|
87
|
if (isInput) { |
|
88
|
// go to the first data-hit item |
|
89
|
const nextItem = modalRef.current?.querySelector( |
|
90
|
"[data-index]:not(.hidden) [data-hit]" |
|
91
|
) as HTMLElement |
|
92
|
nextItem?.focus() |
|
93
|
} else { |
|
94
|
// handle go down for hit items |
|
95
|
// find if there's a data-hit item after this one |
|
96
|
const afterItem = findNextSibling(focusedItem, "[data-hit]") |
|
97
|
if (afterItem) { |
|
98
|
// focus the next item |
|
99
|
afterItem.focus() |
|
100
|
} else if (focusedItem.parentElement) { |
|
101
|
// try to go to next hit in the next group |
|
102
|
const nextGroupFirstItem = findNextSibling( |
|
103
|
focusedItem.parentElement, |
|
104
|
"[data-group]" |
|
105
|
)?.firstElementChild as HTMLElement |
|
106
|
if (nextGroupFirstItem) { |
|
107
|
nextGroupFirstItem.focus() |
|
108
|
} |
|
109
|
} |
|
110
|
} |
|
111
|
} |
|
112
|
} |
|
113
|
|
|
114
|
/** Handles starting to type which focuses the input */ |
|
115
|
const handleKeyDown = useCallback( |
|
116
|
(e: KeyboardEvent) => { |
|
117
|
if (!isOpen) { |
|
118
|
return |
|
119
|
} |
|
120
|
// check if shortcut keys were pressed |
|
121
|
const lowerPressedKey = e.key.toLowerCase() |
|
122
|
const pressedShortcut = [...shortcutKeys, "Escape"].some( |
|
123
|
(s) => s.toLowerCase() === lowerPressedKey |
|
124
|
) |
|
125
|
if (pressedShortcut) { |
|
126
|
return |
|
127
|
} |
|
128
|
|
|
129
|
const focusedItem = modalRef.current?.querySelector( |
|
130
|
":focus" |
|
131
|
) as HTMLElement |
|
132
|
const inputElm = getInputElm() |
|
133
|
if (inputElm && focusedItem !== inputElm) { |
|
134
|
focusInput() |
|
135
|
} |
|
136
|
}, |
|
137
|
[ |
|
138
|
shortcutKeys, |
|
139
|
isOpen, |
|
140
|
modalRef.current, |
|
141
|
shortcutKeys, |
|
142
|
getInputElm, |
|
143
|
focusInput, |
|
144
|
] |
|
145
|
) |
|
146
|
|
|
147
|
useEffect(() => { |
|
148
|
window.addEventListener("keydown", handleKeyDown) |
|
149
|
|
|
150
|
return () => { |
|
151
|
window.removeEventListener("keydown", handleKeyDown) |
|
152
|
} |
|
153
|
}, [handleKeyDown]) |
|
154
|
|
|
155
|
useKeyboardShortcut({ |
|
156
|
metakey: false, |
|
157
|
shortcutKeys: shortcutKeys, |
|
158
|
checkEditing: false, |
|
159
|
isLoading: false, |
|
160
|
action: handleKeyAction, |
|
161
|
...keyboardProps, |
|
162
|
}) |
|
163
|
} |