React

[React] CDD ๊ฐœ๋ฐœ ๋„๊ตฌ Styled Components

mellomello.made 2022. 7. 1. 01:27

๐Ÿ”–  CSS in JS ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ž€?

CSS in JS ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด CSS๋„ ์‰ฝ๊ฒŒ Javascript ์•ˆ์— ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, HTML + JS + CSS๊นŒ์ง€ ๋ฌถ์–ด์„œ ํ•˜๋‚˜์˜ JSํŒŒ์ผ ์•ˆ์—์„œ ์ปดํฌ๋„ŒํŠธ ๋‹จ์œ„๋กœ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๋‹ค. ํ˜„์žฌ CSS in JS ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ค‘ ๊ฐ€์žฅ ์ธ๊ธฐ ์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” Styled Components ์ด๋‹ค.

 


 

๐Ÿ”–  Styled Components ์„ค์น˜ํ•˜๊ธฐ

// 1. ํ„ฐ๋ฏธ๋„์— ์•„๋ž˜์˜ ๋ช…๋ น์–ด ํ•œ ์ค„์„ ์ž…๋ ฅํ•œ๋‹ค.

# with npm
$ npm install --save styled-components

# with yarn
$ yarn add styled-components

// 2. package.json์— ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€(๊ถŒ์žฅํ•˜๋Š”  ํŽธ)  
// ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ์—ฌ๋Ÿฌ ๋ฒ„์ „์˜ Styled Components๊ฐ€ ์„ค์น˜๋˜์–ด ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ์ค„์—ฌ์ค€๋‹ค.

{
  "resolutions": {
    "styled-components": "^5"
  }
}

// 3. Styled Components๋ฅผ ์‚ฌ์šฉํ•  ํŒŒ์ผ๋กœ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.

import styled from "styled-components"

 


 

๐Ÿ”–  Styled Components ๋ฌธ๋ฒ•

 

1.  ์ปดํฌ๋„ŒํŠธ ๋งŒ๋“ค๊ธฐ

  • Styled Components๋Š” ES6์˜ Templete Literals ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•œ๋‹ค.
  • ์ฆ‰, ๋”ฐ์˜ดํ‘œ๊ฐ€ ์•„๋‹Œ ๋ฐฑํ‹ฑ(`)์„ ์‚ฌ์šฉํ•œ๋‹ค.
  • ์ปดํฌ๋„ŒํŠธ๋ฅผ ์„ ์–ธํ•œ ํ›„ styled.ํƒœ๊ทธ์ข…๋ฅ˜๋ฅผ ํ• ๋‹นํ•œ๋‹ค.
  • ๋ฐฑํ‹ฑ ์•ˆ์— ๊ธฐ์กด์— CSS๋ฅผ ์ž‘์„ฑํ•˜๋˜ ๋ฌธ๋ฒ•๊ณผ ๋˜‘๊ฐ™์ด ์Šคํƒ€์ผ ์†์„ฑ์„ ์ž‘์„ฑํ•œ๋‹ค. 
  • React ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋“ฏ ๋ฆฌํ„ด๋ฌธ ์•ˆ์— ์ž‘์„ฑํ•ด์ฃผ๋ฉด ์Šคํƒ€์ผ์ด ์ ์šฉ๋œ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

const ์ปดํฌ๋„ŒํŠธ ์ด๋ฆ„ = styled.ํƒœ๊ทธ์ข…๋ฅ˜`
	css ์†์„ฑ1: ์†์„ฑ๊ฐ’;
    css ์†์„ฑ2: ์†์„ฑ๊ฐ’;
    
`;


const BlueButton = styled.button` //<-๋ฐฑํ‹ฑ(1)
	background-color: blue;
    color: white;

`; //<-๋ฐฑํ‹ฑ(2)
import styled from "styled-component";

// 1. Styled Component๋กœ ์ปดํฌ๋„ŒํŠธ ๋งŒ๋“ ๋‹ค.
// ์ปดํฌ๋„ŒํŠธ๋ฅผ ์„ ์–ธ(const BlueButton)ํ•œ ํ›„ styled.ํƒœ๊ทธ์ข…๋ฅ˜(styled.button)๋ฅผ ํ• ๋‹น
const BlueButton = styled.button` //<-๋ฐฑํ‹ฑ(1)
	background-color: blue;
    color: white;

`; //<-๋ฐฑํ‹ฑ(2) ์—ฌ๊ธฐ ๋ฐฑํ‹ฑ์€ ์ž˜ ์•ˆ๋ณด์ด๋‹ˆ ๋ˆˆ ํฌ๊ฒŒ ๋– ์•ผํ•จ ๐Ÿ‘€

export default function App() {
	return <BlueButton> Blue Button </BlueButton>;
    }

 


 

2.  ๊ธฐ์กด ์ปดํฌ๋„ŒํŠธ๋ฅผ ์žฌ์‚ฌ์šฉํ•ด์„œ ์ƒˆ๋กœ์šด ์ปดํฌ๋„ŒํŠธ ๋งŒ๋“ค๊ธฐ

์ปดํฌ๋„ŒํŠธ๋ฅผ ์„ ์–ธํ•˜๊ณ  styled() ์— ์žฌํ™œ์šฉํ•  ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ „๋‹ฌํ•ด์ค€ ๋‹ค์Œ, ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์€ ์Šคํƒ€์ผ ์†์„ฑ์„ ์ž‘์„ฑํ•œ๋‹ค.

const ์ปดํฌ๋„ŒํŠธ ์ด๋ฆ„ = styled(์žฌ์‚ฌ์šฉ ํ•  ์ปดํฌ๋„ŒํŠธ)`
	์ถ”๊ฐ€ํ•  css ์†์„ฑ1: ์†์„ฑ๊ฐ’;
    ์ถ”๊ฐ€ํ•  css ์†์„ฑ2: ์†์„ฑ๊ฐ’;
`;

cosnt BigBlueButton = styled(BlueButton)`
	padding: 10px;
    margin-top: 10px;
`;
import style from "styled-components";

const BlueButton = styled.button`
	background-color: blue;
    color: white;
`;

//๋งŒ๋“ค์–ด์ง„ BlueButton ์ปดํฌ๋„ŒํŠธ๋ฅผ ์žฌ์‚ฌ์šฉํ•ด์„œ ์ƒˆ ์ปดํฌ๋„ŒํŠธ ๋งŒ๋“ค๊ธฐ
const BigBlueButton = styled(BlueButon)`
	padding: 10px;
    margin-top: 10px;
`;

//์žฌ์‚ฌ์šฉํ•œ ์ปดํฌํ„ดํŠธ๋Š” ๋˜ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
const BigRedButton = styled(BigBlueButton)`
	background-color: red;
`

export default function App(){
	return (
    <>
     <BlueButton>Blue Button</BlueButton>
      <br />
      <BigBlueButton>Big Blue Button</BigBlueButton>
      <br />
      <BigRedButton>Big Red Button</BigRedButton>
    </>
  );
}
    )