diff --git a/v2/src/theme/NavbarItem/recipeSelector.js b/v2/src/theme/NavbarItem/recipeSelector.js index b6bd1c262..68d476ea0 100644 --- a/v2/src/theme/NavbarItem/recipeSelector.js +++ b/v2/src/theme/NavbarItem/recipeSelector.js @@ -1,13 +1,13 @@ -import React, { useState, useEffect } from "react"; +import React, { useCallback, useState, useEffect, useRef } from "react"; import clsx from "clsx"; import CloseIcon from "../../../static/img/recipe_selector_close.png"; -import OpenIcon from "../../../static/img/recipe_selector_open.png"; -import { useLocation } from '@docusaurus/router'; +import { useLocation } from "@docusaurus/router"; export default function RecipeSelector(props) { - const { pathname } = useLocation() + const { pathname } = useLocation(); const currDocs = pathname.split("/")[2]; + const ref = useRef(null); const [open, setOpen] = useState(false); const activeSelector = (id) => { return id === currDocs; @@ -42,25 +42,33 @@ export default function RecipeSelector(props) { return "Select Recipe"; } }; + const closeDropdownMenuOnOusideClick = useCallback((e) => { + if (ref.current && !ref.current.contains(e.target)) { + setOpen(false); + } + }, []); + useEffect(() => { - const closeDropDown = (e) => { - if (e.target.classList[0]?.startsWith("recipe_selector")) { - // no-op - } else { - setOpen(false); - } - }; - window.addEventListener("click", closeDropDown); + window.addEventListener("click", closeDropdownMenuOnOusideClick); + return () => + window.removeEventListener("click", closeDropdownMenuOnOusideClick); }, []); + return ( -