Skip to content

Commit

Permalink
[fix] close search bar on navigation back event
Browse files Browse the repository at this point in the history
  • Loading branch information
cupoftea4 committed Nov 17, 2024
1 parent 74a9636 commit be3150e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/features/header/HomeHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ const HeaderPanel: FC<OwnProps> = ({ timetableType, className }) => {
const { isFocused, blur, focus } = useDatalistFocus();
const [showSearchBar, setShowSearchBar] = useState(!shouldShrinkSearchBar || isFocused);

useEffect(() => {
const onPopstate = () => setShowSearchBar(false);
window.addEventListener("popstate", onPopstate);
return () => {
window.removeEventListener("popstate", onPopstate);
};
}, []);

useEffect(() => {
if (isFocused) {
setShowSearchBar(true);
Expand Down
5 changes: 3 additions & 2 deletions src/pages/HomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ const HomePage: FC<OwnProps> = ({ timetableType }) => {
t && navigate(t);
});
}
window.onpopstate = () => {
const onPopstate = () => {
setSelectedSecond(null);
};
window.addEventListener("popstate", onPopstate);
return () => {
window.onpopstate = null;
window.removeEventListener("popstate", onPopstate);
};
}, [force, navigate, timetableType]);

Expand Down

0 comments on commit be3150e

Please sign in to comment.