styled-components

Doozuuยท2023๋…„ 2์›” 17์ผ
0

CSS

๋ชฉ๋ก ๋ณด๊ธฐ
6/8

๐Ÿ’…๐Ÿฝ styled-components

์‹ค์ˆ˜๋กœ ์—ฌ๋Ÿฌ ํŒŒ์ผ์—์„œ ๊ฐ™์€ ํด๋ž˜์Šค๋ช…์„ ์ด์šฉํ•ด ํด๋ž˜์Šค๋ช…์ด ๊ฒน์น˜๊ฒŒ ๋˜๋ฉด ์Šคํƒ€์ผ์ด ์ถฉ๋Œํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.
์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š๊ณ  ์Šคํƒ€์ผ์„ ์ ์šฉ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๊ฐ€ styled-components์ด๋‹ค.

https://styled-components.com/


์žฅ์ 

๋ชจ๋“  ๊ตฌ์„ฑ ์š”์†Œ๋งˆ๋‹ค ๊ณ ์œ ํ•œ ํด๋ž˜์Šค๋ช…์„ ๋žœ๋คํ•˜๊ฒŒ ์ƒ์„ฑํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ํด๋ž˜์Šค๋ช…์ด ๊ฒน์น  ๊ฑฑ์ •์ด ์—†๋‹ค.
(+ ํด๋ž˜์Šค๋ช… ์ง“๋Š๋ผ ๊ณ ์ƒ ์•ˆํ•ด๋„ ๋œ๋‹ค!)
ํƒœ๊ทธ๋ฅผ css๋กœ ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

ํŠน์ง•

ํŒŒ์ผ ํ˜•์‹์ด js ํ˜น์€ jsx์ด๋‹ค.
(์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ์ž‘์„ฑํ•˜๋Š” css์ด๊ธฐ ๋•Œ๋ฌธ.)

์ฃผ์˜์‚ฌํ•ญ

โญ๏ธ ์Šคํƒ€์ผ ๊ตฌ์„ฑ ์š”์†Œ์˜ ์ˆœ์„œ๋ฅผ ์ž˜ ์ •ํ•ด์•ผ ํ•œ๋‹ค.

-> ๋‚˜์ค‘์— ํƒ€๊ฒŸ์ด ๋  ๊ฒƒ์„ ๋จผ์ € ์„ ์–ธํ•˜๊ธฐ
ex) container ์•ˆ์— button๊ณผ span์— ๊ด€ํ•œ style์ด ์žˆ์œผ๋ฉด, container๋ฅผ ์„ ์–ธํ•˜๊ธฐ ์ „์— button๊ณผ span์„ ๋จผ์ € ์„ ์–ธํ•ด์ฃผ์–ด์•ผ ๋‚˜์ค‘์— container์—์„œ button๊ณผ span์„ ์“ธ ๋•Œ ์ž˜ ์ธ์‹๋œ๋‹ค.



๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜

yarn add styled-components



์‚ฌ์šฉ ๋ฐฉ์‹

  1. ํŒŒ์ผํ˜•์‹์„ js ๋‚˜ jsx๋กœ ์„ค์ •ํ•œ๋‹ค.
    ํŒŒ์ผ๋ช….styles.jsx

  2. ์ƒ๋‹จ์— styled๋ฅผ import ํ•œ๋‹ค.

import styled from "styled-components";
  1. component ๋งŒ๋“ค๋“ฏ์ด ๋งŒ๋“ค์–ด์„œ exportํ•œ๋‹ค.
    ๋ช…๋ช…๊ทœ์น™๋„ React component ๋ช…๋ช…๊ทœ์น™์— ๋”ฐ๋ฅธ๋‹ค.

์˜ˆ์‹œ

styled.ํƒœ๊ทธ๋ช…'๋‚ด์šฉ'

๋ฐฑํ‹ฑ๊ธฐํ˜ธ ์‚ฌ์šฉ.
ex. styled.buttonmargin:0

import styled from "styled-components";

export const NavigationContainer = styled.div`
  height: 70px;
  width: 100%;
  display: flex;
  justify-content: space-between;
  margin-bottom: 25px;
`;

์˜ˆ์‹œ

styled(ํƒœ๊ทธ์™ธ์š”์†Œ)'๋‚ด์šฉ'

Link ๊ฐ™์€ ๊ฒƒ๋„ ๊พธ๋ฐ€ ์ˆ˜ ์žˆ๋‹ค.
. ๋Œ€์‹  ( ) ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ์œ ์˜ํ•˜๊ธฐ.
ex. styled(Link)margin:0

import styled from "styled-components";
import { Link } from "react-router-dom"; // ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด import

export const LogoContainer = styled(Link)`
  height: 100%;
  width: 70px;
  padding: 25px;
`;
  1. ์Šคํƒ€์ผ ์ ์šฉํ•  ๊ณณ์— import ํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ
import { NavigationContainer, LogoContainer} from "./navigation.styles.jsx";

<NavigationContainer>
   <LogoContainer to='/'>
     <CrwnLogo className="logo" />
   </LogoContainer>
<NavigationContainer/>



ํ™œ์šฉ

์Šคํƒ€์ผ์€ ์œ ์ง€ํ•˜๊ณ  ํƒœ๊ทธ๋งŒ ๋ฐ”๊พธ๊ณ  ์‹ถ์„ ๋•Œ

as = 'ํƒœ๊ทธ๋ช…'

as ์†์„ฑ์„ ์ด์šฉํ•ด ํƒœ๊ทธ๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.

import { NavigationContainer } from "./navigation.styles.jsx";

<NavigationContainer as='span'> // div -> span ์œผ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Œ.
   <LogoContainer to='/'>
     <CrwnLogo className="logo" />
   </LogoContainer>
<NavigationContainer/>



๊ณตํ†ต๋œ ์Šคํƒ€์ผ์„ ๋ฌผ๋ ค๋ฐ›๊ณ  ์‹ถ์„ ๋•Œ

styled(๋ฌผ๋ ค๋ฐ›์„ ์ปดํฌ๋„ŒํŠธ)'๋‚ด์šฉ'

import styled from "styled-components";

export const BaseBtn = styled.button`
  min-width: 165px;
  width: auto;
  height: 50px;
  letter-spacing: 0.5px;
  line-height: 50px;
  padding: 0 35px 0 35px;
`;

export const GoogleSignInBtn = styled(BaseBtn)` // Button ๊ธฐ๋ณธ ์Šคํƒ€์ผ ๋ฌผ๋ ค๋ฐ›๊ธฐ
  background-color: #4285f4;
  color: white;

  &:hover {
    background-color: #357ae8;
    border: none;
  }
`;



SVG ํŒŒ์ผ ๋‹ค๋ฃจ๋Š” ๋ฒ•

svg๋ฅผ importํ•ด์™€์„œ ์†Œ๊ด„ํ˜ธ ์•ˆ์— ๋„ฃ์–ด์“ฐ๊ธฐ

styled(svg๋ช…)'๋‚ด์šฉ'

import styled from "styled-components";
import { ReactComponent as ShoppingSvg } from "../../assets/shopping-bag.svg";

export const ShoppingIcon = styled(ShoppingSvg)`
  width: 24px;
  height: 24px;
`;



props ๋ฐ›์•„์™€์„œ ์“ฐ๋Š” ๋ฒ•

props๋กœ imageUrl ์ „๋‹ฌ๋ฐ›์Œ.

 <div className="background-image" imageUrl={imageUrl} />

background image์˜ url๋กœ ๋„ฃ์–ด์คŒ.

import styled from "styled-components";

export const BackgroundImage = styled.div`
  width: 100%;
  height: 100%;
  background-size: cover;
  background-position: center;
  background-image: ${({ imageUrl }) => `url(${imageUrl})`};
`;



CSS ์บก์Šํ™”

css๋ฅผ ์บก์Šํ™” ํ•˜์—ฌ ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

import { css } from "styled-components";

์˜ˆ์‹œ

import styled, { css } from "styled-components";

const subColor = "grey";
const mainColor = "black";

// css ์บก์Šํ™”
const shrinkLabel = css`
  top: -14px;
  font-size: 12px;
  color: ${mainColor};
`;

export const FormInputLabel = styled.label`
  color: ${subColor};
  font-size: 16px;
  font-weight: normal;
  position: absolute;
  pointer-events: none;
  left: 5px;
  top: 10px;
  transition: 300ms ease all;

  ${({ shrink }) => shrink && shrinkLabel} // ์‚ฌ์šฉ
`;

์ด๋ฏธ์ง€ ์ ์šฉํ•˜๊ธฐ
https://velog.io/@shinwonse/React-styled-components%EC%97%90%EC%84%9C-%EC%9D%B4%EB%AF%B8%EC%A7%80

profile
๋ชจ๋“ ๊ฒŒ ์ƒˆ๋กญ๊ณ  ์žฌ๋ฐŒ๋Š” ํ”„๋ก ํŠธ์—”๋“œ ์ƒˆ์‹น

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