Files
sorsfonalak/services/frontend/src/components/Modal.tsx
T

56 lines
1.4 KiB
TypeScript
Raw Normal View History

"use client";
import { useEffect, useId, useRef } from "react";
/**
* A modal window (the browser's own <dialog>: focus stays inside, Escape
* closes it). Shown while mounted; a click on the dimmed backdrop closes it.
*/
export function Modal({
title,
subtitle,
onClose,
children,
}: {
title: string;
subtitle?: React.ReactNode;
onClose: () => void;
children: React.ReactNode;
}) {
const ref = useRef<HTMLDialogElement>(null);
const titleId = useId();
// No close() on unmount: that would fire onClose; a removed dialog is gone anyway.
useEffect(() => {
const dialog = ref.current;
if (dialog && !dialog.open) dialog.showModal();
}, []);
return (
<dialog
ref={ref}
className="modal"
aria-labelledby={titleId}
onClose={onClose}
onClick={(e) => {
if (e.target === ref.current) onClose();
}}
>
<div className="modal-inner">
<div className="modal-head">
<div>
<h2 id={titleId}>{title}</h2>
{subtitle && <div className="muted modal-sub">{subtitle}</div>}
</div>
<button type="button" className="modal-close" onClick={onClose} aria-label="Bezárás">
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d="M6 6l12 12M18 6L6 18" />
</svg>
</button>
</div>
{children}
</div>
</dialog>
);
}