리액트 모달창 스크롤 방지

hapwoo·2022년 8월 9일
0

React

목록 보기
1/2
import React, { useEffect } from 'react';
import styled from 'styled-components';

type ModalProps = {
    visible?: boolean;
    closeModal: () => void;
    children: React.ReactNode;
};

const Modal: React.FC<ModalProps> = ({ visible, closeModal, children }) => {
    useEffect(() => {
        if (visible) {
            document.body.style.overflow = 'hidden';
        } else {
            document.body.style.overflow = 'unset';
        }
    }, [visible]);
    return <>{visible && <ModalWrapper onClick={closeModal}>{children}</ModalWrapper>}</>;
};

export default Modal;

const ModalWrapper = styled.div`
    position: fixed;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.6);
    top: 0;
    left: 0;
    z-index: 10;
    display: flex;
    justify-content: center;
    align-items: center;
`;
profile
프론트 개발자

0개의 댓글