mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-28 00:17:12 +01:00
feat: allow command bar navigation to wrap around (#9370)
Lets you navigate to the top of the list when you're at the bottom, and vice versa. Arrow down at the end of the list takes you to the search field and arrow up from the search field takes you to the end of the list.
This commit is contained in:
parent
3f373665ed
commit
27bd4e2491
@ -257,9 +257,18 @@ export const CommandBar = () => {
|
||||
const { allCommandBarLinks, selectedIndex } = itemsAndIndex;
|
||||
|
||||
const newIndex = selectedIndex + 1;
|
||||
if (newIndex >= allCommandBarLinks.length) return;
|
||||
|
||||
(allCommandBarLinks[newIndex] as HTMLElement).focus();
|
||||
if (newIndex >= allCommandBarLinks.length) {
|
||||
const element = searchInputRef.current;
|
||||
if (element) {
|
||||
element.focus();
|
||||
element.setSelectionRange(
|
||||
element.value.length,
|
||||
element.value.length,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
(allCommandBarLinks[newIndex] as HTMLElement).focus();
|
||||
}
|
||||
},
|
||||
);
|
||||
useKeyboardShortcut(
|
||||
@ -276,7 +285,7 @@ export const CommandBar = () => {
|
||||
|
||||
if (newIndex >= 0) {
|
||||
(allCommandBarLinks[newIndex] as HTMLElement).focus();
|
||||
} else {
|
||||
} else if (newIndex === -1) {
|
||||
const element = searchInputRef.current;
|
||||
if (element) {
|
||||
element.focus();
|
||||
@ -285,6 +294,12 @@ export const CommandBar = () => {
|
||||
element.value.length,
|
||||
);
|
||||
}
|
||||
} else if (newIndex === -2) {
|
||||
(
|
||||
allCommandBarLinks[
|
||||
allCommandBarLinks.length - 1
|
||||
] as HTMLElement
|
||||
).focus();
|
||||
}
|
||||
},
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user