import type { RollMode } from "@initiative/domain"; import { ChevronsDown, ChevronsUp } from "lucide-react"; import { useLayoutEffect, useRef, useState } from "react"; import { useClickOutside } from "../hooks/use-click-outside.js"; interface RollModeMenuProps { readonly position: { x: number; y: number }; readonly onSelect: (mode: RollMode) => void; readonly onClose: () => void; } export function RollModeMenu({ position, onSelect, onClose, }: RollModeMenuProps) { const ref = useRef(null); const [pos, setPos] = useState<{ top: number; left: number } | null>(null); useLayoutEffect(() => { const el = ref.current; if (!el) return; const rect = el.getBoundingClientRect(); const vw = document.documentElement.clientWidth; const vh = document.documentElement.clientHeight; let left = position.x; let top = position.y; if (left + rect.width > vw) left = vw - rect.width - 8; if (left < 8) left = 8; if (top + rect.height > vh) top = position.y - rect.height; if (top < 8) top = 8; setPos({ top, left }); }, [position.x, position.y]); useClickOutside(ref, onClose); return (
); }