Skip to content

Commit

Permalink
feat: 모달 외부 영역 클릭시, 모달 사라지도록 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
pepperdad committed Jul 8, 2024
1 parent b288849 commit 4b0de19
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const ModalWrapper = styled.div`
position: fixed;
top: 0;
left: 0;
z-index: 100;
z-index: 20;
display: flex;
align-items: center;
justify-content: center;
Expand All @@ -15,6 +15,7 @@ export const ModalWrapper = styled.div`
`;

export const ModalContainer = styled.div`
z-index: 30;
display: flex;
flex-direction: column;
gap: 3.2rem;
Expand Down
32 changes: 28 additions & 4 deletions src/components/commons/modal/components/ModalWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
import React from "react";
import useModal from "@hooks/useModal";
import React, { useRef } from "react";
import * as S from "./ModalWrapper.styled";

interface ModalWrapperProps {
children: React.ReactNode;
type: "alert" | "confirm" | "modal";
}

const ModalWrapper = ({ children }: ModalWrapperProps) => {
const ModalWrapper = ({ children, type }: ModalWrapperProps) => {
const { closeAlert, closeConfirm } = useModal();
const containerRef = useRef<HTMLDivElement>(null);

const closeModal = () => {
switch (type) {
case "alert":
closeAlert();
return;
case "confirm":
closeConfirm();
return;
default:
return;
}
};

const handleClickOutside = (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
if (containerRef.current && !containerRef.current.contains(e.target as Node)) {
closeModal();
}
};

return (
<S.ModalWrapper>
<S.ModalContainer>{children}</S.ModalContainer>
<S.ModalWrapper onClick={handleClickOutside}>
<S.ModalContainer ref={containerRef}>{children}</S.ModalContainer>
</S.ModalWrapper>
);
};
Expand Down

0 comments on commit 4b0de19

Please sign in to comment.