Skip to content

Commit

Permalink
fix: close mobile filter dropdown when menu opens
Browse files Browse the repository at this point in the history
  • Loading branch information
CraigMcCahill committed Dec 2, 2022
1 parent c524e81 commit 2a30016
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/components/MobileBlogFilters/MobileBlogFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC, useState, useRef, useEffect, useId } from "react";
import { FC, useState, useRef, useEffect, useId, useCallback } from "react";

import Box from "../Box";
import Button from "../Button";
Expand All @@ -10,6 +10,7 @@ import PostCategoryList, {
PostCategoryListProps,
PostCategoryPage,
} from "../Posts/PostCategoryList/PostCategoryList";
import { useMenuContext } from "../../context/Menu";

export type MobileBlogFiltersProps = {
categoryListProps: Omit<PostCategoryListProps, "labelledBy" | "page">;
Expand All @@ -19,6 +20,10 @@ export type MobileBlogFiltersProps = {
const MobileBlogFilters: FC<MobileBlogFiltersProps> = (props) => {
const [isOpen, setIsOpen] = useState(false);
const [categoryListHeight, setCategoryListHeight] = useState<number>(0);

const { open } = useMenuContext();
const menuOpen = open; //rename here to save confusion

const categoryListRef = useRef<HTMLDivElement>(null);
const checkAndSetHeight = () => {
if (categoryListRef.current) {
Expand All @@ -37,9 +42,16 @@ const MobileBlogFilters: FC<MobileBlogFiltersProps> = (props) => {
const menuId = useId();
const triggerId = useId();

const close = () => {
const close = useCallback(() => {
setIsOpen(false);
};
}, [setIsOpen]);

// Close the dropdown if the menu is open
useEffect(() => {
if (isOpen && menuOpen) {
close();
}
}, [isOpen, menuOpen, close]);

return (
<Flex
Expand Down

0 comments on commit 2a30016

Please sign in to comment.