ํ๋ก ํธ์๋/React
styled-components
alswlfl
2022. 12. 8. 14:49
styled-components: JS ์์ CSS๋ฅผ ์์ฑํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ (CSS in JS ๊ด๋ จ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค ๊ฐ์ฅ ์ธ๊ธฐ ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ)
→ emotion, styled-jsx
Tagged Template Literal
: ๋ฌธ์์ด ์กฐํฉ์ ๋์ฑ ์ฝ๊ฒ ํ ์ ์๊ฒ ํด์ฃผ๋ ES6 ๋ฌธ๋ฒ
- Template Literal ์ฌ์ฉ ํ ๋, ${} ์์ ์ผ๋ฐ ๋ฌธ์์ด์ด๋ ์ซ์๊ฐ ์๋ ๊ฐ์ฒด๋ฅผ ๋ฃ๋ ๊ฒฝ์ฐ
const object = {a:1};
const text = `${object}`
console.log(text);
// "[object Object]"
Tmeplate Literal: ๋ด์ฅ๋ ํํ์์ ํ์ฉํ๋ ๋ฌธ์์ด ๋ฆฌํฐ๋ด → ์ฌ๋ฌ ์ค๋ก ์ด๋ค์ง ๋ฌธ์์ด๊ณผ ๋ฌธ์ ๋ณด๊ฐ ๊ธฐ๋ฅ ์ฌ์ฉ ๊ฐ๋ฅ
• ๋ฐฑํฑ(` `
- Template Literal์ ํจ์ ๋ฃ๋ ๊ฒฝ์ฐ
const fn = () => true
const msg = `${fn}`;
console.log(msg);
// "() => true"
Template Literal ์ฌ์ฉํ๋ฉด์, ๊ทธ ๋ด๋ถ์ ๋ฃ์ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ ์กฐํํ๊ฑฐ๋ ํจ์ ์ฌ์ฉํ๊ณ ์ถ์ ๊ฒฝ์ฐ Tagged Template Literal ๋ฌธ๋ฒ ์ฌ์ฉ
const red='๋นจ๊ฐ์';
const blue='ํ๋์';
function favoriteColors(texts, ...values){
console.log(texts);
console.log(values);
}
favoriteColors `์ ๊ฐ ์ข์ํ๋ ์์ ${red}๊ณผ ${blue}์
๋๋ค.`
styled-components ์ฌ์ฉ
- ์คํ์ผ์ ์ ๋ ฅํจ๊ณผ ๋์์ ํด๋น ์คํ์ผ์ ๊ฐ์ง ์ปดํฌ๋ํธ ๋ง๋ค ์ ์์
- ex) div ์คํ์ผ๋งํ๊ณ ์ถ์ผ๋ฉด styled.div, input ์คํ์ผ๋งํ๊ณ ์ถ์ผ๋ฉด styled.input
import React from "react";
import styled from "styled-components";
const Circle = styled.div`
witdh: 5rem;
height: 5rem;
background: black;
border-radius: 50%;
`;
function App() {
return <Circle />;
}
export default App;
- props ๋ฃ๊ธฐ
const Circle = styled.div`
witdh: 5rem;
height: 5rem;
background: ${(props) => props.color || "black"};
border-radius: 50%;
`;
→ props ๊ฐ์ ์ค์ ํด์คฌ์ผ๋ฉด, ํด๋น ๊ฐ์ ๋ฐฐ๊ฒฝ์์ผ๋ก ์ค์ ํ๊ณ ์ค์ ์ํ์ผ๋ฉด ๋ฐฐ๊ฒฝ์์ ๊ฒ์ ์์ผ๋ก ์ฌ์ฉํ๋๋ก ์ค์
- ์ฌ๋ฌ ์ค์ CSS ์ฝ๋๋ฅผ ์กฐ๊ฑด๋ถ๋ก ๋ณด์ฌ์ฃผ๊ณ ์ถ์ ๊ฒฝ์ฐ, css ์ฌ์ฉ(์คํ์ผ ๋ด๋ถ์์๋ ๋ค๋ฅธ props ์กฐํ ๊ฐ๋ฅ)
const Circle = styled.div`
witdh: 5rem;
height: 5rem;
background: ${(props) => props.color || "black"};
border-radius: 50%;
${(props) =>
props.huge &&
css`
width: 10rem;
height: 10rem;
`}
`;
- polished ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์คํ์ผ ๊ด๋ จ ์ ํธ ํจ์ ์ฌ์ฉ
$ yarn add polished
import { darken, lighten } from "polished";
const StyledButton = styled.button`
/*์์*/
background: #228be6;
&:hover {
background: ${lighten(0.1, "#228be6")};
}
&:active {
background: ${darken(0.1, "#228be6")};
}
`;
- ThemeProvider ๊ธฐ๋ฅ ์ฌ์ฉํ์ฌ styled-component๋ก ๋ง๋๋ ๋ชจ๋ ์ปดํฌ๋ํธ์์ ์กฐํํ์ฌ ์ฌ์ฉํ ์ ์๋ ์ ์ญ์ ์ธ ๊ฐ ์ค์
import React from "react";
import styled, { ThemeProvider } from "styled-components";
import Button from "./components/Button";
const AppBlock = styled.div`
width: 512px;
margin: 0 auto;
margin-top: 4rem;
border: 1px solid black;
padding: 1rem;
`;
function App() {
return (
<ThemeProvider
theme={{
palette: {
blue: "#228be6",
gray: "#495057",
pink: "#f06595",
},
}}
>
<AppBlock>
<Button>BUTTON</Button>
</AppBlock>
</ThemeProvider>
);
}
export default App;
→ theme ์ค์ ํ๋ฉด ThemeProvider ๋ด๋ถ์ ๋ ๋๋ง๋ styled-components๋ก ๋ง๋ ์ปดํฌ๋ํธ์์ palette๋ฅผ ์กฐํํ์ฌ ์ฌ์ฉ ๊ฐ๋ฅ
${(props) => {
const selected = props.theme.palette[props.color];
return css`
background: ${selected};
&:hover {
background: ${lighten(0.1, selected)};
}
&:active {
background: ${darken(0.1, selected)};
}
`;
}}
→ ThemeProvider๋ก ์ค์ ํ ๊ฐ์ styled-component์์ props.theme๋ก ์กฐํ ๊ฐ๋ฅ
- ํธ๋์ง์
ํจ๊ณผ → CSS Keyframe ์ฌ์ฉ
- ์ฌ๋ผ์ง๋ ํจ๊ณผ ๊ตฌํ(๋ ๊ฐ์ ๋ก์ปฌ ์ํ ๊ด๋ฆฌ ํ์)
- animate: ํ์ฌ ํธ๋์ง์ ํจ๊ณผ๋ฅผ ๋ณด์ฌ์ฃผ๊ณ ์๋ ์ค์ด๋ผ๋ ์ํ๋ฅผ ์๋ฏธ
- localVisible: ์ค์ ๋ก ์ปดํฌ๋ํธ๊ฐ ์ฌ๋ผ์ง๋ ์์ ์ ์ง์ฐ์ํค๊ธฐ ์ํ ๊ฐ
- ์ฌ๋ผ์ง๋ ํจ๊ณผ ๊ตฌํ(๋ ๊ฐ์ ๋ก์ปฌ ์ํ ๊ด๋ฆฌ ํ์)