Skip to content

Commit

Permalink
Search shortcut
Browse files Browse the repository at this point in the history
Fixes #2265
  • Loading branch information
tom2drum committed Oct 21, 2024
1 parent 2ed8133 commit c2d944d
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 6 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions ui/snippets/searchBar/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ const SearchBar = ({ isHomepage }: Props) => {
<PopoverContent
w={ `${ menuWidth.current }px` }
ref={ menuRef }
overflow="hidden"
>
<PopoverBody
p={ 0 }
Expand Down
70 changes: 64 additions & 6 deletions ui/snippets/searchBar/SearchBarInput.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InputGroup, Input, InputLeftElement, chakra, useColorModeValue, forwardRef, InputRightElement } from '@chakra-ui/react';
import { InputGroup, Input, InputLeftElement, chakra, useColorModeValue, forwardRef, InputRightElement, Center } from '@chakra-ui/react';
import throttle from 'lodash/throttle';
import React from 'react';
import type { ChangeEvent, FormEvent, FocusEvent } from 'react';
Expand Down Expand Up @@ -62,9 +62,69 @@ const SearchBarInput = (
};
}, [ isMobile, handleScroll ]);

const handleKeyPress = React.useCallback((event: KeyboardEvent) => {
if (isMobile) {
return;
}

switch (event.key) {
case '/': {
if ([ 'INPUT', 'TEXTAREA' ].includes((event.target as HTMLElement).tagName)) {
break;
}

if (!isSuggestOpen) {
event.preventDefault();
innerRef.current?.querySelector('input')?.focus();
onFocus?.();
}
break;
}
case 'Escape': {
if (isSuggestOpen) {
innerRef.current?.querySelector('input')?.blur();
onHide?.();
}
break;
}
}
}, [ isMobile, isSuggestOpen, onFocus, onHide ]);

React.useEffect(() => {
window.addEventListener('keydown', handleKeyPress);
return () => {
window.removeEventListener('keydown', handleKeyPress);
};
}, [ handleKeyPress ]);

const bgColor = useColorModeValue('white', 'black');
const transformMobile = scrollDirection !== 'down' ? 'translateY(0)' : 'translateY(-100%)';

const rightElement = (() => {
if (value) {
return <ClearButton onClick={ onClear }/>;
}

if (isMobile) {
return null;
}

return (
<Center
boxSize="20px"
my="2px"
mr={{ base: 1, lg: isHomepage ? 2 : 1 }}
borderRadius="sm"
borderWidth="1px"
borderColor="gray.400"
color="gray.400"
display={{ base: 'none', lg: 'flex' }}
>
/
</Center>
);
})();

return (
<chakra.form
ref={ innerRef }
Expand Down Expand Up @@ -111,11 +171,9 @@ const SearchBarInput = (
color={ useColorModeValue('black', 'white') }
value={ value }
/>
{ value && (
<InputRightElement top={{ base: 2, lg: isHomepage ? 3 : 2 }} right={ 2 }>
<ClearButton onClick={ onClear }/>
</InputRightElement>
) }
<InputRightElement top={{ base: 2, lg: isHomepage ? 3 : 2 }} right={ 2 }>
{ rightElement }
</InputRightElement>
</InputGroup>
</chakra.form>
);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c2d944d

Please sign in to comment.