import React from "react"; /** * Modal dialog — role="dialog" aria-modal, focus trap, Esc to close, * backdrop click, focus restored to the opener on unmount. * R02: 실제 Tab 이동 가능한 요소만 trap 대상으로 계산. 유효 요소가 없으면 패널에 포커스 유지. * R16: onClose는 ref로 최신값을 읽고, 초기 포커스·스크롤 잠금의 수명은 open에만 연결. */ const modalStacks = new WeakMap(); function isTabbable(el) { if (el.matches(":disabled") || el.hidden || el.tabIndex < 0) return false; if (el.closest("[inert], [hidden], [aria-hidden=\"true\"]") || getComputedStyle(el).visibility !== "visible") return false; if (!(el.offsetWidth || el.offsetHeight || el.getClientRects().length)) return false; return true; } export function Modal({ open, onClose, title, description, footer, size = "md", children, style }) { const panelRef = React.useRef(null); const openerRef = React.useRef(null); const onCloseRef = React.useRef(onClose); React.useEffect(() => { onCloseRef.current = onClose; }); const titleId = React.useId(); const descId = React.useId(); const widths = { sm: 400, md: 520, lg: 720 }; React.useEffect(() => { if (!open) return; openerRef.current = document.activeElement; let state = modalStacks.get(document); if (!state) { state = { items: [], overflow: document.body.style.overflow, opener: document.activeElement }; modalStacks.set(document, state); } const entry = { panel: panelRef.current, opener: openerRef.current }; const descendant = state.items.findIndex(item => entry.panel?.contains(item.panel)); if (descendant < 0) state.items.push(entry); else state.items.splice(descendant, 0, entry); document.body.style.overflow = "hidden"; const focusables = () => Array.from(panelRef.current?.querySelectorAll('a[href],button,textarea,input,select,[tabindex]') || []).filter(isTabbable).sort((a, b) => (a.tabIndex > 0 ? a.tabIndex : Infinity) - (b.tabIndex > 0 ? b.tabIndex : Infinity)); const first = focusables()[0] || panelRef.current; if (state.items[state.items.length - 1] === entry) first?.focus(); const onKey = (e) => { if (state.items[state.items.length - 1] !== entry || e.defaultPrevented) return; if (e.key === "Escape") { e.preventDefault(); e.stopPropagation(); onCloseRef.current?.(); return; } if (e.key !== "Tab") return; const f = focusables(); if (!f.length) { e.preventDefault(); panelRef.current?.focus(); return; } const a = f[0], z = f[f.length - 1]; const inside = f.includes(document.activeElement); if (e.shiftKey && (document.activeElement === a || !inside)) { e.preventDefault(); z.focus(); } else if (!e.shiftKey && (document.activeElement === z || !inside)) { e.preventDefault(); a.focus(); } }; document.addEventListener("keydown", onKey); return () => { document.removeEventListener("keydown", onKey); const wasTop = state.items[state.items.length - 1] === entry; state.items = state.items.filter(item => item !== entry); if (!state.items.length) { document.body.style.overflow = state.overflow; modalStacks.delete(document); } if (wasTop) { const parent = state.items[state.items.length - 1]; const opener = entry.opener?.isConnected ? entry.opener : state.opener; if (!parent || parent.panel?.contains(opener)) opener?.focus?.(); else (Array.from(parent.panel?.querySelectorAll('button,input,select,textarea,a[href],[tabindex]') || []).find(isTabbable) || parent.panel)?.focus(); } }; }, [open]); if (!open) return null; return (
{description}
}