MCPcopy
hub / github.com/fccview/jotty / DropdownMenu

Function DropdownMenu

app/_components/GlobalComponents/Dropdowns/DropdownMenu.tsx:22–132  ·  view source on GitHub ↗
({
  trigger,
  items,
  align = "left",
}: DropdownMenuProps)

Source from the content-addressed store, hash-verified

20}
21
22export const DropdownMenu = ({
23 trigger,
24 items,
25 align = "left",
26}: DropdownMenuProps) => {
27 const [isOpen, setIsOpen] = useState(false);
28 const [openUpward, setOpenUpward] = useState(false);
29 const dropdownRef = useRef<HTMLDivElement>(null);
30 const triggerRef = useRef<HTMLDivElement>(null);
31
32 useEffect(() => {
33 const handleClickOutside = (event: MouseEvent) => {
34 if (
35 dropdownRef.current &&
36 !dropdownRef.current.contains(event.target as Node)
37 ) {
38 setIsOpen(false);
39 }
40 };
41 const handleEscape = (event: KeyboardEvent) => {
42 if (event.key === "Escape") {
43 setIsOpen(false);
44 }
45 };
46
47 if (isOpen) {
48 document.addEventListener("mousedown", handleClickOutside);
49 document.addEventListener("keydown", handleEscape);
50 }
51
52 return () => {
53 document.removeEventListener("mousedown", handleClickOutside);
54 document.removeEventListener("keydown", handleEscape);
55 };
56 }, [isOpen]);
57
58 const handleItemClick = (itemOnClick: () => void) => {
59 itemOnClick();
60 setIsOpen(false);
61 };
62
63 const handleToggle = () => {
64 if (!isOpen && triggerRef.current) {
65 const rect = triggerRef.current.getBoundingClientRect();
66
67 let scrollParent: HTMLElement | null = triggerRef.current.parentElement;
68 while (scrollParent) {
69 if (scrollParent.classList.contains('jotty-sidebar-categories') || scrollParent.classList.contains('jotty-sidebar-tags')) {
70 break;
71 }
72 scrollParent = scrollParent.parentElement;
73 }
74
75 if (scrollParent) {
76 const containerRect = scrollParent.getBoundingClientRect();
77 const actualSpaceBelow = containerRect.bottom - rect.bottom;
78 const threshold = 200;
79

Callers

nothing calls this directly

Calls 2

cnFunction · 0.90
handleItemClickFunction · 0.70

Tested by

no test coverage detected