Skip to content

Commit

Permalink
feat: focusManager on recents shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
juliajforesti committed Oct 17, 2024
1 parent fa3b086 commit bef1cd3
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions apps/meteor/client/sidebarv2/header/SearchSection.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { useFocusManager } from '@react-aria/focus';
import { css } from '@rocket.chat/css-in-js';
import { Box, Icon, TextInput, Palette, SidebarV2Section, IconButton } from '@rocket.chat/fuselage';
import { useMergedRefs, useOutsideClick } from '@rocket.chat/fuselage-hooks';
import { useTranslation, useUser } from '@rocket.chat/ui-contexts';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { FocusScope } from 'react-aria';
import { useForm } from 'react-hook-form';
import tinykeys from 'tinykeys';

Expand Down Expand Up @@ -48,8 +50,11 @@ const shortcut = ((): string => {
return '(Ctrl+K)';
})();

const isRecentButton = (node: EventTarget) => (node as HTMLElement).title === 'Recent';

const SearchSection = () => {
const t = useTranslation();
const focusManager = useFocusManager();
const user = useUser();
const [showRecentSearch, setShowRecent] = useState(false);

Expand Down Expand Up @@ -81,6 +86,12 @@ const SearchSection = () => {
}
}, [filterText]);

useEffect(() => {
if (showRecentSearch) {
focusManager.focusNext({ accept: (node) => isRecentButton(node) });
}
}, [focusManager, showRecentSearch]);

useEffect(() => {
const unsubscribe = tinykeys(window, {
'$mod+K': (event) => {
Expand All @@ -96,7 +107,6 @@ const SearchSection = () => {
'Shift+$mod+K': (event) => {
event.preventDefault();
setShowRecent(true);
setFocus('filterText');
},
'Escape': (event) => {
event.preventDefault();
Expand Down Expand Up @@ -139,7 +149,9 @@ const SearchSection = () => {
)}
</SidebarV2Section>
{(isDirty || showRecentSearch) && (
<SearchList filterText={filterText} onEscSearch={handleEscSearch} showRecentSearch={showRecentSearch} />
<FocusScope>
<SearchList filterText={filterText} onEscSearch={handleEscSearch} showRecentSearch={showRecentSearch} />
</FocusScope>
)}
</Box>
);
Expand Down

0 comments on commit bef1cd3

Please sign in to comment.