Skip to content

Commit

Permalink
feat: Modal 창에서 엔터창 입력시, primaryButton클릭하도록 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
hyonun321 committed Nov 30, 2024
1 parent 5b90fc7 commit 20d6d3c
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions client/src/components/modal/modal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { motion, AnimatePresence } from "framer-motion";
import { useEffect } from "react";
import { createPortal } from "react-dom";
import { TextButton } from "../button/textButton";
import { modalContainerAnimation, overlayAnimation } from "./modal.animation";
Expand Down Expand Up @@ -29,6 +30,26 @@ export const Modal = ({
}: ModalProps) => {
const portal = document.getElementById("modal") as HTMLElement;

useEffect(() => {
const handleKeyPress = (event: KeyboardEvent) => {
if (isOpen && event.key === "Enter") {
event.preventDefault();
primaryButtonOnClick?.();
}
};

// 모달이 열려있을 때만 이벤트 리스너 추가
if (isOpen) {
window.addEventListener("keydown", handleKeyPress);
}

// 클린업 함수
return () => {
window.removeEventListener("keydown", handleKeyPress);
};
}, [isOpen, primaryButtonOnClick]);

if (!isOpen) return null;
return createPortal(
<AnimatePresence>
{isOpen && (
Expand Down

0 comments on commit 20d6d3c

Please sign in to comment.