Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions src/components/common/Modal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { type MouseEventHandler, type ReactNode } from 'react';
import {
type MouseEventHandler,
type ReactNode,
useEffect,
useRef,
} from 'react';
import Button from './Button';
import ic_check from '@/assets/icons/modal_check.svg';
import ic_exclamation from '@/assets/icons/modal_exclamation.svg';
Expand All @@ -14,6 +19,8 @@ interface Props {
yesButtonContent?: string;
/** modal 문구 */
children?: ReactNode;
/** 모달이 닫힐 때 (버튼 클릭 제외) 실행할 함수 */
onClose: () => void;
}

export default function Modal({
Expand All @@ -22,11 +29,42 @@ export default function Modal({
onYesButtonClick,
yesButtonContent,
children,
onClose,
}: Props) {
const modalRef = useRef<HTMLDivElement>(null);

useEffect(() => {
// 1. 바깥 클릭 시 닫기
function handleClickOutside(event: MouseEvent): void {
const target = event.target as Node;
if (modalRef.current && !modalRef.current.contains(target)) {
onClose();
}
}

// 2. ESC 키로 닫기
function handleKeyDown(event: KeyboardEvent): void {
if (event.key === 'Escape') {
onClose();
}
}

document.addEventListener('mousedown', handleClickOutside);
document.addEventListener('keydown', handleKeyDown);

return () => {
document.removeEventListener('mousedown', handleClickOutside);
document.removeEventListener('keydown', handleKeyDown);
};
}, [onClose]);

return (
<div className="fixed inset-0 z-40 content-center justify-items-center bg-[#000000b3]">
{option === 'alert' ? (
<div className="flex h-220 w-327 flex-col items-center justify-between rounded-lg bg-white p-28 md:h-250 md:w-540">
<div
ref={modalRef}
className="flex h-220 w-327 flex-col items-center justify-between rounded-lg bg-white p-28 md:h-250 md:w-540"
>
<div className="mt-53 text-center text-body1/19 font-regular text-[#333236] md:mt-80 md:text-[18px]/21">
{children}
</div>
Expand All @@ -38,7 +76,10 @@ export default function Modal({
</button>
</div>
) : (
<div className="flex h-184 w-298 flex-col items-center justify-between rounded-xl bg-white p-24">
<div
ref={modalRef}
className="flex h-184 w-298 flex-col items-center justify-between rounded-xl bg-white p-24"
>
<div className="flex flex-col items-center gap-16">
{option === 'action' ? (
<img src={ic_check} />
Expand Down
8 changes: 5 additions & 3 deletions src/pages/account/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ export default function Login() {
}
}

const handleModalConfirm = () => {
function handleModalConfirm() {
setModal({ isOpen: false, message: '' });
};
}
Comment on lines -84 to +86
Copy link
Contributor

@Moon-ju-young Moon-ju-young Jun 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💬 원래대로 써도 되지 않을까 싶네요!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저희가 함수 선언식을 화살표 함수가 아닌 function으로 정했던걸로 알고있어서 수정했습니다!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아마 컴포넌트 함수만 통일 했었던 것 같은데 일단 확인했습니다!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이부분은 화살표로 쓰셔도 상관없습니다! 컴포넌트만 통일입니다


return (
<>
Expand Down Expand Up @@ -128,7 +128,9 @@ export default function Login() {
</form>

{modal.isOpen && (
<Modal onButtonClick={handleModalConfirm}>{modal.message}</Modal>
<Modal onClose={handleModalConfirm} onButtonClick={handleModalConfirm}>
{modal.message}
</Modal>
)}
</>
);
Expand Down
10 changes: 6 additions & 4 deletions src/pages/account/Signup.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useState, useCallback } from 'react';
import { Link, useNavigate } from 'react-router-dom';
import { postUser, type UserType } from '@/api/userApi';
import Input from '@/components/common/Input';
Expand Down Expand Up @@ -107,14 +107,14 @@ export default function Signup() {
}
}

const handleModalConfirm = () => {
const handleModalConfirm = useCallback(() => {
if (modal.message === '가입이 완료되었습니다!') {
setModal({ isOpen: false, message: '' });
navigate('/login');
} else {
setModal({ isOpen: false, message: '' });
}
};
}, [modal.message, navigate]);

return (
<>
Expand Down Expand Up @@ -223,7 +223,9 @@ export default function Signup() {
</form>

{modal.isOpen && (
<Modal onButtonClick={handleModalConfirm}>{modal.message}</Modal>
<Modal onClose={handleModalConfirm} onButtonClick={handleModalConfirm}>
{modal.message}
</Modal>
)}
</>
);
Expand Down