Skip to content

Commit ecd7650

Browse files
committed
feat: enhance CommandBarBackdrop with keyboard shortcuts and recent actions handling
1 parent c57abd4 commit ecd7650

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

src/Pages/Shared/CommandBar/CommandBarBackdrop.tsx

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
ResponseType,
1212
SearchBar,
1313
stopPropagation,
14+
SupportedKeyboardKeysType,
1415
updateUserPreferences,
1516
useQuery,
1617
useRegisterShortcut,
@@ -97,7 +98,7 @@ const CommandBarBackdrop = ({ handleClose }: CommandBarBackdropProps) => {
9798

9899
return recentActionsGroup
99100
? [...recentActionsGroup.items, ...NAVIGATION_GROUPS.flatMap((group) => group.items)]
100-
: []
101+
: [...NAVIGATION_GROUPS.flatMap((group) => group.items)]
101102
}, [areFiltersApplied, recentActionsGroup, filteredGroups])
102103

103104
const handleClearFilters = () => {
@@ -159,7 +160,6 @@ const CommandBarBackdrop = ({ handleClose }: CommandBarBackdropProps) => {
159160

160161
const currentItemId = sanitizeItemId(item)
161162

162-
// const updatedRecentActions = recentActionsGroup?.items.filter((action) => action.id !== currentItemId)
163163
// In this now we will put the id as first item in the list and keep first 5 items then
164164
const updatedRecentActions: UserPreferencesType['commandBar']['recentNavigationActions'] = [
165165
{
@@ -187,6 +187,21 @@ const CommandBarBackdrop = ({ handleClose }: CommandBarBackdropProps) => {
187187
}
188188
}
189189

190+
useEffect(() => {
191+
if (!isLoading && recentActionsGroup?.items?.length && !areFiltersApplied) {
192+
if (selectedItemIndex !== 0) {
193+
const selectedIndex = selectedItemIndex + recentActionsGroup.items.length
194+
195+
const itemElement = itemRefMap.current[itemFlatList[selectedIndex]?.id]
196+
if (itemElement) {
197+
itemElement.scrollIntoView({ block: 'nearest', inline: 'nearest', behavior: 'smooth' })
198+
}
199+
200+
setSelectedItemIndex(selectedIndex)
201+
}
202+
}
203+
}, [isLoading, recentActionsGroup])
204+
190205
useEffect(() => {
191206
const { keys } = SHORT_CUTS.OPEN_COMMAND_BAR
192207

@@ -261,6 +276,15 @@ const CommandBarBackdrop = ({ handleClose }: CommandBarBackdropProps) => {
261276
}
262277
}
263278

279+
const renderKeyboardShortcuts = (keys: SupportedKeyboardKeysType[], label: string) => (
280+
<div className="flexbox dc__gap-8 dc__align-items-center">
281+
{keys.map((key) => (
282+
<KeyboardShortcut key={key} keyboardKey={key} />
283+
))}
284+
<span className="cn-9 fs-12 fw-4 lh-20">{label}</span>
285+
</div>
286+
)
287+
264288
return (
265289
<Backdrop onEscape={handleEscape} onClick={handleClose} deactivateFocusOnEscape={!!searchText}>
266290
<div
@@ -294,6 +318,7 @@ const CommandBarBackdrop = ({ handleClose }: CommandBarBackdropProps) => {
294318
>
295319
{!areFiltersApplied && (
296320
<CommandGroup
321+
key="recent-navigation"
297322
{...(recentActionsGroup || RECENT_ACTIONS_GROUP)}
298323
isLoading={isLoading}
299324
baseIndex={0}
@@ -310,27 +335,11 @@ const CommandBarBackdrop = ({ handleClose }: CommandBarBackdropProps) => {
310335

311336
<div className="flexbox dc__content-space dc__align-items-center px-20 py-12 border__primary--top bg__secondary">
312337
<div className="flexbox dc__gap-20 dc__align-items-center">
313-
<div className="flexbox dc__gap-8 dc__align-items-center">
314-
<KeyboardShortcut keyboardKey="ArrowUp" />
315-
<KeyboardShortcut keyboardKey="ArrowDown" />
316-
<span className="cn-9 fs-12 fw-4 lh-20">to navigate</span>
317-
</div>
318-
319-
<div className="flexbox dc__gap-8 dc__align-items-center">
320-
<KeyboardShortcut keyboardKey="Enter" />
321-
<span className="cn-9 fs-12 fw-4 lh-20">to select</span>
322-
</div>
323-
324-
<div className="flexbox dc__gap-8 dc__align-items-center">
325-
<KeyboardShortcut keyboardKey="Escape" />
326-
<span className="cn-9 fs-12 fw-4 lh-20">to close</span>
327-
</div>
328-
</div>
329-
330-
<div className="flexbox dc__gap-8 dc__align-items-center">
331-
<KeyboardShortcut keyboardKey=">" />
332-
<span className="cn-9 fs-12 fw-4 lh-20">to search actions</span>
338+
{renderKeyboardShortcuts(['ArrowUp', 'ArrowDown'], 'to navigate')}
339+
{renderKeyboardShortcuts(['Enter'], 'to select')}
340+
{renderKeyboardShortcuts(['Escape'], 'to close')}
333341
</div>
342+
{renderKeyboardShortcuts(['>'], 'to search actions')}
334343
</div>
335344
</div>
336345
</Backdrop>

src/Pages/Shared/CommandBar/CommandGroup.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const CommandGroup = ({
3535
<div
3636
className={`flexbox px-16 py-12 cursor dc__align-items-center dc__gap-12 dc__content-space br-8 bg__hover ${selectedItemIndex === baseIndex + index ? 'command-bar__container--selected-item' : ''}`}
3737
role="option"
38+
id={item.id}
3839
aria-selected={selectedItemIndex === baseIndex + index}
3940
ref={updateItemRef(item.id)}
4041
onClick={getHandleItemClick(item)}

src/Pages/Shared/CommandBar/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,4 +467,4 @@ export const SHORT_CUTS: Record<
467467
keys: ['Enter'],
468468
description: 'Select Item',
469469
},
470-
} as const
470+
}

0 commit comments

Comments
 (0)