-
Notifications
You must be signed in to change notification settings - Fork 0
/
DropMenu.jsx
74 lines (69 loc) · 2.48 KB
/
DropMenu.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { createContext, useContext, useState } from "react";
import { IoMdArrowDropdown, IoMdArrowDropup } from "react-icons/io";
import { useCloseModal } from "../hooks/useCloseModal";
import { createPortal } from "react-dom";
import { motion } from "framer-motion";
import UserAvatar from "../features/authentication/UserAvatar";
const MenuContext = createContext();
function DropMenu({ children }) {
const [isOpen, setIsOpen] = useState("");
const closeMenu = () => setIsOpen("");
const openMenu = (id) => setIsOpen(id);
return (
<MenuContext.Provider value={{ isOpen, setIsOpen, closeMenu, openMenu }}>
{children}
</MenuContext.Provider>
);
}
function Toggle({ id }) {
const { isOpen, closeMenu, openMenu } = useContext(MenuContext);
function handleClick(e) {
e.stopPropagation();
//we do not want the document or any parent to recieve this event we want it to only belong to this button no bubbling no capturing down
//cause we do not want to check on the click on the document if the clicked is button so we do not close
//it is easier to not read this event by document at all
isOpen === "" || isOpen !== id ? openMenu(id) : closeMenu();
}
function handleMouseEnter(e) {
e.stopPropagation();
openMenu(id);
}
return (
<section
className=" cursor-pointer select-none flex items-center gap-[.1rem] text-xl relative"
onMouseOver={(e) => handleMouseEnter(e)}
>
<UserAvatar />
{
<IoMdArrowDropdown
style={{ transition: "100ms" }}
className={`${isOpen && "rotate-180 transition-all duration-100"}`}
onClick={(e) => handleClick(e)}
/>
}
</section>
);
}
function Menu({ children, id }) {
const { isOpen, closeMenu } = useContext(MenuContext);
const ref = useCloseModal(closeMenu, false);
if (isOpen !== id) return;
return createPortal(
<div ref={ref} className="fixed right-6 top-10 z-50 flex flex-col ">
<IoMdArrowDropup className=" text-center text-xl items-center m-auto" />
<motion.ul
initial={{ opacity: 0 }}
whileInView={{ opacity: 1 }}
transition={{ duration: 0.3 }}
className={` mt-2 flex flex-col bg-black bg-opacity-40 py-2 px-6 gap-2 text-lg text-gray-200
z-50 shadow-md `}
>
{children}
</motion.ul>
</div>,
document.body
);
}
DropMenu.Toggle = Toggle;
DropMenu.Menu = Menu;
export default DropMenu;