Commit 0594ad59 authored by tom's avatar tom

hide suggest when input is hidden on mobile

parent add2a861
...@@ -102,15 +102,22 @@ const SearchResultsPageContent = () => { ...@@ -102,15 +102,22 @@ const SearchResultsPageContent = () => {
); );
})(); })();
const inputRef = React.useRef<HTMLFormElement>(null);
const handelHide = React.useCallback(() => {
inputRef.current?.querySelector('input')?.blur();
}, [ ]);
const renderSearchBar = React.useCallback(() => { const renderSearchBar = React.useCallback(() => {
return ( return (
<SearchBarInput <SearchBarInput
ref={ inputRef }
onChange={ handleSearchTermChange } onChange={ handleSearchTermChange }
onSubmit={ handleSubmit } onSubmit={ handleSubmit }
value={ searchTerm } value={ searchTerm }
onHide={ handelHide }
/> />
); );
}, [ handleSearchTermChange, handleSubmit, searchTerm ]); }, [ handleSearchTermChange, handleSubmit, searchTerm, handelHide ]);
const renderHeader = React.useCallback(() => { const renderHeader = React.useCallback(() => {
return <Header renderSearchBar={ renderSearchBar }/>; return <Header renderSearchBar={ renderSearchBar }/>;
......
...@@ -35,7 +35,7 @@ const Transactions = () => { ...@@ -35,7 +35,7 @@ const Transactions = () => {
]; ];
return ( return (
<Page hideMobileHeaderOnScrollDown> <Page>
<Box h="100%"> <Box h="100%">
<PageTitle text="Transactions"/> <PageTitle text="Transactions"/>
<RoutedTabs <RoutedTabs
......
...@@ -13,7 +13,6 @@ import NavigationDesktop from 'ui/snippets/navigation/NavigationDesktop'; ...@@ -13,7 +13,6 @@ import NavigationDesktop from 'ui/snippets/navigation/NavigationDesktop';
interface Props { interface Props {
children: React.ReactNode; children: React.ReactNode;
wrapChildren?: boolean; wrapChildren?: boolean;
hideMobileHeaderOnScrollDown?: boolean;
isHomePage?: boolean; isHomePage?: boolean;
renderHeader?: () => React.ReactNode; renderHeader?: () => React.ReactNode;
} }
...@@ -21,7 +20,6 @@ interface Props { ...@@ -21,7 +20,6 @@ interface Props {
const Page = ({ const Page = ({
children, children,
wrapChildren = true, wrapChildren = true,
hideMobileHeaderOnScrollDown,
isHomePage, isHomePage,
renderHeader, renderHeader,
}: Props) => { }: Props) => {
...@@ -47,7 +45,7 @@ const Page = ({ ...@@ -47,7 +45,7 @@ const Page = ({
<Flex flexDir="column" flexGrow={ 1 } w={{ base: '100%', lg: 'auto' }}> <Flex flexDir="column" flexGrow={ 1 } w={{ base: '100%', lg: 'auto' }}>
{ renderHeader ? { renderHeader ?
renderHeader() : renderHeader() :
<Header isHomePage={ isHomePage } hideOnScrollDown={ hideMobileHeaderOnScrollDown }/> <Header isHomePage={ isHomePage }/>
} }
<ErrorBoundary renderErrorScreen={ renderErrorScreen }> <ErrorBoundary renderErrorScreen={ renderErrorScreen }>
{ renderedChildren } { renderedChildren }
......
...@@ -13,15 +13,14 @@ import ColorModeToggler from './ColorModeToggler'; ...@@ -13,15 +13,14 @@ import ColorModeToggler from './ColorModeToggler';
type Props = { type Props = {
isHomePage?: boolean; isHomePage?: boolean;
hideOnScrollDown?: boolean;
renderSearchBar?: () => React.ReactNode; renderSearchBar?: () => React.ReactNode;
} }
const Header = ({ hideOnScrollDown, isHomePage, renderSearchBar }: Props) => { const Header = ({ isHomePage, renderSearchBar }: Props) => {
const bgColor = useColorModeValue('white', 'black'); const bgColor = useColorModeValue('white', 'black');
const scrollDirection = useScrollDirection(); const scrollDirection = useScrollDirection();
const searchBar = renderSearchBar ? renderSearchBar() : <SearchBar withShadow={ !hideOnScrollDown }/>; const searchBar = renderSearchBar ? renderSearchBar() : <SearchBar/>;
return ( return (
<> <>
...@@ -40,7 +39,7 @@ const Header = ({ hideOnScrollDown, isHomePage, renderSearchBar }: Props) => { ...@@ -40,7 +39,7 @@ const Header = ({ hideOnScrollDown, isHomePage, renderSearchBar }: Props) => {
zIndex="sticky2" zIndex="sticky2"
transitionProperty="box-shadow" transitionProperty="box-shadow"
transitionDuration="slow" transitionDuration="slow"
boxShadow={ !hideOnScrollDown && scrollDirection === 'down' ? 'md' : 'none' } boxShadow={ scrollDirection === 'down' ? 'md' : 'none' }
> >
<Burger/> <Burger/>
<NetworkLogo/> <NetworkLogo/>
......
import { Popover, PopoverTrigger, PopoverContent, PopoverBody, useDisclosure } from '@chakra-ui/react'; import { Popover, PopoverTrigger, PopoverContent, PopoverBody, useDisclosure } from '@chakra-ui/react';
import _debounce from 'lodash/debounce';
import type { FormEvent, FocusEvent } from 'react'; import type { FormEvent, FocusEvent } from 'react';
import React from 'react'; import React from 'react';
...@@ -10,11 +11,10 @@ import SearchBarSuggest from './SearchBarSuggest'; ...@@ -10,11 +11,10 @@ import SearchBarSuggest from './SearchBarSuggest';
import useSearchQuery from './useSearchQuery'; import useSearchQuery from './useSearchQuery';
type Props = { type Props = {
withShadow?: boolean;
isHomepage?: boolean; isHomepage?: boolean;
} }
const SearchBar = ({ isHomepage, withShadow }: Props) => { const SearchBar = ({ isHomepage }: Props) => {
const { isOpen, onClose, onOpen } = useDisclosure(); const { isOpen, onClose, onOpen } = useDisclosure();
const inputRef = React.useRef<HTMLFormElement>(null); const inputRef = React.useRef<HTMLFormElement>(null);
const menuRef = React.useRef<HTMLDivElement>(null); const menuRef = React.useRef<HTMLDivElement>(null);
...@@ -35,6 +35,11 @@ const SearchBar = ({ isHomepage, withShadow }: Props) => { ...@@ -35,6 +35,11 @@ const SearchBar = ({ isHomepage, withShadow }: Props) => {
onOpen(); onOpen();
}, [ onOpen ]); }, [ onOpen ]);
const handelHide = React.useCallback(() => {
onClose();
inputRef.current?.querySelector('input')?.blur();
}, [ onClose ]);
const handleBlur = React.useCallback((event: FocusEvent<HTMLFormElement>) => { const handleBlur = React.useCallback((event: FocusEvent<HTMLFormElement>) => {
const isFocusInMenu = menuRef.current?.contains(event.relatedTarget); const isFocusInMenu = menuRef.current?.contains(event.relatedTarget);
if (!isFocusInMenu) { if (!isFocusInMenu) {
...@@ -48,10 +53,18 @@ const SearchBar = ({ isHomepage, withShadow }: Props) => { ...@@ -48,10 +53,18 @@ const SearchBar = ({ isHomepage, withShadow }: Props) => {
}, [ menuPaddingX ]); }, [ menuPaddingX ]);
React.useEffect(() => { React.useEffect(() => {
const inputEl = inputRef.current;
if (!inputEl) {
return;
}
calculateMenuWidth(); calculateMenuWidth();
window.addEventListener('resize', calculateMenuWidth);
const resizeHandler = _debounce(calculateMenuWidth, 200);
const resizeObserver = new ResizeObserver(resizeHandler);
resizeObserver.observe(inputRef.current);
return function cleanup() { return function cleanup() {
window.removeEventListener('resize', calculateMenuWidth); resizeObserver.unobserve(inputEl);
}; };
}, [ calculateMenuWidth ]); }, [ calculateMenuWidth ]);
...@@ -71,8 +84,8 @@ const SearchBar = ({ isHomepage, withShadow }: Props) => { ...@@ -71,8 +84,8 @@ const SearchBar = ({ isHomepage, withShadow }: Props) => {
onSubmit={ handleSubmit } onSubmit={ handleSubmit }
onFocus={ handleFocus } onFocus={ handleFocus }
onBlur={ handleBlur } onBlur={ handleBlur }
onHide={ handelHide }
isHomepage={ isHomepage } isHomepage={ isHomepage }
withShadow={ withShadow }
value={ searchTerm } value={ searchTerm }
/> />
</PopoverTrigger> </PopoverTrigger>
......
...@@ -12,12 +12,12 @@ interface Props { ...@@ -12,12 +12,12 @@ interface Props {
onSubmit: (event: FormEvent<HTMLFormElement>) => void; onSubmit: (event: FormEvent<HTMLFormElement>) => void;
onBlur?: (event: FocusEvent<HTMLFormElement>) => void; onBlur?: (event: FocusEvent<HTMLFormElement>) => void;
onFocus?: () => void; onFocus?: () => void;
onHide?: () => void;
isHomepage?: boolean; isHomepage?: boolean;
withShadow?: boolean;
value: string; value: string;
} }
const SearchBarInput = ({ onChange, onSubmit, isHomepage, onFocus, onBlur, withShadow, value }: Props, ref: React.ForwardedRef<HTMLFormElement>) => { const SearchBarInput = ({ onChange, onSubmit, isHomepage, onFocus, onBlur, onHide, value }: Props, ref: React.ForwardedRef<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();
...@@ -28,7 +28,7 @@ const SearchBarInput = ({ onChange, onSubmit, isHomepage, onFocus, onBlur, withS ...@@ -28,7 +28,7 @@ const SearchBarInput = ({ onChange, onSubmit, isHomepage, onFocus, onBlur, withS
} else { } else {
setIsSticky(false); setIsSticky(false);
} }
}, []); }, [ ]);
React.useEffect(() => { React.useEffect(() => {
if (!isMobile || isHomepage) { if (!isMobile || isHomepage) {
...@@ -48,6 +48,12 @@ const SearchBarInput = ({ onChange, onSubmit, isHomepage, onFocus, onBlur, withS ...@@ -48,6 +48,12 @@ const SearchBarInput = ({ onChange, onSubmit, isHomepage, onFocus, onBlur, withS
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%)';
React.useEffect(() => {
if (isMobile && scrollDirection === 'down') {
onHide?.();
}
}, [ scrollDirection, onHide, isMobile ]);
return ( return (
<chakra.form <chakra.form
ref={ ref } ref={ ref }
...@@ -65,7 +71,7 @@ const SearchBarInput = ({ onChange, onSubmit, isHomepage, onFocus, onBlur, withS ...@@ -65,7 +71,7 @@ const SearchBarInput = ({ onChange, onSubmit, isHomepage, onFocus, onBlur, withS
paddingX={{ base: isHomepage ? 0 : 4, lg: 0 }} paddingX={{ base: isHomepage ? 0 : 4, lg: 0 }}
paddingTop={{ base: isHomepage ? 0 : 1, lg: 0 }} paddingTop={{ base: isHomepage ? 0 : 1, lg: 0 }}
paddingBottom={{ base: isHomepage ? 0 : 4, lg: 0 }} paddingBottom={{ base: isHomepage ? 0 : 4, lg: 0 }}
boxShadow={ withShadow && scrollDirection !== 'down' && isSticky ? 'md' : 'none' } boxShadow={ scrollDirection !== 'down' && isSticky ? 'md' : 'none' }
transform={{ base: isHomepage ? 'none' : transformMobile, lg: 'none' }} transform={{ base: isHomepage ? 'none' : transformMobile, lg: 'none' }}
transitionProperty="transform,box-shadow" transitionProperty="transform,box-shadow"
transitionDuration="slow" transitionDuration="slow"
......
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