UI 컴포넌트의 필요성
아무리 복잡한 화면도 내부에서 사용되는 UI는 반복적으로 재사용되는 경우가 많다. 따라서 반복되는 UI는 컴포넌트로 관리하면 개발 기간을 단축할 수 있다.
디자인 시스템 이용의 이점
디자인 시스템을 이용하면 UI 컴포넌트들을 효과적으로 관리할 수 있다.
디자인 시스템은 서비스를 만드는데 사용한 컬러, 서체, 인터랙션 등을 체계적으로 정리한 것이기 때문이다.
화면부터 만들고 기능을 붙인다.
position: fixed;
left: 0px;
right: 0px;
top: 0px;
bottom: 0px;
모달창을 만들면서, 이벤트핸들러를 연결한 X 버튼 외에 모달 박스를 클릭하여도 모달 창이 닫히는 문제가 발견 되었다.
이는 이벤트 버블링으로 인해 발생한 것이며, 이벤트 버블링이란 "자식 요소를 클릭했을 때, 부모 요소에 적용된 이벤트도 함께 실행되는 것"이다.
onClick = ((event) => e.stopPropagation())postion은 요소의 위치를 결정하는 속성이며, relative & absolute를 이용하는 방법과 fixed를 이용하는 방법이 있다.
relative (부모) & absolute (자식)왼쪽-위 or 오른쪽-아래를 기준으로 값을 주면 된다. position: absolute;
left: 0px;
top: 0px;
fixedopacity로 불투명도를 조정하면, 해당 요소에 포함된 모든 요소들에게도 불투명도가 적용된다. (자식요소도 불투명해짐)background-color : rgba(n, n, n, 여기)에서 불투명도를 조절할 수 있다. opacity와 달리 선택한 요소에만 투명도가 설정된다. import { useState } from 'react';
import styled from 'styled-components';
export const ModalContainer = styled.div`
display: flex;
position: relative;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100%;
`;
export const ModalBackdrop = styled.div`
background-color: rgba(0, 0, 0, 0.4);
position: absolute;
left: 0px;
top: 0px;
display: flex;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
`;
export const ModalBtn = styled.button`
background-color: var(--coz-purple-600);
text-decoration: none;
border: none;
padding: 20px;
color: white;
border-radius: 30px;
cursor: grab;
`;
export const ModalView = styled.div.attrs((props) => ({
role: 'dialog',
}))`
background-color: white;
width: 300px;
height: 150px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
border-radius: 30px;
>button {
background: none;
color: black;
font-size: 1rem;
border: none;
}
`;
export const Modal = () => {
const [isOpen, setIsOpen] = useState(false);
const openModalHandler = () => {
setIsOpen(!isOpen);
};
return (
<>
<ModalContainer>
<ModalBtn
onClick={openModalHandler}
>
{isOpen ? "Opened!" : "Open Modal"}
</ModalBtn>
{isOpen ? (
<>
<ModalBackdrop onClick={openModalHandler} >
<ModalView onClick={((e) => e.stopPropagation())} >
<button onClick={openModalHandler}> X </button>
<h2 className='text'>HELLO CODSTATES!</h2>
</ModalView>
</ModalBackdrop>
</>
)
: null}
</ModalContainer>
</>
);
};
import React from 'react';
import '../variables.css';
import { Modal } from '../components/BareMinimumRequirements/Modal';
export default {
title: 'Example/Modal',
component: Modal
};
const Template = (args) => <Modal {...args} />;
export const Primary = Template.bind({});
Primary.args = {
primary: true,
label: 'Modal'
};