let's get IT with DAVINA ๐Ÿ’ป

React-Styled Components ๋ณธ๋ฌธ

DEV_IN/CSS

React-Styled Components

๋‹ค๋นˆ์น˜์ฝ”๋“œ๐Ÿ’Ž 2023. 2. 8. 00:56

Styled Components

  • ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ css๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
  • ์šฐ๋ฆฌ๊ฐ€ ์•Œ๊ณ ์žˆ๋Š” css๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • css๋ฅผ ์ปดํฌ๋„ŒํŠธ ์•ˆ์œผ๋กœ ์บก์Šํ™”ํ•˜์—ฌ ๋„ค์ด๋ฐ์ด๋‚˜ ์ตœ์ ํ™”๋ฅผ ์‹ ๊ฒฝ ์“ธ ํ•„์š”๊ฐ€ ์—†์Œ
  • ๋น„๊ต์  ๋น ๋ฅธ ํŽ˜์ด์ง€ ๋กœ๋“œ์— ๋ถˆ๋ฆฌ

Styled Components ์„ค์น˜๋ฒ•

  • ํ„ฐ๋ฏธ๋„์—์„œ Styled Components ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜
# with npm
$ npm install --save styled-components

# with yarn
$ yarn add styled-components
  • package.json์— ์ƒ๊ธฐ ์ฝ”๋“œ ์ถ”๊ฐ€ ๊ถŒ์žฅ (์—ฌ๋Ÿฌ ๋ฒ„์ „ ์„ค์น˜ ๋ฐฉ์ง€)
{
  "resolutions": {
    "styled-components": "^5"
  }
}
  • ํ•„์š”ํ• ๋•Œ๋งˆ๋‹ค importํ•ด์„œ ์‚ฌ์šฉ
import styled from "styled-components"

Styled Components ์‚ฌ์šฉ๋ฒ•

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

์ „์—ญ์Šคํƒ€์ผ์€ ๋ชจ๋“  html ์š”์†Œ์— ๋Œ€ํ•ด์„œ ๊ณตํ†ต์ ์ธ ์Šคํƒ€์ผ์„ ๊ฐ€์ง„๋‹ค.

ex)

  • ๋ชจ๋“  li ํƒœ๊ทธ์—์„œ list-style ์ œ๊ฑฐํ•˜๊ธฐ
  • a ํƒœ๊ทธ์—์„œ text-decoration ์ œ๊ฑฐํ•˜๊ธฐ
  • button ํƒœ๊ทธ์— cursor: pointer ์ง€์ •ํ•˜๊ธฐ

reset CSS๋ฅผ ํ†ตํ•ด ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ ๐Ÿ”ฝ

  • src/style/GlobalStyle.jsx
import { createGlobalStyle } from "styled-components";
import reset from "styled-reset";

const GlobalStyle = createGlobalStyle`
  ${reset}
  :root {
    *{
      box-sizing: border-box;
      outline: none;
      list-style: none;
      text-decoration: none;
      font-family: 'Noto Sans KR', sans-serif;
      ::-webkit-scrollbar {
      display: none;
      }
      user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    }
  }

`;

export default GlobalStyle;

theme ์ง€์ • (๋‚ด๊ฐ€ ์“ธ ์ƒ‰์ƒ๋“ค ํŒ”๋ ˆํŠธ ๋งŒ๋“ค๊ธฐ)

  • src/style/Theme.jsx
export const colorTheme = {
  darkBlack: "#000",
  mediumBlack: "#333",
  lightBlack: "#4a4a4a",
  darkGrey: "#999",
  mediumGrey: "#a4a4a4",
  lightGrey: "#ccc",
  white: "#fff",
  pointColor: "#20b2aa",
};

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

Styled Components๋Š” ES6์˜ Templete Literals ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋”ฐ์˜ดํ‘œ๊ฐ€ ์•„๋‹Œ ๋ฐฑํ‹ฑ(`)์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

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

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

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

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

const ์ปดํฌ๋„ŒํŠธ์ด๋ฆ„ = styled(์žฌํ™œ์šฉํ•  ์ปดํฌ๋„ŒํŠธ)`
	์ถ”๊ฐ€ํ•  css์†์„ฑ1: ์†์„ฑ๊ฐ’;
	์ถ”๊ฐ€ํ•  css์†์„ฑ2: ์†์„ฑ๊ฐ’;
`
//๋งŒ๋“ค์–ด์ง„ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์žฌํ™œ์šฉํ•ด ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
const BigBlueButton = styled(BlueButton)`
  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>
    </>
  );
}

4. Props ํ™œ์šฉํ•˜๊ธฐ

Styled Components๋Š” ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด ๋ฌธ๋ฒ•( ${ } )์„ ์‚ฌ์šฉํ•˜์—ฌ JavaScript ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. props๋ฅผ ๋ฐ›์•„์˜ค๋ ค๋ฉด props๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

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

import styled from "styled-components";
import GlobalStyle from "./GlobalStyle";
//๋ฐ›์•„์˜จ prop์— ๋”ฐ๋ผ ์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋ง์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
const Button1 = styled.button`
  background: ${(props) => (props.skyblue ? "skyblue" : "white")};
`;

export default function App() {
  return (
    <>
      <GlobalStyle />
      <Button1>Button1</Button1>
      <Button1 skyblue>Button1</Button1>
    </>
  );
}

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

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

5. ThemeProvider ์‚ฌ์šฉํ•˜๊ธฐ

  • styled-components์—์„œ ThemeProvider๋ฅผ importํ•œ๋‹ค.
import { ThemeProvider } from "styled-components";
  • ThemeProvider ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฐ€์žฅ ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐฐ์น˜ํ•œ๋‹ค. (GlobalStyle๋ณด๋‹ค ์œ„์—)
function App() {
	return (
		<ThemeProvider>
			<GlobalStyle />
			<BrowserRouter>
				<Switch>
					<Route path="/login">
						<LogIn />
					</Route>
					<Route path="/">
						<Home />
					</Route>
				</Switch>
			</BrowserRouter>
		</ThemeProvider>
	);
}
  • theme์ด๋ผ๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง„ ๋น„์–ด์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค.
const theme = {}
  • ์ƒ์„ฑํ•œ theme ๊ฐ์ฒด๋ฅผ ThemeProvider์˜ props๋กœ ๋„ฃ์–ด์ค€๋‹ค.
    • ๊ทธ๋Ÿฌ๋ฉด styled-components๊ฐ€ ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ์— ์ด theme ๋ณ€์ˆ˜๋ฅผ injectํ•ด์ค€๋‹ค.
function App() {
	return (
		<ThemeProvider theme={theme}>
			<GlobalStyle />
			<BrowserRouter>
				<Switch>
					<Route path="/login">
						<LogIn />
					</Route>
					<Route path="/">
						<Home />
					</Route>
				</Switch>
			</BrowserRouter>
		</ThemeProvider>
	);
}
  • ์›ํ•˜๋Š” theme ์ž‘์„ฑํ•˜๊ธฐ (camelCase)
const theme = {
	primaryColor: "#f8049c",
	secondaryColor: " #fdd54f",
};

์ ์šฉ์€ ๐Ÿ”ฝ

${props => props.theme.primaryColor}

//ex)
background-color: ${(props)=>
	props.secondary ? props.theme.secondaryColor : props.themeprimaryColor};

border-bottom: 3px solid ${(props) => props.theme.secondaryColor};

Styled Component ์ด๋ฒคํŠธ ์ฃผ๊ธฐ

  • App.js
import styled from "styled-components"; //์Šคํƒ€์ผ๋“œ์ปดํฌ๋„ŒํŠธ ์ ์šฉ
import GlobalStyle from "./GlobalStyle"; //์ „์—ญ์Šคํƒ€์ผ ์ ์šฉ

const BlueButton = styled.button`
  background-color: powderblue;
  padding: 1rem;
  font-size: 2rem;
  border-radius: 1rem;
  transition: 0.5s;
//hover ์ด๋ฒคํŠธ ์ฃผ๊ธฐ!!!
  &:hover {
    background: cornflowerblue;
    color: white;
    transition: 0.5s;
  }
`;

export default function App() {
  return (
    <>
      <GlobalStyle />
      <BlueButton>Practice!</BlueButton>
    </>
  );
}
  • GlobalStyle.js → ์ „์—ญ์Šคํƒ€์ผ ์„ค์ •ํ•˜๊ธฐ
import { createGlobalStyle } from "styled-components";

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

export default GlobalStyle;

'DEV_IN > CSS' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

div์— img ๋„ฃ๊ธฐ (props)  (0) 2023.02.09
๊ธ€์ž ์ˆ˜ ์ดˆ๊ณผ์‹œ?  (0) 2023.02.09
Position  (0) 2023.02.09
GMT/UTC time ํ˜•์‹ โ€œ2023-02-02โ€ format์œผ๋กœ change  (0) 2023.02.08
react-datepicker  (2) 2023.02.08
Comments