Spaces:
Running

Bot / frontend /src /shared /components /Modal.tsx
ddosvn's picture
Initial deploy of Panel ThTuanIT full stack and Lighthouse-optimized frontend
7e79d39 verified
Raw History Blame Contribute Delete
2.98 kB
'use client';
import React, { useEffect } from 'react';
import { X } from 'lucide-react';
import { Button } from '@/shared/components/Button';
import { cn } from '@/shared/lib/cn';
interface ModalProps {
open: boolean;
onClose: () => void;
title: string;
maxWidthClass?: string;
children: React.ReactNode;
}
export function Modal({
open,
onClose,
title,
maxWidthClass = 'max-w-md',
children,
}: ModalProps) {
useEffect(() => {
if (!open) return;
const onKeyDown = (e: KeyboardEvent) => {
if (e.key === 'Escape') onClose();
};
window.addEventListener('keydown', onKeyDown);
return () => window.removeEventListener('keydown', onKeyDown);
}, [open, onClose]);
if (!open) return null;
return (
<div
role="dialog"
aria-modal="true"
aria-label={title}
onClick={onClose}
className="fixed inset-0 z-50 flex items-center justify-center bg-black/75 backdrop-blur-sm p-4"
>
<div
onClick={(e) => e.stopPropagation()}
className={cn(
'w-full rounded-2xl bg-surface-1 border border-line shadow-2xl overflow-hidden flex flex-col max-h-[90vh]',
maxWidthClass,
)}
>
<div className="px-5 py-3.5 border-b border-line flex items-center justify-between gap-3">
<h3 className="text-sm font-bold text-ink-1 truncate">{title}</h3>
<button
type="button"
onClick={onClose}
aria-label={`Đóng ${title}`}
className="p-2 min-h-[40px] min-w-[40px] flex items-center justify-center rounded-lg text-ink-3 hover:text-ink-1 hover:bg-surface-2 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent"
>
<X className="w-4 h-4" aria-hidden="true" />
</button>
</div>
<div className="p-5 overflow-y-auto">{children}</div>
</div>
</div>
);
}
interface ConfirmModalProps {
open: boolean;
onClose: () => void;
onConfirm: () => void | Promise<void>;
title: string;
description: string;
confirmLabel?: string;
danger?: boolean;
loading?: boolean;
}
export function ConfirmModal({
open,
onClose,
onConfirm,
title,
description,
confirmLabel = 'Xác nhận',
danger = false,
loading = false,
}: ConfirmModalProps) {
return (
<Modal open={open} onClose={onClose} title={title} maxWidthClass="max-w-sm">
<div className="space-y-4">
<p className="text-xs sm:text-sm text-ink-2 leading-relaxed">{description}</p>
<div className="flex items-center justify-end gap-2">
<Button variant="secondary" size="sm" onClick={onClose} disabled={loading}>
Huỷ
</Button>
<Button
variant={danger ? 'danger' : 'primary'}
size="sm"
onClick={() => void onConfirm()}
loading={loading}
>
{confirmLabel}
</Button>
</div>
</div>
</Modal>
);
}