Styled Components
๋ ธ๋ง๋์ฝ๋ - React JS ๋ง์คํฐ ํด๋์ค https://styled-components.com/
Install
CRA
ํ์npm i styled-components
Using
1. import
import styled from "styled-components";
2. Create styled components
๋ฐฑํฑ
์ฌ์ด์๋css
๊ฐ ๋ค์ด๊ฐvscode ์ต์คํ ์ ์ค์นํ๋ฉด ์๋์์ฑ
ํด๋์ค ๋ค์์ ๋๋คํ๊ฒ ์ง์ ๋จ
const Father = styled.div`
display: flex;
`;
const Btn = styled.button`
color: white;
background-color: tomato;
border: 0;
border-radius: 15px;
`;
}
3. Adapting and Extending
adapting(modify)
props
์ฌ์ฉ ๊ฐ๋ฅ
const Box = styled.div`
background-color: ${(props) => props.bgColor}
`;
...์ค๋ต
<Box bgColor="tomato" />
extending
styled(๊ธฐ์กด์ปดํฌ๋ํธ)
๊ธฐ์กด ์ปดํฌ๋ํธ์ ๋ชจ๋ ์์ฑ์ ์๋ก์ด ์์ฑ์ ๋ ํด์ค
const Circle = styled(Box)`
border-radius: 50px;
`;
...์ค๋ต
<Circle bgColor="teal" />
4. 'As' and Attrs
as
๋ผ๋ props๋ฅผ ์ด์ฉํ๋ฉด ์ ์๋ ์คํ์ผ์ ๊ทธ๋๋ก ๊ฐ์ง๊ณ ๊ฐ๋ ํ๊ทธ๋ค์์ ๋ฐ๊ฟ ์ ์์
const Btn = styled.button`
color: white;
background-color: tomato;
border: 0;
border-radius: 15px;
`;
...์ค๋ต
<Btn>Log In</Btn>
<Btn as="a" href="#">
Link
</Btn>
attrs
๋ฅผ ์ด์ฉํ๋ฉด html์ ์์ฑ์ ๋ฏธ๋ฆฌ ์ค์ ํ ์ ์์์ฌ๋ฌ๊ฐ์ ๋์ผํ ์ค์ ์ด ํ์ํ ๊ฒฝ์ฐ ์ ์ฉ
const Input = styled.input.attrs({ required: true, minLength: 10 })`
background-color: tomato;
`;
5. Animations and Pseudo Selectors
animations
keyframes
๋ผ๋ ํฌํผ ํจ์๋ฅผ ์ถ๊ฐ๋ฐ์์ animation์ ์ ์ธํ๊ณ
styled-components
์์์ ์ฌ์ฉ
import styled, { keyframes } from "styled-components";
const rotate = keyframes`
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
`;
const Box = styled.div`
width: 200px;
height: 200px;
background-color: tomato;
animation: ${rotate} 1s linear infinite;
`;
seudo Selectors
styled-components
๊ฐ ์๋๋๋ผ๋ ์ปดํฌ๋ํธ ๋ด์์ ํ์ ํ๊ทธ์ ๋ํ ํ๊ฒ ์ ํ ๊ฐ๋ฅ์ถ์ฝ์ด
&
case1
const Box = styled.div`
...์ค๋ต
span {
font-size: 36px;
&:hover {
font-size: 40px;
}
}
`;
...์ค๋ต
<Box>
<span>๐ค</span>
</Box>
styled-components
๋ด์์ ํ์ ์ปดํฌ๋ํธ์ ๋ํ ํ๊ฒ ์ ํ ๊ฐ๋ฅ(๋ณ์๋ช ์ฒ๋ผ ์ฌ์ฉ)case2
const Emoji = styled.span`
font-size: 36px;
`;
const Box = styled.div`
...์ค๋ต
${Emoji}:hover {
font-size: 98px;
}
`;
...์ค๋ต
<Box>
<Emoji as="p">๐ค</Emoji>
</Box>
Themes
๋ชจ๋ ์์์ ๊ฐ์ง๊ณ ์๋ object
index.js์์ ์ค์ ํ๊ณ App.js์์
props
๋ก ๊ฐ์ ธ๋ค ์์์ ์ฟจํจ๐คฉ
Last updated