// SPDX-License-Identifier: AGPL-3.0-or-later // Copyright (C) 2026 Karim Gabriele Varano import { useEffect, useRef, useState } from 'react' import Icon from './Icon' export default function ContextMenu({ x, y, items, onClose }) { const ref = useRef(null) const [pos, setPos] = useState({ left: x, top: y }) // Falls Menue rechts/unten ueberlaufen wuerde, links/oben verschieben useEffect(() => { if (!ref.current) return const rect = ref.current.getBoundingClientRect() const vw = window.innerWidth const vh = window.innerHeight let left = x, top = y if (left + rect.width > vw - 4) left = vw - rect.width - 4 if (top + rect.height > vh - 4) top = vh - rect.height - 4 if (left < 4) left = 4 if (top < 4) top = 4 setPos({ left, top }) }, [x, y]) useEffect(() => { const handleClick = (ev) => { if (ref.current && !ref.current.contains(ev.target)) onClose() } const handleKey = (ev) => { if (ev.key === 'Escape') onClose() } const handleContext = (ev) => { ev.preventDefault(); onClose() } const t = setTimeout(() => { document.addEventListener('mousedown', handleClick) document.addEventListener('keydown', handleKey) document.addEventListener('contextmenu', handleContext) }, 0) return () => { clearTimeout(t) document.removeEventListener('mousedown', handleClick) document.removeEventListener('keydown', handleKey) document.removeEventListener('contextmenu', handleContext) } }, [onClose]) return (