mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 02:40:12 +08:00
112 lines
2.8 KiB
TypeScript
112 lines
2.8 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useCallback } from "react";
|
|
import { X } from "lucide-react";
|
|
|
|
interface ModalProps {
|
|
isOpen: boolean;
|
|
onClose: () => void;
|
|
title?: string;
|
|
titleIcon?: React.ReactNode;
|
|
children: React.ReactNode;
|
|
footer?: React.ReactNode;
|
|
width?: "sm" | "md" | "lg" | "xl";
|
|
closeOnBackdrop?: boolean;
|
|
closeOnEscape?: boolean;
|
|
showCloseButton?: boolean;
|
|
}
|
|
|
|
const widthClasses = {
|
|
sm: "w-[400px]",
|
|
md: "w-[500px]",
|
|
lg: "w-[600px]",
|
|
xl: "w-[800px]",
|
|
};
|
|
|
|
/**
|
|
* Shared Modal base component
|
|
*/
|
|
export default function Modal({
|
|
isOpen,
|
|
onClose,
|
|
title,
|
|
titleIcon,
|
|
children,
|
|
footer,
|
|
width = "md",
|
|
closeOnBackdrop = true,
|
|
closeOnEscape = true,
|
|
showCloseButton = true,
|
|
}: ModalProps) {
|
|
// Handle escape key
|
|
const handleEscape = useCallback(
|
|
(e: KeyboardEvent) => {
|
|
if (closeOnEscape && e.key === "Escape") {
|
|
onClose();
|
|
}
|
|
},
|
|
[closeOnEscape, onClose],
|
|
);
|
|
|
|
useEffect(() => {
|
|
if (isOpen) {
|
|
document.addEventListener("keydown", handleEscape);
|
|
// Prevent body scroll when modal is open
|
|
document.body.style.overflow = "hidden";
|
|
}
|
|
return () => {
|
|
document.removeEventListener("keydown", handleEscape);
|
|
document.body.style.overflow = "";
|
|
};
|
|
}, [isOpen, handleEscape]);
|
|
|
|
if (!isOpen) return null;
|
|
|
|
const handleBackdropClick = (e: React.MouseEvent) => {
|
|
if (closeOnBackdrop && e.target === e.currentTarget) {
|
|
onClose();
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div
|
|
className="fixed inset-0 bg-black/40 backdrop-blur-sm flex items-center justify-center z-50 animate-in fade-in"
|
|
onClick={handleBackdropClick}
|
|
>
|
|
<div
|
|
className={`bg-[var(--card)] border border-[var(--border)] rounded-2xl shadow-2xl ${widthClasses[width]} max-h-[90vh] flex flex-col animate-in zoom-in-95`}
|
|
>
|
|
{/* Header */}
|
|
{(title || titleIcon || showCloseButton) && (
|
|
<div className="p-4 border-b border-[var(--border)] flex items-center justify-between shrink-0">
|
|
<h3 className="font-bold text-[var(--foreground)] flex items-center gap-2">
|
|
{titleIcon}
|
|
{title}
|
|
</h3>
|
|
{showCloseButton ? (
|
|
<button
|
|
onClick={onClose}
|
|
className="p-1 hover:bg-[var(--muted)] rounded-lg transition-colors"
|
|
>
|
|
<X className="w-5 h-5 text-[var(--muted-foreground)]" />
|
|
</button>
|
|
) : (
|
|
<div />
|
|
)}
|
|
</div>
|
|
)}
|
|
|
|
{/* Content */}
|
|
<div className="flex-1 overflow-y-auto">{children}</div>
|
|
|
|
{/* Footer */}
|
|
{footer && (
|
|
<div className="p-4 border-t border-[var(--border)] shrink-0">
|
|
{footer}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|