douyinWEB/frontend/src/components/Modal.tsx
eric 60ea916a4d feat: 全站 H5 响应式导航与可选 PWA 安装
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-14 22:19:06 -05:00

66 lines
1.8 KiB
TypeScript

"use client";
import { useEffect, useState, type ReactNode } from "react";
import { createPortal } from "react-dom";
type ModalProps = {
open: boolean;
onClose: () => void;
children: ReactNode;
/** Panel wrapper classes (the dialog card). */
panelClassName?: string;
zIndex?: number;
};
/**
* Viewport-locked overlay. Always portals to document.body so parent
* transforms (e.g. Shell `.page-enter`) cannot trap `position: fixed`.
*/
export function Modal({
open,
onClose,
children,
panelClassName = "max-h-[min(90dvh,90vh)] w-full max-w-2xl overflow-y-auto rounded-2xl border border-white/10 bg-[#0b1626] p-4 shadow-2xl sm:p-5",
zIndex = 80,
}: ModalProps) {
const [mounted, setMounted] = useState(false);
useEffect(() => {
setMounted(true);
}, []);
useEffect(() => {
if (!open) return;
const onKey = (e: KeyboardEvent) => {
if (e.key === "Escape") onClose();
};
window.addEventListener("keydown", onKey);
const prev = document.body.style.overflow;
document.body.style.overflow = "hidden";
return () => {
window.removeEventListener("keydown", onKey);
document.body.style.overflow = prev;
};
}, [open, onClose]);
if (!mounted || !open) return null;
return createPortal(
<div
className="fixed inset-0 flex items-end justify-center bg-black/70 p-0 backdrop-blur-[2px] sm:items-center sm:p-4"
style={{ zIndex, position: "fixed", top: 0, left: 0, right: 0, bottom: 0 }}
onClick={onClose}
role="dialog"
aria-modal="true"
>
<div
className={`${panelClassName} max-sm:max-h-[92dvh] max-sm:rounded-b-none max-sm:pb-[max(1rem,env(safe-area-inset-bottom))]`}
onClick={(e) => e.stopPropagation()}
>
{children}
</div>
</div>,
document.body,
);
}