summaryrefslogtreecommitdiff
path: root/frontend/src/components/Dialog.tsx
blob: 4fa6bf78ddfc281f981376c78b446e0415cdc7a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { FC, PropsWithChildren, useEffect, useRef } from 'react';

interface DialogProps {
  visible: boolean;
  closeDialog: () => void;
}

const Dialog: FC<PropsWithChildren<DialogProps>> = ({
  visible,
  closeDialog,
  children,
}) => {
  const dialogRef = useRef<HTMLDialogElement>(null);

  useEffect(() => {
    if (visible) {
      dialogRef.current?.showModal();
    } else {
      dialogRef.current?.close();
    }
  }, [visible]);

  const handleButtonClick = (e: React.MouseEvent<HTMLButtonElement>) => {
    e.preventDefault();
    closeDialog();
  };

  return (
    <dialog ref={dialogRef}>
      <div className="p-4 flex flex-col max-w-[90vw] md:max-w-[50vw] bg-white shadow">
        <div className="flex w-full">
          <button className="ml-auto" onClick={handleButtonClick}>
            <svg
              xmlns="http://www.w3.org/2000/svg"
              fill="none"
              viewBox="0 0 24 24"
              strokeWidth={1.5}
              stroke="currentColor"
              className="w-6 h-6">
              <path
                strokeLinecap="round"
                strokeLinejoin="round"
                d="M6 18 18 6M6 6l12 12"
              />
            </svg>
          </button>
        </div>
        {children}
      </div>
    </dialog>
  );
};

export default Dialog;