Skip to content

Commit c57abd4

Browse files
committed
feat: add keyboard shortcut for selecting items in CommandBarBackdrop
1 parent c4b6870 commit c57abd4

File tree

2 files changed

+69
-37
lines changed

2 files changed

+69
-37
lines changed

src/Pages/Shared/CommandBar/CommandBarBackdrop.tsx

Lines changed: 64 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ const CommandBarBackdrop = ({ handleClose }: CommandBarBackdropProps) => {
6464

6565
const handleSearchChange = (value: string) => {
6666
setSearchText(value)
67-
setSelectedItemIndex(0)
67+
if (value !== searchText) {
68+
setSelectedItemIndex(0)
69+
}
6870
}
6971

7072
const updateItemRefMap = (id: string, el: HTMLDivElement) => {
@@ -96,7 +98,7 @@ const CommandBarBackdrop = ({ handleClose }: CommandBarBackdropProps) => {
9698
return recentActionsGroup
9799
? [...recentActionsGroup.items, ...NAVIGATION_GROUPS.flatMap((group) => group.items)]
98100
: []
99-
}, [recentActionsGroup, filteredGroups])
101+
}, [areFiltersApplied, recentActionsGroup, filteredGroups])
100102

101103
const handleClearFilters = () => {
102104
setSearchText('')
@@ -141,37 +143,6 @@ const CommandBarBackdrop = ({ handleClose }: CommandBarBackdropProps) => {
141143
})
142144
}
143145

144-
useEffect(() => {
145-
registerShortcut({
146-
keys: SHORT_CUTS.FOCUS_SEARCH_BAR.keys,
147-
description: SHORT_CUTS.FOCUS_SEARCH_BAR.description,
148-
callback: focusSearchBar,
149-
})
150-
151-
return () => {
152-
unregisterShortcut(SHORT_CUTS.FOCUS_SEARCH_BAR.keys)
153-
}
154-
}, [])
155-
156-
useEffect(() => {
157-
registerShortcut({
158-
keys: SHORT_CUTS.NAVIGATE_UP.keys,
159-
description: SHORT_CUTS.NAVIGATE_UP.description,
160-
callback: () => handleNavigation('up'),
161-
})
162-
163-
registerShortcut({
164-
keys: SHORT_CUTS.NAVIGATE_DOWN.keys,
165-
description: SHORT_CUTS.NAVIGATE_DOWN.description,
166-
callback: () => handleNavigation('down'),
167-
})
168-
169-
return () => {
170-
unregisterShortcut(SHORT_CUTS.NAVIGATE_UP.keys)
171-
unregisterShortcut(SHORT_CUTS.NAVIGATE_DOWN.keys)
172-
}
173-
}, [itemFlatList])
174-
175146
const sanitizeItemId = (item: CommandBarItemType) =>
176147
(item.id.startsWith(RECENT_NAVIGATION_ITEM_ID_PREFIX)
177148
? item.id.replace(RECENT_NAVIGATION_ITEM_ID_PREFIX, '')
@@ -192,14 +163,14 @@ const CommandBarBackdrop = ({ handleClose }: CommandBarBackdropProps) => {
192163
// In this now we will put the id as first item in the list and keep first 5 items then
193164
const updatedRecentActions: UserPreferencesType['commandBar']['recentNavigationActions'] = [
194165
{
195-
id: currentItemId as CommandBarActionIdType,
166+
id: currentItemId,
196167
},
197-
...(recentActionsGroup?.items
168+
...(recentActionsGroup?.items || [])
198169
.filter((action) => sanitizeItemId(action) !== currentItemId)
199170
.slice(0, 4)
200171
.map((action) => ({
201172
id: sanitizeItemId(action),
202-
})) || []),
173+
})),
203174
]
204175

205176
await updateUserPreferences({
@@ -208,6 +179,63 @@ const CommandBarBackdrop = ({ handleClose }: CommandBarBackdropProps) => {
208179
})
209180
}
210181

182+
const handleEnterSelectedItem = async () => {
183+
const selectedItem = itemFlatList[selectedItemIndex]
184+
185+
if (selectedItem) {
186+
await onItemClick(selectedItem)
187+
}
188+
}
189+
190+
useEffect(() => {
191+
const { keys } = SHORT_CUTS.OPEN_COMMAND_BAR
192+
193+
registerShortcut({
194+
keys,
195+
description: SHORT_CUTS.FOCUS_SEARCH_BAR.description,
196+
callback: focusSearchBar,
197+
})
198+
199+
return () => {
200+
unregisterShortcut(keys)
201+
}
202+
}, [])
203+
204+
useEffect(() => {
205+
const { keys } = SHORT_CUTS.ENTER_ITEM
206+
registerShortcut({
207+
keys,
208+
description: SHORT_CUTS.ENTER_ITEM.description,
209+
callback: handleEnterSelectedItem,
210+
})
211+
212+
return () => {
213+
unregisterShortcut(keys)
214+
}
215+
}, [selectedItemIndex, itemFlatList, recentActionsGroup])
216+
217+
useEffect(() => {
218+
const navigateUpKeys = SHORT_CUTS.NAVIGATE_UP.keys
219+
const navigateDownKeys = SHORT_CUTS.NAVIGATE_DOWN.keys
220+
221+
registerShortcut({
222+
keys: navigateUpKeys,
223+
description: SHORT_CUTS.NAVIGATE_UP.description,
224+
callback: () => handleNavigation('up'),
225+
})
226+
227+
registerShortcut({
228+
keys: navigateDownKeys,
229+
description: SHORT_CUTS.NAVIGATE_DOWN.description,
230+
callback: () => handleNavigation('down'),
231+
})
232+
233+
return () => {
234+
unregisterShortcut(navigateUpKeys)
235+
unregisterShortcut(navigateDownKeys)
236+
}
237+
}, [itemFlatList])
238+
211239
const renderNavigationGroups = (baseIndex: number) => {
212240
let nextIndex = baseIndex
213241

src/Pages/Shared/CommandBar/constants.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ export const RECENT_ACTIONS_GROUP: CommandBarGroupType = {
441441
export const RECENT_NAVIGATION_ITEM_ID_PREFIX = 'recent-navigation-' as const
442442

443443
export const SHORT_CUTS: Record<
444-
'OPEN_COMMAND_BAR' | 'FOCUS_SEARCH_BAR' | 'NAVIGATE_UP' | 'NAVIGATE_DOWN',
444+
'OPEN_COMMAND_BAR' | 'FOCUS_SEARCH_BAR' | 'NAVIGATE_UP' | 'NAVIGATE_DOWN' | 'ENTER_ITEM',
445445
{
446446
keys: SupportedKeyboardKeysType[]
447447
description: string
@@ -463,4 +463,8 @@ export const SHORT_CUTS: Record<
463463
keys: ['ArrowDown'],
464464
description: 'Navigate Down',
465465
},
466+
ENTER_ITEM: {
467+
keys: ['Enter'],
468+
description: 'Select Item',
469+
},
466470
} as const

0 commit comments

Comments
 (0)