|
| 1 | +import { ReactNode, useEffect, useRef } from "react"; |
| 2 | +import { NavLink } from "react-router-dom"; |
| 3 | +import { twMerge } from "tailwind-merge"; |
| 4 | +import { Button } from "../ui/button/Button"; |
| 5 | +import { LinkOption } from "../../types/linkOptionsType"; |
| 6 | + |
| 7 | +type ControlPanelTypes = { |
| 8 | + classNames?: string; |
| 9 | + options: LinkOption[]; |
| 10 | + children: ReactNode; |
| 11 | + openMenu: boolean; |
| 12 | + setOpenMenu: (open: boolean) => void; |
| 13 | +}; |
| 14 | + |
| 15 | +export const DropdownMenu = ({ |
| 16 | + classNames, |
| 17 | + options, |
| 18 | + children, |
| 19 | + openMenu, |
| 20 | + setOpenMenu, |
| 21 | +}: ControlPanelTypes) => { |
| 22 | + const wrapperRef = useRef<HTMLDivElement | null>(null); |
| 23 | + |
| 24 | + useEffect(() => { |
| 25 | + if (!openMenu) return; |
| 26 | + |
| 27 | + const onPointerDown = (e: PointerEvent) => { |
| 28 | + const el = wrapperRef.current; |
| 29 | + if (!el) { |
| 30 | + return; |
| 31 | + } |
| 32 | + if (!el.contains(e.target as Node)) { |
| 33 | + setOpenMenu(false); |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + document.addEventListener("pointerdown", onPointerDown); |
| 38 | + return () => document.removeEventListener("pointerdown", onPointerDown); |
| 39 | + }, [openMenu]); |
| 40 | + |
| 41 | + return ( |
| 42 | + <div |
| 43 | + ref={wrapperRef} |
| 44 | + className={twMerge( |
| 45 | + "hidden md:flex relative flex-col items-end", |
| 46 | + classNames, |
| 47 | + )} |
| 48 | + > |
| 49 | + {children} |
| 50 | + <div |
| 51 | + className={[ |
| 52 | + "absolute top-[130%] right-0 z-400 flex flex-col items-start rounded-2xl border border-white/10 bg-[#15141D] shadow-[0_12px_40px_rgba(0,0,0,0.35)] overflow-hidden", |
| 53 | + "min-w-40 w-max", |
| 54 | + "opacity-0 invisible transition-opacity duration-300 ease-in-out", |
| 55 | + openMenu && "opacity-100 visible", |
| 56 | + ] |
| 57 | + .filter(Boolean) |
| 58 | + .join(" ")} |
| 59 | + > |
| 60 | + {options.map((option) => { |
| 61 | + const Icon = option.icon; |
| 62 | + |
| 63 | + const content = ( |
| 64 | + <div className="flex items-center gap-2.5 text-light-100"> |
| 65 | + {Icon && <Icon className="w-5 h-5 shrink-0" />} |
| 66 | + <span>{option.title}</span> |
| 67 | + </div> |
| 68 | + ); |
| 69 | + |
| 70 | + return option.link ? ( |
| 71 | + <Button |
| 72 | + key={option.id} |
| 73 | + as={NavLink} |
| 74 | + className="flex justify-start w-full rounded-none p-2.5 transition-none |
| 75 | + hover:text-(--color-dark-900) hover:bg-gray-400" |
| 76 | + to={option.link} |
| 77 | + variant="link" |
| 78 | + onClick={() => setOpenMenu(false)} |
| 79 | + > |
| 80 | + {content} |
| 81 | + </Button> |
| 82 | + ) : ( |
| 83 | + <Button |
| 84 | + key={option.id} |
| 85 | + className="flex justify-start w-full rounded-none p-2.5 transition-none |
| 86 | + hover:text-(--color-dark-900) hover:bg-gray-400" |
| 87 | + variant="link" |
| 88 | + onClick={() => { |
| 89 | + option.actionCallback?.(); |
| 90 | + setOpenMenu(false); |
| 91 | + }} |
| 92 | + > |
| 93 | + {content} |
| 94 | + </Button> |
| 95 | + ); |
| 96 | + })} |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + ); |
| 100 | +}; |
0 commit comments