40 lines
795 B
TypeScript
40 lines
795 B
TypeScript
import { useRef, useEffect, type ReactNode } from 'react';
|
||
export function Dialog({
|
||
id,
|
||
label,
|
||
close,
|
||
children,
|
||
ownClose = false,
|
||
}: {
|
||
id: string;
|
||
label: string;
|
||
close: () => void;
|
||
children: ReactNode;
|
||
ownClose?: boolean;
|
||
}) {
|
||
const ref = useRef<HTMLDialogElement>(null);
|
||
useEffect(() => {
|
||
const node = ref.current!;
|
||
node.showModal();
|
||
return () => node.close();
|
||
}, []);
|
||
return (
|
||
<dialog
|
||
id={id}
|
||
ref={ref}
|
||
aria-label={label}
|
||
onCancel={event => {
|
||
event.preventDefault();
|
||
close();
|
||
}}
|
||
>
|
||
{!ownClose && (
|
||
<button type="button" className="dialog-close" data-action="close-dialog" aria-label={label} onClick={close}>
|
||
×
|
||
</button>
|
||
)}
|
||
{children}
|
||
</dialog>
|
||
);
|
||
}
|