| 1 | import React, { useEffect, useLayoutEffect, useRef } from "react"; |
| 2 | |
| 3 | export function Modal({ title, children, onClose, wide = false, footer, anchor }) { |
| 4 | const panel = useRef(), |
| 5 | closeRef = useRef(onClose); |
| 6 | closeRef.current = onClose; |
| 7 | useLayoutEffect(() => { |
| 8 | if (!anchor) return; |
| 9 | const node = panel.current; |
| 10 | anchor.setAttribute("aria-expanded", "true"); |
| 11 | const position = () => { |
| 12 | if (!anchor.isConnected) { |
| 13 | closeRef.current?.(); |
| 14 | return; |
| 15 | } |
| 16 | const rect = anchor.getBoundingClientRect(); |
| 17 | const box = node.getBoundingClientRect(); |
| 18 | const left = Math.max(8, Math.min(rect.right - box.width, window.innerWidth - box.width - 8)); |
| 19 | const below = rect.bottom + 6; |
| 20 | const top = below + box.height <= window.innerHeight - 8 |
| 21 | ? below : Math.max(8, rect.top - box.height - 6); |
| 22 | node.style.left = `${left}px`; |
| 23 | node.style.top = `${top}px`; |
| 24 | }; |
| 25 | position(); |
| 26 | const observer = new ResizeObserver(position); |
| 27 | observer.observe(node); |
| 28 | window.addEventListener("resize", position); |
| 29 | document.addEventListener("scroll", position, true); |
| 30 | return () => { |
| 31 | anchor.focus(); |
| 32 | anchor.removeAttribute("aria-expanded"); |
| 33 | observer.disconnect(); |
| 34 | window.removeEventListener("resize", position); |
| 35 | document.removeEventListener("scroll", position, true); |
| 36 | }; |
| 37 | }, [anchor]); |
| 38 | useEffect(() => { |
| 39 | const previous = anchor || document.activeElement; |
| 40 | const first = panel.current.querySelector( |
| 41 | "input:not([disabled]),button:not([disabled]),select:not([disabled])", |
| 42 | ); |
| 43 | first?.focus(); |
| 44 | const key = (e) => { |
| 45 | if ( |
| 46 | [...document.querySelectorAll('[role="dialog"]')].at(-1) !== |
| 47 | panel.current |
| 48 | ) |
| 49 | return; |
| 50 | if (e.key === "Escape") { |
| 51 | if (e.target.closest('[role="combobox"][aria-expanded="true"]')) return; |
| 52 | e.preventDefault(); |
| 53 | e.stopPropagation(); |
| 54 | closeRef.current?.(); |
| 55 | } |
| 56 | if (e.key === "Tab") { |
| 57 | const nodes = [ |
| 58 | ...panel.current.querySelectorAll( |
| 59 | "input:not([disabled]),button:not([disabled]),select:not([disabled]),textarea:not([disabled]),a[href]", |
| 60 | ), |
| 61 | ].filter((n) => n.offsetParent !== null); |
| 62 | if (!nodes.length) return; |
| 63 | const index = nodes.indexOf(document.activeElement); |
| 64 | if (e.shiftKey && index <= 0) { |
| 65 | e.preventDefault(); |
| 66 | nodes.at(-1).focus(); |
| 67 | } else if (!e.shiftKey && (index === nodes.length - 1 || index < 0)) { |
| 68 | e.preventDefault(); |
| 69 | nodes[0].focus(); |
| 70 | } |
| 71 | } |
| 72 | }; |
| 73 | const closeEvent = () => { |
| 74 | if ( |
| 75 | [...document.querySelectorAll('[role="dialog"]')].at(-1) === |
| 76 | panel.current |
| 77 | ) |
| 78 | closeRef.current?.(); |
| 79 | }; |
| 80 | document.addEventListener("cage-close-modal", closeEvent); |
| 81 | document.addEventListener("keydown", key, true); |
| 82 | return () => { |
| 83 | document.removeEventListener("keydown", key, true); |
| 84 | document.removeEventListener("cage-close-modal", closeEvent); |
| 85 | previous?.focus?.(); |
| 86 | }; |
| 87 | }, []); |
| 88 | return ( |
| 89 | <div |
| 90 | className={`modal-scrim${anchor ? " anchored-modal-scrim" : ""}`} |
| 91 | onMouseDown={(e) => { |
| 92 | if (e.target === e.currentTarget) onClose?.(); |
| 93 | }} |
| 94 | > |
| 95 | <section |
| 96 | ref={panel} |
| 97 | className={`modal ${wide ? "wide" : ""}`} |
| 98 | role="dialog" |
| 99 | aria-modal="true" |
| 100 | aria-label={title} |
| 101 | > |
| 102 | <header className="modal-head"> |
| 103 | <h2>{title}</h2> |
| 104 | {onClose && ( |
| 105 | <button |
| 106 | className="icon-button close-modal" |
| 107 | aria-label="关闭面板" |
| 108 | onClick={onClose} |
| 109 | > |
| 110 | × |
| 111 | </button> |
| 112 | )} |
| 113 | </header> |
| 114 | {children} |
| 115 | {footer && <footer className="modal-footer">{footer}</footer>} |
| 116 | </section> |
| 117 | </div> |
| 118 | ); |
| 119 | } |