๐Ÿ’…๐Ÿป Styled Components

์ง€์€ยท2022๋…„ 10์›” 27์ผ
1

Node.js Library

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

Styled Components

: CSS๋ฅผ ์ปดํฌ๋„ŒํŠธํ™” ์‹œํ‚ด์œผ๋กœ์จ CSS ์ฝ”๋“œ์˜ ๋ถˆํŽธํ•จ์„ ํ•ด๊ฒฐํ•ด์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

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

Styled-Component


Styled Components ์„ค์น˜

  1. npm install ๋ช…๋ น์–ด๋กœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋‹ค์šด๋ฐ›๋Š”๋‹ค.
npm install styled-components
  1. ์—ฌ๋Ÿฌ ๋ฒ„์ „์˜ Styled Components๊ฐ€ ์„ค์น˜๋˜์–ด ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด package.json ํŒŒ์ผ์— ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. (๊ถŒ์žฅ ์‚ฌํ•ญ)
{
  "resolution": {
  	"styled-components": "^5"
  }
}
  1. Styled Components๋ฅผ ์‚ฌ์šฉํ•  ํŒŒ์ผ์—์„œ importํ•ด์ค€๋‹ค.
import styled from "styled-components';

Styled Components ๋ฌธ๋ฒ•

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

const ์ปดํฌ๋„ŒํŠธ๋ช… = styled.ํƒœ๊ทธ์ข…๋ฅ˜`
  // ์—ฌ๊ธฐ์— CSS ์ฝ”๋“œ ์ž‘์„ฑ
`;
  • Styled Components๋Š” ES6์˜ ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•œ๋‹ค.
  • ์ปดํฌ๋„ŒํŠธ๋ฅผ ์„ ์–ธํ•œ ํ›„ styled.ํƒœ๊ทธ์ข…๋ฅ˜๋ฅผ ํ• ๋‹นํ•˜๊ณ , ๋ฐฑํ‹ฑ(`) ์•ˆ์— CSS ๋ฌธ๋ฒ•์„ ์ž‘์„ฑํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
  • ์ด๋ ‡๊ฒŒ ๋งŒ๋“  ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ฆฌํ„ด๋ฌธ์— ์ž‘์„ฑํ•ด์ฃผ๋ฉด, ์Šคํƒ€์ผ์ด ์ ์šฉ๋œ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋ง๋œ๋‹ค.
const BlueButton = styled.button`
  background-color: blue;
  color: white;
  padding: 15px;
  border: none;
  border-radius: 15px;
`;
// ์ƒ๋žต
return (
  <BlueButton>Click Me</BlueButton>
);


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

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

const ์ปดํฌ๋„ŒํŠธ๋ช… = styled(์žฌํ™œ์šฉํ•  ์ปดํฌ๋„ŒํŠธ)`
  // ์ถ”๊ฐ€ํ•  CSS ์†์„ฑ
`;
const GreenButton = styled(BlueButton)`
  background-color: green;
`;


3. Props ์ „๋‹ฌํ•˜๊ธฐ

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

const ์ปดํฌ๋„ŒํŠธ๋ช… = styled.ํƒœ๊ทธ์ข…๋ฅ˜`
  CSS์†์„ฑ: ${(props) => ํ•จ์ˆ˜ ์ฝ”๋“œ}
`;
  • Styled Components๋Š” ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด ๋ฌธ๋ฒ•(${ })์„ ์‚ฌ์šฉํ•ด์„œ JavaScript ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
  • props๋ฅผ ๋ฐ›์•„์˜ค๊ธฐ ์œ„ํ•ด์„œ๋Š”, props๋ฅผ ์ „๋‹ฌ์ธ์ž๋กœ ๋ฐ›๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•œ๋‹ค.

1) Props๋กœ ์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋งํ•˜๊ธฐ

์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด ์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋ง์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

const Button = styled.button`
  width: ${(props) => props.big ? "120px" : "80px"};
`;
// ์ƒ๋žต
return (
  <>
    <Button>Button 1</Button>
    <Button big>Button 2</Button>
  </>
);
// ์ค‘๊ด„ํ˜ธ{}๋ฅผ ์ด์šฉํ•ด ํŠน์ • props๋งŒ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜๋„ ์žˆ๋‹ค.
width: ${({big}) => big ? "120px" : "80px"};
// ์กฐ๊ฑด๋ฌธ ์ „์ฒด๋ฅผ ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด๋กœ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
${(props) => props.big ? "width: 120px" : "width: 80px"}

2) Props ๊ฐ’์œผ๋กœ ๋ Œ๋”๋งํ•˜๊ธฐ

props ๊ฐ’์„ ํ†ต์งธ๋กœ ํ™œ์šฉํ•ด์„œ ์ปดํฌ๋„ŒํŠธ ๋ Œ๋”๋ง์— ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

const Button = styled.button`
  background-color: ${(props) => props.color ? props.color : "lightgrey"};
`;
// ์ƒ๋žต
return (
  <>
    <Button color="blue">Button 1</Button>
    <Button color="green">Button 2</Button>
    <Button>Button 3</Button>
  </>
);

์‚ผํ•ญ์—ฐ์‚ฐ์ž ๋Œ€์‹  OR ์—ฐ์‚ฐ์ž(||)๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
JavaScript ์ฝ”๋“œ๋ผ๋ฉด ๋ฌด์—‡์ด๋“  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ํ•จ์ˆ˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

const Button = styled.button`
  background-color: ${(props) => props.color || "lightgrey"};
// OR ์—ฐ์‚ฐ์ž๋Š” ๋‘˜ ์ค‘์— ํ•˜๋‚˜๋Š” ์ฐธ์ด์–ด์•ผ ํ•˜๋ฏ€๋กœ, props.color๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด lightgrey๊ฐ€ ๋œ๋‹ค.
`;

4. ์ „์—ญ ์Šคํƒ€์ผ ์„ค์ •ํ•˜๊ธฐ

์ „์—ญ ์Šคํƒ€์ผ์„ ์„ค์ •ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด createGlobalStyle ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

import { createGlobalStyle } from "styled-components";
  • ์ปดํฌ๋„ŒํŠธ๋ฅผ ์„ ์–ธํ•œ ํ›„ createGlobalStyle ํ•จ์ˆ˜๋ฅผ ํ• ๋‹นํ•˜๊ณ , CSS ํŒŒ์ผ์—์„œ ์ž‘์„ฑํ•˜๋“ฏ์ด ์ „์—ญ์— ์ง€์ •ํ•ด์ฃผ๊ณ  ์‹ถ์€ ์Šคํƒ€์ผ์„ ์ž‘์„ฑํ•œ๋‹ค.
  • ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด์ง„ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ตœ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉํ•ด์ฃผ๋ฉด, ์ „์—ญ์— ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์˜ ์Šคํƒ€์ผ์ด ์ ์šฉ๋œ๋‹ค.
const GlobalStyle = createGlobalStyle`
  button {
    margin: 10px;
	box-shadow: inset 0px -3px rgba(0, 0, 0, 0.5)
  }
`;
// ์ƒ๋žต
return (
  <>
    <GlobalStyle /> // ์ตœ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์— ์ž‘์„ฑ
    <Button color="blue">Button 1</Button>
    <Button color="green">Button 2</Button>
    <Button>Button 3</Button>
  </>
);


The component styled.div with the id of ___ has been created dynamically ๊ฒฝ๊ณ 

The component styled.div with the id of "___" has been created dynamically. You may see this warning because you've called styled inside another component. To resolve this only create new StyledComponents outside of any render method and function component.

๋‚ด๊ฐ€ ๋‘ ๋ฒˆ์ด๋‚˜ ๋ฒ”ํ•œ ์˜ค๋ฅ˜๋กœ.. styled component๋ฅผ ์ •์˜ํ•  ๋•Œ, ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ ์ •์˜ํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ, ๋ฐ–์— ์ •์˜ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

ํ•ด์„

___๋ผ๋Š” id๋ฅผ ๊ฐ€์ง„ styled div ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋™์ ์œผ๋กœ ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
๋‹น์‹ ์ด ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ ์•ˆ์— styled๋ฅผ ํ˜ธ์ถœํ–ˆ์„ ๋•Œ ์ด ๊ฒฝ๊ณ ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
์ด ์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋ Œ๋” ๋ฉ”์†Œ๋“œ๋‚˜ ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ "๋ฐ”๊นฅ์—" styled ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ƒ์„ฑํ•˜์‹ญ์‹œ์˜ค.

์˜ˆ์‹œ

const Wrapper = styled.section`
  height: 100vh;
  background-color: #d6e4e5;

  display: flex;
  justify-content: center;
  align-items: center;
`;

const Conatiner = styled.div`
  width: 100%;
  max-width: 500px;
  min-width: 300px;
  border-radius: 5px;
  background-color: white;
  position: relative;
  box-shadow: 0px 20px 40px rgba(82, 97, 107, 0.2);
`;

export default function App() {
  /* ์ด ์•ˆ์—์„œ const ~ styled ์ปดํฌ๋„ŒํŠธ ์„ ์–ธํ•  ๊ฒฝ์šฐ ํ•ด๋‹น ๊ฒฝ๊ณ ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. */
  return (
    <Wrapper>
      <Conatiner>
        {data.map((d) => (
          <Menu key={d.city} city={d.city} description={d.description} />
        ))}
      </Conatiner>
    </Wrapper>
  );
}

์ถœ์ฒ˜ : https://github.com/styled-components/styled-components/issues/3117

profile
๋ธ”๋กœ๊ทธ ์ด์ „ -> https://janechun.tistory.com

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