Commit 6d89036a authored by isstuev's avatar isstuev

fix search hide on scroll bug

parent 2605cd63
...@@ -20,25 +20,33 @@ interface Props { ...@@ -20,25 +20,33 @@ 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);
React.useImperativeHandle(ref, () => innerRef.current!, []);
const [ isSticky, setIsSticky ] = React.useState(false); const [ isSticky, setIsSticky ] = React.useState(false);
const scrollDirection = useScrollDirection(); const scrollDirection = useScrollDirection();
const isMobile = useIsMobile(); const isMobile = useIsMobile();
const handleScroll = React.useCallback(() => { const handleScroll = React.useCallback(() => {
const TOP_BAR_HEIGHT = 36; const TOP_BAR_HEIGHT = 36;
if (window.pageYOffset >= TOP_BAR_HEIGHT) { if (!isHomepage) {
setIsSticky(true); if (window.scrollY >= TOP_BAR_HEIGHT) {
} else { setIsSticky(true);
setIsSticky(false); } else {
setIsSticky(false);
}
} }
}, [ ]);
if (isMobile && innerRef?.current?.getBoundingClientRect() && innerRef?.current?.getBoundingClientRect().y < TOP_BAR_HEIGHT) {
onHide?.();
}
}, [ isMobile, onHide, isHomepage ]);
const handleChange = React.useCallback((event: ChangeEvent<HTMLInputElement>) => { const handleChange = React.useCallback((event: ChangeEvent<HTMLInputElement>) => {
onChange(event.target.value); onChange(event.target.value);
}, [ onChange ]); }, [ onChange ]);
React.useEffect(() => { React.useEffect(() => {
if (!isMobile || isHomepage) { if (!isMobile) {
return; return;
} }
const throttledHandleScroll = throttle(handleScroll, 300); const throttledHandleScroll = throttle(handleScroll, 300);
...@@ -55,15 +63,9 @@ const SearchBarInput = ({ onChange, onSubmit, isHomepage, onFocus, onBlur, onHid ...@@ -55,15 +63,9 @@ const SearchBarInput = ({ onChange, onSubmit, isHomepage, onFocus, onBlur, onHid
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={ innerRef }
noValidate noValidate
onSubmit={ onSubmit } onSubmit={ onSubmit }
onBlur={ onBlur } onBlur={ onBlur }
......
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