카테고리 없음

최종프로젝트 마켓컬리) 모달 팝업창 만들기

jyee 2023. 6. 12. 19:24
728x90
반응형

마켓컬리 마이컬리 부분에서 후기 작성이 모달 팝업창으로 뜨길래  이 부분을 구현해야 하는데

리액트를 잘 몰라서 무작정 서치해서 알아보기로 했다. 

1. 함수형으로 컴포넌트 만들기

 

- CSS파일 (함수형/클래스형 공통)

.modal {
  display: none;
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 99;
  background-color: rgba(0, 0, 0, 0.6);
}
.modal button {
  outline: none;
  cursor: pointer;
  border: 0;
}
.modal > section {
  width: 90%;
  max-width: 450px;
  margin: 0 auto;
  border-radius: 0.3rem;
  background-color: #fff;
  /* 팝업이 열릴때 스르륵 열리는 효과 */
  animation: modal-show 0.3s;
  overflow: hidden;
}
.modal > section > header {
  position: relative;
  padding: 16px 64px 16px 16px;
  background-color: #f1f1f1;
  font-weight: 700;
}
.modal > section > header button {
  position: absolute;
  top: 15px;
  right: 15px;
  width: 30px;
  font-size: 21px;
  font-weight: 700;
  text-align: center;
  color: #999;
  background-color: transparent;
}
.modal > section > main {
  padding: 16px;
  border-bottom: 1px solid #dee2e6;
  border-top: 1px solid #dee2e6;
}
.modal > section > footer {
  padding: 12px 16px;
  text-align: right;
}
.modal > section > footer button {
  padding: 6px 12px;
  color: #fff;
  background-color: #6c757d;
  border-radius: 5px;
  font-size: 13px;
}
.modal.openModal {
  display: flex;
  align-items: center;
  /* 팝업이 열릴때 스르륵 열리는 효과 */
  animation: modal-bg-show 0.3s;
}
@keyframes modal-show {
  from {
    opacity: 0;
    margin-top: -50px;
  }
  to {
    opacity: 1;
    margin-top: 0;
  }
}
@keyframes modal-bg-show {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

 

 

 

- ModalPage.js

 
 import React, { useState } from "react";
 import "../css/modal.css";

const ModalPage = (props) => {
  //열기 , 닫기 , 모달 헤더 텍스트를 부모로부터 받아옴
  const { open, close, header } = props;

  return (
    //모달이 열릴 때 openModal 클래스가 생성된다,
    <div className={open ? "openModal modal" : "modal"}>
      {open ? (
        <section>
          <header>
            {header}
            <button className="close" onClick={close}>
              &times;
            </button>
          </header>
          <main>{props.children}</main>
          <footer>
            <button className="close" onClick={close}>
              close
            </button>
          </footer>
        </section>
      ) : null}
    </div>
  );
};

export default ModalPage;

 

 

- App.js

import styled from "styled-components";
import React, { useEffect, useState } from "react";
import "../css/modal.css";
import ModalPage from "./ModalPage";

const Mypage = () => {
  const [modalOpen, setModalOpen] = useState(false);

  const openModal = () => {
    setModalOpen(true);
  };

  const closeModal = () => {
    setModalOpen(false);
  };
  return (
    <React.Fragment>
      <button onClick={openModal}>모달팝업</button>
      <ModalPage open={modalOpen} close={closeModal} header="Modal heading">
        test용 팝업창입니다!

      </ModalPage>
    </React.Fragment>
  );
};


export default Mypage;

 

728x90
반응형