๐จ Frontend/React
๐ฎ React - 9. ์ปดํฌ๋ํธ ์คํ์ผ๋ง
kongmi
2023. 4. 18. 18:07
๋ฆฌ์กํธ์์ ์ปดํฌ๋ํธ๋ฅผ ์คํ์ผ๋ง ํ ๋๋ ๋ค์ํ ๋ฐฉ์์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
ํ์ฌ๋ง๋ค ์๊ตฌํ๋ ๋ฐฉ์์ด ๋ค๋ฅด๊ณ ๊ฐ๋ฐ์๋ง๋ค ๊ฐ์ ์ทจํฅ์ ๋ฐ๋ผ ์ ํํด์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์์ฃผ ์ฌ์ฉ๋๋ ์คํ์ผ๋ง ๋ฐฉ์
- ์ผ๋ฐ CSS : ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ๋ฐฉ์
- Sass : ์์ฃผ ์ฌ์ฉ๋๋ CSS ์ ์ฒ๋ฆฌ๊ธฐ ์ค ํ๋๋ก ํ์ฅ๋ CSS ๋ฌธ๋ฒ์ ์ฌ์ฉํ์ฌ CSS ์ฝ๋๋ฅผ ๋์ฑ ์ฝ๊ฒ ์์ฑํ ์ ์๋๋ก ํด ์ค๋๋ค.
- CSS Module : ์คํ์ผ์ ์์ฑํ ๋ CSS ํด๋์ค๊ฐ ๋ค๋ฅธ CSS ํด๋์ค์ ์ด๋ฆ๊ณผ ์ ๋ ์ถฉ๋ํ์ง ์๋๋ก ํ์ผ๋ง๋ค ๊ณ ์ ํ ์ด๋ฆ์ ์๋์ผ๋ก ์์ฑํด ์ฃผ๋ ์ต์ ์ ๋๋ค.
- styled-components : ์คํ์ผ์ ์๋ฐ์คํฌ๋ฆฝํธ ํ์ผ์ ๋ด์ฅ์ํค๋ ๋ฐฉ์์ผ๋ก ์คํ์ผ์ ์์ฑํจ๊ณผ ๋์์ ํด๋น ์คํ์ผ์ด ์ ์ฉ๋ ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค ์ ์๊ฒ ํด์ค๋๋ค.
๐ฎ ๋ฆฌ์กํธ ํ๋ก์ ํธ ์์ฑ
$ yarn create react-app [ํด๋๋ช
]
$ cd [ํด๋๋ช
]
$ yarn start
Sass ์ฌ์ฉํ๊ธฐ
$ yarn add sass
- Sass๋ .sass์ .scss ๋ ๊ฐ์ง๊ฐ ์์ต๋๋ค.
- SCSS๋ Sass์ 3๋ฒ์ ์ดํ ์ถ๊ฐ๋ ๋ฌธ๋ฒ์ผ๋ก์, CSS์์ ํธํ์ฑ์ด ๋ ๋์ต๋๋ค.
- SCSS๋ ๊ธฐ๋ณธ์ ์ผ๋ก .scss ํ์ฅ์๋ฅผ ๊ฐ์ง๋ฉฐ, ์ค๊ดํธ{ }์ ์ธ๋ฏธ์ฝ๋ก (;)์ ์ฌ์ฉํฉ๋๋ค.
- ๋ฆฌ์กํธ์์ SCSS๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ๊ด๋ จ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ค์นํ๊ณ , import ๊ตฌ๋ฌธ์ ์ฌ์ฉํ์ฌ SCSS ํ์ผ์ ๋ถ๋ฌ์์ ์ฌ์ฉํฉ๋๋ค.
import React from "react";
import "./0418_1_style.scss"
const MyComponent = () => {
return (
<div className="my-component">
<h1 className="title">์๋
ํ์ธ์. ๋ฆฌ์กํธ ์ธ์์ ์ค์ ๊ฒ์ ํ์ ํฉ๋๋ค.</h1>
</div>
);
}
export default MyComponent;
$primary-color: #007bff;
.my-component {
background-color: $primary-color;
}
.title {
font-size: 2em;
color: #fff;
}
์์ฉ ์์
import React from "react";
import "./0418_2_SassComponent.scss"
const SassComponent = () => {
return (
<div className="SassComponent">
<div className="box red"></div>
<div className="box orange"></div>
<div className="box yellow"></div>
<div className="box green"></div>
<div className="box blue"></div>
<div className="box navy"></div>
<div className="box purple"></div>
</div>
);
}
export default SassComponent;
@import "./styles/utils.scss";
.SassComponent {
display: flex;
// .SassComponent .box ์ ๊ฐ์!!
.box {
background: red;
cursor: pointer;
transition: all 0.3s ease-in;
// .box์ .red๋ฅผ ํจ๊ป ์ฌ์ฉํ๋ค๋ ์๋ฏธ
&.red {
background: $red;
@include square(1);
}
&.orange {
background: $orange;
@include square(2);
}
&.yellow {
background: $yellow;
@include square(3);
}
&.green {
background: $green;
@include square(4);
}
&.blue {
background: $blue;
@include square(5);
}
&.navy {
background: $navy;
@include square(6);
}
&.purple {
background: $purple;
@include square(7);
}
}
}
utils ํจ์ ๋ถ๋ฆฌํ๊ธฐ
์ฌ๋ฌ ํ์ผ์์ ์ฌ์ฉ๋ ์ ์๋ Sass ๋ณ์ ๋ฐ ๋ฏน์ค์ธ์ ๋ค๋ฅธ ํ์ผ๋ก ๋ฐ๋ก ๋ถ๋ฆฌํ์ฌ ์์ฑํ ๋ค ํ์ํ ๊ณณ์์ ์ฝ๊ฒ ๋ถ๋ฌ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
@import
src/styles/utils.scss
$red: #fa5252;
$orange: #fd7e14;
$yellow: #fcc419;
$green: #40c057;
$blue: #339af0;
$navy: #5c7cfa;
$purple: #7950f2;
// ๋ฏน์ค์ธ ๋ง๋ค๊ธฐ : ์ฌ์ฌ์ฉ๋๋ ์คํ์ผ ๋ธ๋ก์ ํจ์์ฒ๋ผ ๋ง๋ค์ด ์ฌ์ฉ
@mixin square($size) {
$calculator: 32px * $size;
width: $calculator;
height: $calculator;
}
SassComponent.scss
@import "./styles/utils.scss";
.SassComponent {
display: flex;
// .SassComponent .box ์ ๊ฐ์!!
.box {
background: red;
cursor: pointer;
transition: all 0.3s ease-in;
// .box์ .red๋ฅผ ํจ๊ป ์ฌ์ฉํ๋ค๋ ์๋ฏธ
&.red {
background: $red;
@include square(1);
}
&.orange {
background: $orange;
@include square(2);
}
&.yellow {
background: $yellow;
@include square(3);
}
&.green {
background: $green;
@include square(4);
}
&.blue {
background: $blue;
@include square(5);
}
&.navy {
background: $navy;
@include square(6);
}
&.purple {
background: $purple;
@include square(7);
}
}
}
styled-components
- ์ต๊ทผ ๋ง์ด ์ฐ์ด๋ ๋ฐฉ์์ ์๋ฐ์คํฌ๋ฆฝํธ ํ์ผ ์์ ์คํ์ผ์ ์ ์ธํ๋ ๋ฐฉ์ ์ ๋๋ค.
- ์ด ๋ฐฉ์์ CSS-in-JS๋ผ๊ณ ๋ถ๋ฆ ๋๋ค.
$ yarn add styled-components
- styled-components์ ์ผ๋ฐ className์ ์ฌ์ฉํ๋ CSS/Sass๋ฅผ ๋น๊ตํ์ ๋,
style-components์ ๊ฐ์ฅ ํฐ ์ฅ์ ์ props ๊ฐ์ผ๋ก ์ ๋ฌ ํด์ฃผ๋ ๊ฐ์ ์ฝ๊ฒ ์คํ์ผ์ ์ ์ฉํ ์ ์๋ค๋ ๊ฒ ์ ๋๋ค.
import styled, { css } from "styled-components";
const Box = styled.div`
background: ${(props) => props.color || "blue"};
padding: 1rem;
display: flex;
width: 1024px;
margin: 0 auto;
/* ์ต๋ ๋๋น๊ฐ 1024px ์ผ ๋, 768px๋ก ๋ณ๊ฒฝ */
@media (max-width: 1024px) {
width: 768px;
}
@media (max-width: 768px) {
width: 90%;
}
`;
const Button = styled.button`
background: #fff;
color: #000;
border-radius: 4px;
padding: 0.5rem;
display: flex;
align-items: center;
justify-content: center;
box-sizing: border-box;
font-size: 1rem;
font-weight: 600;
&:hover {
background: rgba(255, 255, 255, 0.9);
}
${(props) =>
props.inverted &&
css`
background: none;
border: 2px solid #fff;
color: #fff;
&:hover {
background: #fff;
color: #000;
}
`};
& + button {
margin-left: 2rem;
}
`;
const StyledComponent = () => {
return (
<Box color="orangered">
<Button>์๋
ํ์ธ์.</Button>
<Button inverted={true}>ํ
๋๋ฆฌ๋ง..</Button>
</Box>
);
};
export default StyledComponent;