From c75114041358e294214ae734ad1d6199e0f57832 Mon Sep 17 00:00:00 2001 From: Bogdan Carpusor Date: Wed, 7 Aug 2024 20:06:31 +0300 Subject: [PATCH] fix recipe selector --- v2/src/theme/NavbarItem/recipeSelector.js | 64 +++++++++++++++-------- 1 file changed, 43 insertions(+), 21 deletions(-) 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 ( -
+
setOpen(!open)} className={clsx("recipe_selector__dropdown", { open: open })} > {label()}
- +
- Email password + Social / Enterprise Login + + Email password + Social / Enterprise Login +
  • - Email password Login + + Email password Login +
  • - Social / Enterprise Login + Passwordless + + Social / Enterprise Login + Passwordless +
  • - Social / Enterprise Login + + Social / Enterprise Login +
  • - Phone Password Login + + Phone Password Login +
  • @@ -152,14 +170,18 @@ export default function RecipeSelector(props) { active: activeSelector("microservice_auth"), })} > - Microservice Authentication + + Microservice Authentication +
  • - Multi tenancy / organizations + + Multi tenancy / organizations +