Commit 7a436b85 authored by isstuev's avatar isstuev

search bar fixes

parent 6d89036a
import { Box, Popover, PopoverTrigger, PopoverContent, PopoverBody, useDisclosure, PopoverFooter } from '@chakra-ui/react'; import { Box, Portal, Popover, PopoverTrigger, PopoverContent, PopoverBody, useDisclosure, PopoverFooter, useOutsideClick } from '@chakra-ui/react';
import _debounce from 'lodash/debounce'; import _debounce from 'lodash/debounce';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
import type { FormEvent, FocusEvent } from 'react'; import type { FormEvent } from 'react';
import React from 'react'; import React from 'react';
import { Element } from 'react-scroll'; import { Element } from 'react-scroll';
...@@ -59,13 +59,15 @@ const SearchBar = ({ isHomepage }: Props) => { ...@@ -59,13 +59,15 @@ const SearchBar = ({ isHomepage }: Props) => {
inputRef.current?.querySelector('input')?.blur(); inputRef.current?.querySelector('input')?.blur();
}, [ onClose ]); }, [ onClose ]);
const handleBlur = React.useCallback((event: FocusEvent<HTMLFormElement>) => { const handleOutsideClick = React.useCallback((event: Event) => {
const isFocusInMenu = menuRef.current?.contains(event.relatedTarget); const isFocusInInput = inputRef.current?.contains(event.target as Node);
const isFocusInInput = inputRef.current?.contains(event.relatedTarget);
if (!isFocusInMenu && !isFocusInInput) { if (!isFocusInInput) {
onClose(); handelHide();
} }
}, [ onClose ]); }, [ handelHide ]);
useOutsideClick({ ref: menuRef, handler: handleOutsideClick });
const handleClear = React.useCallback(() => { const handleClear = React.useCallback(() => {
handleSearchTermChange(''); handleSearchTermChange('');
...@@ -118,53 +120,54 @@ const SearchBar = ({ isHomepage }: Props) => { ...@@ -118,53 +120,54 @@ const SearchBar = ({ isHomepage }: Props) => {
onChange={ handleSearchTermChange } onChange={ handleSearchTermChange }
onSubmit={ handleSubmit } onSubmit={ handleSubmit }
onFocus={ handleFocus } onFocus={ handleFocus }
onBlur={ handleBlur }
onHide={ handelHide } onHide={ handelHide }
onClear={ handleClear } onClear={ handleClear }
isHomepage={ isHomepage } isHomepage={ isHomepage }
value={ searchTerm } value={ searchTerm }
/> />
</PopoverTrigger> </PopoverTrigger>
<PopoverContent <Portal>
w={ `${ menuWidth.current }px` } <PopoverContent
ref={ menuRef } w={ `${ menuWidth.current }px` }
> ref={ menuRef }
<PopoverBody
p={ 0 }
color="chakra-body-text"
> >
<Box <PopoverBody
maxH="50vh" p={ 0 }
overflowY="auto" color="chakra-body-text"
id={ SCROLL_CONTAINER_ID }
ref={ scrollRef }
as={ Element }
px={ 4 }
> >
{ searchTerm.trim().length === 0 && recentSearchKeywords.length > 0 && ( <Box
<SearchBarRecentKeywords onClick={ handleSearchTermChange } onClear={ onClose }/> maxH="50vh"
) } overflowY="auto"
{ searchTerm.trim().length > 0 && ( id={ SCROLL_CONTAINER_ID }
<SearchBarSuggest ref={ scrollRef }
query={ query } as={ Element }
searchTerm={ debouncedSearchTerm } px={ 4 }
onItemClick={ handleItemClick }
containerId={ SCROLL_CONTAINER_ID }
/>
) }
</Box>
</PopoverBody>
{ searchTerm.trim().length > 0 && query.data && query.data.length >= 50 && (
<PopoverFooter>
<LinkInternal
href={ route({ pathname: '/search-results', query: { q: searchTerm } }) }
fontSize="sm"
> >
{ searchTerm.trim().length === 0 && recentSearchKeywords.length > 0 && (
<SearchBarRecentKeywords onClick={ handleSearchTermChange } onClear={ onClose }/>
) }
{ searchTerm.trim().length > 0 && (
<SearchBarSuggest
query={ query }
searchTerm={ debouncedSearchTerm }
onItemClick={ handleItemClick }
containerId={ SCROLL_CONTAINER_ID }
/>
) }
</Box>
</PopoverBody>
{ searchTerm.trim().length > 0 && query.data && query.data.length >= 50 && (
<PopoverFooter>
<LinkInternal
href={ route({ pathname: '/search-results', query: { q: searchTerm } }) }
fontSize="sm"
>
View all results View all results
</LinkInternal> </LinkInternal>
</PopoverFooter> </PopoverFooter>
) } ) }
</PopoverContent> </PopoverContent>
</Portal>
</Popover> </Popover>
); );
}; };
......
...@@ -21,7 +21,7 @@ interface Props { ...@@ -21,7 +21,7 @@ interface Props {
const SearchBarInput = ({ onChange, onSubmit, isHomepage, onFocus, onBlur, onHide, onClear, value }: Props, ref: React.ForwardedRef<HTMLFormElement>) => { const SearchBarInput = ({ onChange, onSubmit, isHomepage, onFocus, onBlur, onHide, onClear, value }: Props, ref: React.ForwardedRef<HTMLFormElement>) => {
const innerRef = React.useRef<HTMLFormElement>(null); const innerRef = React.useRef<HTMLFormElement>(null);
React.useImperativeHandle(ref, () => innerRef.current!, []); React.useImperativeHandle(ref, () => innerRef.current as HTMLFormElement, []);
const [ isSticky, setIsSticky ] = React.useState(false); const [ isSticky, setIsSticky ] = React.useState(false);
const scrollDirection = useScrollDirection(); const scrollDirection = useScrollDirection();
const isMobile = useIsMobile(); const isMobile = useIsMobile();
...@@ -56,9 +56,7 @@ const SearchBarInput = ({ onChange, onSubmit, isHomepage, onFocus, onBlur, onHid ...@@ -56,9 +56,7 @@ const SearchBarInput = ({ onChange, onSubmit, isHomepage, onFocus, onBlur, onHid
return () => { return () => {
window.removeEventListener('scroll', throttledHandleScroll); window.removeEventListener('scroll', throttledHandleScroll);
}; };
// replicate componentDidMount }, [ isMobile, handleScroll ]);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ isMobile ]);
const bgColor = useColorModeValue('white', 'black'); const bgColor = useColorModeValue('white', 'black');
const transformMobile = scrollDirection !== 'down' ? 'translateY(0)' : 'translateY(-100%)'; const transformMobile = scrollDirection !== 'down' ? 'translateY(0)' : 'translateY(-100%)';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment