Skip to content

Commit

Permalink
fix scroll issue in mobile search (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sherange authored May 31, 2024
1 parent 8efa1b6 commit ef166b9
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/web-shared/components/Searchbar/Searchbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const Searchbar = (props = {}) => {
const userExist = !!currentUser;
const firstName = currentUser?.profile?.firstName || '';
const [isMobile, setIsMobile] = useState(false);
const [scrollPosition, setScrollPosition] = useState(0);

const textWelcome =
firstName === '' ? <strong>Hey!&nbsp;</strong> : <strong>Hey {firstName}!&nbsp; </strong>;
Expand Down Expand Up @@ -94,6 +95,27 @@ const Searchbar = (props = {}) => {
}
}, [autocompleteState.isOpen]);

useEffect(() => {
if (isMobile && autocompleteState.isOpen) {
// Save the current scroll position
setScrollPosition(window.scrollY);

// Set body to fixed position to prevent scrolling
document.body.style.position = 'fixed';
} else {
// Restore body to normal position
document.body.style.position = '';

// Restore the scroll position
window.scrollTo(0, scrollPosition);
}

// Clean up the effect when the component unmounts
return () => {
document.body.style.position = '';
};
}, [autocompleteState.isOpen, scrollPosition, isMobile]);

const handleGetAutocompleteInstance = (instance) => {
setAutocompleteInstance(instance);
};
Expand Down

0 comments on commit ef166b9

Please sign in to comment.