[React/CSS] Styled Components

Hannahhhยท2022๋…„ 8์›” 26์ผ
0

React

๋ชฉ๋ก ๋ณด๊ธฐ
14/30

๐Ÿ” Styled Components


HTML + JS + CSS๊นŒ์ง€ ๋ฌถ์–ด์„œ ํ•˜๋‚˜์˜ JSํŒŒ์ผ ์•ˆ์—์„œ ์ปดํฌ๋„ŒํŠธ ๋‹จ์œ„๋กœ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ, CSS๋ฅผ ์ปดํฌ๋„ŒํŠธ ์•ˆ์œผ๋กœ ์บก์Šํ™”ํ•œ๋‹ค.


๐Ÿ‘€ setting


ํ„ฐ๋ฏธ๋„์— ์•„๋ž˜์˜ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด styled components ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•˜๊ณ ,

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

# with yarn
$ yarn add styled-components

package.josn์— ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์—ฌ๋Ÿฌ ๋ฒ„์ „์˜ styled components๊ฐ€ ์„ค์น˜๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•œ๋‹ค.

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

์ด ํ›„, app.js์— import styled from "styled-components" ๋ฅผ ์ตœ์ƒ๋‹จ์— ์ž‘์„ฑํ•ด import ํ•ด์ฃผ๋ฉด styled components๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.




๐Ÿ‘€ ๋ฌธ๋ฒ•


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


ES6์˜ Templete Literals ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฑํ‹ฑ( ` )์„ ์‚ฌ์šฉํ•˜์—ฌ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ ๋‹ค.


import styled from "styled-components";

//Styled Components๋กœ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค๊ณ 
const Button = styled.button`
  background-color: blue;
  color: white;
`;

export default function App() {
  // React ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋“ฏ์ด ์‚ฌ์šฉํ•œ๋‹ค.
  return <Button>Button</Button>;
}



2. ์ปดํฌ๋„ŒํŠธ ์žฌํ™œ์šฉ


๋งŒ๋“ค์–ด์ง„ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์žฌํ™œ์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.


import styled from "styled-components";

//Styled Components๋กœ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค๊ณ 
const Button = styled.button`
  background-color: blue;
  color: white;
`;

// ์œ„์˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์žฌํ™œ์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
const BigButton = styled(Button)`
  padding: 10px;
  margin-top: 10px;
`;

export default function App() {
  // React ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋“ฏ์ด ์‚ฌ์šฉํ•œ๋‹ค.
  return (
    <>
      <Button>Button</Button>
      <br />
      <BigButton>Big Button</BigButton>
    </>
  );
}



3. Props ํ™œ์šฉ


React ์ปดํฌ๋„ŒํŠธ์ฒ˜๋Ÿผ props๋ฅผ ๋‚ด๋ ค์ค„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, props ๊ฐ’์— ๋”ฐ๋ผ์„œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ Œ๋”๋งํ•  ์ˆ˜๋„ ์žˆ๋‹ค.


์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋ง


import styled from "styled-components";

//๋ฐ›์•„์˜จ prop์— ๋”ฐ๋ผ ์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋ง์„ ํ•ด์ค€๋‹ค
// props์˜ ๊ฐ’์ด blue๋ฉด background์˜ ์ƒ‰์€ blue, ์•„๋‹ˆ๋ฉด white
const Button = styled.button`
  background: ${(props) => (props.blue ? "blue" : "white")};
`;

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



props ๊ฐ’์œผ๋กœ ๋ Œ๋”๋ง


import styled from "styled-components";

//๋ฐ›์•„์˜จ prop ๊ฐ’์œผ๋กœ ๋ Œ๋”๋งํ•œ๋‹ค
const Button = styled.button`
  background: ${(props) => (props.color ? props.color : "white")};
`;

//๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ๋„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
const Button2 = styled.button`
  background: ${(props) => props.color || "white"};
`;

export default function App() {
  return (
    <>
      <Button>Button1</Button>
      <Button color="blue">Button1</Button>
      <Button color="red">Button1</Button>
      <br />
      <Button2>Button2</Button2>
      <Button2 color="yellow">Button2</Button2>
      <Button2 color="pink">Button2</Button2>
    </>
  );
}



4. ์ „์—ญ ์Šคํƒ€์ผ ์„ค์ •


๋ฐ”๋กœ ์œ„์œ„ **props ๊ฐ’์œผ๋กœ ๋ Œ๋”๋ง** ์˜ˆ์‹œ ์ฝ”๋“œ์— ์ „์—ญ ์Šคํƒ€์ผ์„ ์ ์šฉํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.
import styled from "styled-components";

// ์ „์—ญ ์Šคํƒ€์ผ ์„ค์ ˆ์„ ์œ„ํ•œ ํ•จ์ˆ˜ import
import { createGlobalStyle } from "styled-components";

// ์ „์—ญ ์Šคํƒ€์ผ ์„ค์ •
const GlobalStyle = createGlobalStyle`
	button {
		padding : 5px;
    margin : 2px;
    border-radius : 5px;
	}
`

//๋ฐ›์•„์˜จ prop ๊ฐ’์œผ๋กœ ๋ Œ๋”๋งํ•œ๋‹ค
const Button = styled.button`
  background: ${(props) => (props.color ? props.color : "white")};
`;

//๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ๋„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
const Button2 = styled.button`
  background: ${(props) => props.color || "white"};
`;

export default function App() {
  return (
    <>
      <GlobalStyle />{/*์ „์—ญ ์Šคํƒ€์ผ ์ปดํฌ๋„ŒํŠธ ์ ์šฉ*/}
        <Button>Button1</Button>
        <Button color="blue">Button1</Button>
        <Button color="red">Button1</Button>
        <br />
        <Button2>Button2</Button2>
        <Button2 color="yellow">Button2</Button2>
        <Button2 color="pink">Button2</Button2>
    </>
      );
}




๐Ÿ‘€ ์‹ค์Šต ์˜ˆ์‹œ


์œ„์˜


// app.js

import "./styles.css";

export default function App() {
  return <button id="practice">Practice!</button>;
}

// CSS

* {
  margin: 0.5rem;
}

#practice {
  padding: 1rem;
  font-size: 2rem;
  background: powderblue;
  border-radius: 1rem;
  transition: 0.5s;
}

#practice:hover {
  background: cornflowerblue;
  color: white;
  transition: 0.5s;
}

  • ๋‚˜ํƒ€๋‚˜๋Š” ๋ฒ„ํŠผ

  • hover ์‹œ, ๋ฒ„ํŠผ ๋””์ž์ธ ๋ณ€๊ฒฝ



์œ„์˜ ์ฝ”๋“œ(๊ธฐ๋ณธ CSS)์— styled components๋ฅผ ์ ์šฉ ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.



// app.js

import "./styles.css";
import styled from "styled-components";
import { createGlobalStyle } from "styled-components";

// Styled Components๋Š” ์ด๋ฏธ ์„ค์น˜๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. import ํ•˜๋Š” ๊ฒƒ ์žŠ์ง€ ๋งˆ์„ธ์š”.
// ์ „์—ญ ์Šคํƒ€์ผ๋„ Styled Components ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ”๊ฟ”์„œ ์ ์šฉํ•ด๋ณด์„ธ์š”.
// #practice:hover ์†์„ฑ์€ ์–ด๋–ป๊ฒŒ Styled Components ์ปดํฌ๋„ŒํŠธ์— ์ ์šฉํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? ๊ฒ€์ƒ‰์„ ํ†ตํ•ด ์•Œ์•„๋ณด๊ณ  ์ ์šฉํ•ด๋ณด์„ธ์š”.

const GlobalStyle = createGlobalStyle`
	button {
		margin: 0.5rem;
  }
  `

const Button = styled.button`
  padding: 1rem;
  font-size: 2rem;
  background: powderblue;
  border-radius: 1rem;
  transition: 0.5s;
  &:hover{  
    background: cornflowerblue;
    color: white;
    transition: 0.5s;
  }
`;


export default function App() {
  return (
    <>
    <GlobalStyle />
    <Button>Practice!</Button>
    </>
    );
}




Reference: ์ฝ”๋“œ์Šคํ…Œ์ด์ธ 

0๊ฐœ์˜ ๋Œ“๊ธ€