docs: fix keyboard navigation in search (#12434)
This commit is contained in:
@@ -126,6 +126,7 @@ export const SearchHits = ({
|
||||
"[&_mark]:bg-medusa-bg-highlight",
|
||||
"[&_mark]:text-medusa-fg-interactive"
|
||||
)}
|
||||
data-group
|
||||
>
|
||||
{hits.map((item, index) => {
|
||||
const hierarchies = Object.values(item.hierarchy)
|
||||
|
||||
@@ -65,8 +65,18 @@ export const useSearchNavigation = ({
|
||||
// find if there's a data-hit item before this one
|
||||
const beforeItem = findPrevSibling(focusedItem, "[data-hit]")
|
||||
if (!beforeItem) {
|
||||
// focus the input
|
||||
focusInput()
|
||||
// check if there's a data-group before this one
|
||||
const beforeGroup = focusedItem.parentElement
|
||||
? (findPrevSibling(focusedItem.parentElement, "[data-group]")
|
||||
?.lastElementChild as HTMLElement)
|
||||
: null
|
||||
if (beforeGroup) {
|
||||
// focus the last item in the previous group
|
||||
beforeGroup.focus()
|
||||
} else {
|
||||
// focus the input
|
||||
focusInput()
|
||||
}
|
||||
} else {
|
||||
// focus the previous item
|
||||
beforeItem.focus()
|
||||
@@ -87,6 +97,15 @@ export const useSearchNavigation = ({
|
||||
if (afterItem) {
|
||||
// focus the next item
|
||||
afterItem.focus()
|
||||
} else if (focusedItem.parentElement) {
|
||||
// try to go to next hit in the next group
|
||||
const nextGroupFirstItem = findNextSibling(
|
||||
focusedItem.parentElement,
|
||||
"[data-group]"
|
||||
)?.firstElementChild as HTMLElement
|
||||
if (nextGroupFirstItem) {
|
||||
nextGroupFirstItem.focus()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user