import { type Position } from "react-rnd"; import { FC, useCallback, useEffect, useRef, useState } from "react"; import MenuItemEntry from "./MenuItemEntry"; import StyledMenu from "./StyledMenu"; import menuTransition from "./menuTransition"; import { useMenu } from "@/contexts/menu/index"; import { type MenuState } from "@/contexts/menu/useMenuContextState"; import { FOCUSABLE_ELEMENT, ONE_TIME_PASSIVE_EVENT, PREVENT_SCROLL, } from "@/utils/constants"; import { haltEvent, getElementSize, pxToNum } from "@/utils/functions"; import { useMainElements } from "@/contexts/elements"; type MenuProps = { subMenu?: MenuState; }; // eslint-disable-next-line react-refresh/only-export-components export const topLeftPosition = (): Position => ({ x: 0, y: 0, }); const Menu: FC = ({ subMenu }) => { const { menu: baseMenu = {}, setMenu } = useMenu(); const { items, staticX = 0, staticY = 0, x = 0, y = 0, } = subMenu || baseMenu || {}; const [offset, setOffset] = useState(topLeftPosition()); const menuRef = useRef(null); const { WindowsViewArea, WindowsViewAreaSize } = useMainElements(); const resetMenu = useCallback( ({ relatedTarget }: Partial = {}) => { if ( !(relatedTarget instanceof HTMLElement) || !menuRef.current?.contains(relatedTarget) ) { setMenu(Object.create(null) as MenuState); } }, [setMenu] ); const isSubMenu = Boolean(subMenu); const offsetCalculated = useRef>({}); const calculateOffset = useCallback(() => { if ( !menuRef.current || (offsetCalculated.current.x === x && offsetCalculated.current.y === y) ) { return; } offsetCalculated.current = { x, y }; let { height = 0, width = 0, // eslint-disable-next-line prefer-const x: menuX = 0, // eslint-disable-next-line prefer-const y: menuY = 0, } = menuRef.current?.getBoundingClientRect() || {}; let { height: vh, width: vw } = WindowsViewArea ? getElementSize(WindowsViewArea) : WindowsViewAreaSize; const newOffset = { x: 0, y: 0 }; height = pxToNum(height); width = pxToNum(width); vw = pxToNum(vw); vh = pxToNum(vh); if (!staticX) { const subMenuOffscreenX = Boolean(subMenu) && menuX + width > vw; newOffset.x = Math.round(Math.max(0, x + width - vw)) + (subMenuOffscreenX ? Math.round(width + (subMenu?.x || 0)) : 0); const adjustedOffsetX = subMenuOffscreenX && menuX - newOffset.x < 0 ? newOffset.x - (newOffset.x - menuX) : 0; if (adjustedOffsetX > 0) newOffset.x = adjustedOffsetX; } if (!staticY) { const bottomOffset = y + height > vh ? vh - y : 0; const topAdjustedBottomOffset = bottomOffset + height > vh ? 0 : bottomOffset; const subMenuOffscreenY = Boolean(subMenu) && menuY + height > vh; newOffset.y = Math.round(Math.max(0, y + height - (vh - topAdjustedBottomOffset))) + (subMenuOffscreenY ? Math.round(height + (subMenu?.y || 0)) : 0); } setOffset(newOffset); }, [staticX, staticY, subMenu, x, y, WindowsViewArea]); const menuCallbackRef = useCallback( (ref: HTMLElement) => { menuRef.current = ref; calculateOffset(); }, [calculateOffset] ); useEffect(() => { if ((subMenu || baseMenu)?.items && (x || y)) calculateOffset(); }, [baseMenu, calculateOffset, subMenu, x, y]); useEffect(() => { if (items && !subMenu) { const focusedElement = document.activeElement; if ( focusedElement instanceof HTMLElement && focusedElement !== document.body ) { const options: AddEventListenerOptions = { capture: true, ...ONE_TIME_PASSIVE_EVENT, }; const menuUnfocused = ({ relatedTarget, type, }: FocusEvent | MouseEvent): void => { if ( !(relatedTarget instanceof HTMLElement) || !menuRef.current?.contains(relatedTarget) ) { resetMenu(); } focusedElement.removeEventListener( type === "click" ? "blur" : "click", menuUnfocused, { capture: true } ); }; focusedElement.addEventListener("click", menuUnfocused, options); focusedElement.addEventListener("blur", menuUnfocused, options); } else { menuRef.current?.focus(PREVENT_SCROLL); } } }, [items, resetMenu, subMenu]); useEffect(() => { if (!items) offsetCalculated.current = {}; }, [items, offset.x, offset.y, subMenu]); useEffect(() => { const resetOnEscape = ({ key }: KeyboardEvent): void => { if (key === "Escape") resetMenu(); }; if (items) { window.addEventListener("keydown", resetOnEscape, { passive: true }); } return () => window.removeEventListener("keydown", resetOnEscape); }, [items, resetMenu]); return items ? (
    {items.map((item, index) => ( ))}
) : null; }; export default Menu;