웹 서비스를 이용하다보면 다음과 같은 팝업창을 본 적이 있을거에요
이번 글에서는 팝업을 어떤 식으로 개발하는지 알아볼게요
팝업을 개발하려면 먼저 다음 두가지에 대해 알고 있어야 합니다.


{
z-index: 3000;
opacity: 0.5;
...
}
React를 사용한 간단한 팝업 코드입니다.
Main.tsximport * as React from "react";
import { useCookies } from "react-cookie";
import { default as Popup } from "../components/Popup";
function Main() {
const [cookies, setCookie] = useCookies(["nonePopup"]);
return (
<div
className="main"
style={{
backgroundImage: "url(https://edit-edition.com/images/mainbanner.jpg)",
width: "100%",
height: "100%",
}}
>
{cookies.nonePopup ? null : <Popup setCookie={setCookie}></Popup>}
</div>
);
}
export default Main;
Popup.tsx
import * as React from "react";
import { useState } from "react";
import "../styles/Popup.scss";
interface PopupProps {
setCookie: (
name: "nonePopup",
value: any,
options?: CookieSetOptions | undefined,
) => void;
}
function Popup(props: PopupProps) {
const [isVisible, setIsVisible] = useState<boolean>(true);
const handleRightBtnClick = () => {
setIsVisible(false);
};
const handleLeftBtnClick = () => {
const expires = new Date();
expires.setDate(expires.getDate() + 1);
props.setCookie("nonePopup", true, { expires });
};
return (
<>
{isVisible ? (
<div className="popup">
<div className="popup__dimmed"></div>
<div className="popup__modal">
<div className="modal__content">
<p>내용</p>
</div>
<div className="modal__bottom">
<div className="bottom__left-btn" onClick={handleLeftBtnClick}>
<p>오늘 그만 보기</p>
</div>
<div className="bottom__right-btn" onClick={handleRightBtnClick}>
<p>닫기</p>
</div>
</div>
</div>
</div>
) : null}
</>
);
}
export default Popup;
Popup.scss
.popup {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 3500;
.popup__dimmed {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: #000;
opacity: 0.5;
}
.popup__modal {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 300px;
height: 450px;
border: solid 1px;
border-radius: 20px;
background-color: #fff;
.modal__content {
display: flex;
justify-content: center;
align-items: center;
height: 410px;
}
.modal__bottom {
position: absolute;
left: 0;
bottom: 0;
display: flex;
height: 40px;
border-top: solid 1px;
color: #000;
cursor: pointer;
.bottom__left-btn {
border-right: solid 1px;
border-bottom-left-radius: 20px;
width: 150px;
}
.bottom__right-btn {
border-left: solid 1px;
border-bottom-right-radius: 20px;
width: 150px;
}
.bottom__left-btn p,
.bottom__right-btn p {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
}
}
}
p {
font-weight: bold;
font-size: 15px;
}
브라우저에서는 다음과 같이 보여집니다.
팝업이 노출됐을 때 화면

팝업이 닫혔을 때 화면

오늘 그만 보기 버튼을 눌렀을 때 생성된 쿠키
