From 72c78ee3e15bf529776ac6dab5f1440df524bef3 Mon Sep 17 00:00:00 2001 From: Jungmin Date: Fri, 25 Oct 2024 18:23:10 +0900 Subject: [PATCH] modal refactoring --- src/hook/useModal.tsx | 48 ++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/src/hook/useModal.tsx b/src/hook/useModal.tsx index 553d8f4..dc1b3a5 100644 --- a/src/hook/useModal.tsx +++ b/src/hook/useModal.tsx @@ -1,8 +1,8 @@ "use client"; import { IoCloseOutline } from "react-icons/io5"; -import React, { ReactNode, useState } from "react"; -import ReactDOM from "react-dom"; +import React, { ReactNode, useCallback, useState } from "react"; +import { createPortal } from "react-dom"; type ModalProps = { name: string; @@ -13,36 +13,50 @@ type ModalProps = { export default function useModal() { const [custom, setCustom] = useState(""); - // 모달 열기 함수 const openModal = (name: string) => { setCustom(name); }; - // 모달 닫기 함수 const closeModal = () => { setCustom(""); }; - const Modal = ({ name, title, children }: ModalProps) => - ReactDOM.createPortal( - custom === name ? ( -
-
+ const Modal = ({ name, children, title }: ModalProps) => { + if (typeof document === "undefined") return null; + + return createPortal( + name === custom ? ( +
e.stopPropagation()} // 모달 내부 클릭 시 닫히지 않게 + role="dialog" + aria-modal="true" + className="relative h-auto w-auto rounded-xl bg-white p-6" + onClick={(e) => e.stopPropagation()} > -

{title}

- - {children} +
+

{title}

+ +
+ +
+ +
{children}
) : null, document.body, ); + }; return { Modal, openModal, closeModal }; }