애니메이션을 만들어보자
import styled from "styled-components";
const Box = styled.div`
height: 200px;
width: 200px;
background-color: tomato;
`;
function App() {
return
<Wrapper>
<Box />
</Wrapper>
}
keyframes 가 필요하다.import styled, { keyframes } from "styled-components";
const rotationAnimation = keyframes` <<<----
from {
transfrom : rotate(0deg);
} to {
transfrom : rotate(360deg);
}
`;
const Box = styled.div`
height: 200px;
width: 200px;
background-color: tomato;
animation: ${rotationAnimation} 1s linear infinite; <<<----
`;
linear : 일정한 속도로 진행infinite : 무한반복const Box = styled.div`
height: 200px;
width: 200px;
background-color: tomato;
animation: ${rotationAnimation} 1s linear infinite;
display: flex;
justify-content: center;
align-items: center;
`;
function App() {
return
<Wrapper>
<Box>
<span>😎</span>
</Box>
</Wrapper>
}
<Box> 컴포넌트안에span 태그를 사용하여 이모지 추가display: flex; 설정추가span 태그를 스타일 컴포넌트로 만들 필요 없이,const Box = styled.div`
height: 200px;
width: 200px;
background-color: tomato;
animation: ${rotationAnimation} 1s linear infinite;
display: flex;
justify-content: center;
align-items: center;
span { <<<----
font-size: 36px
}
`;
&:hover 도 가능하다!const Box = styled.div`
height: 200px;
width: 200px;
background-color: tomato;
animation: ${rotationAnimation} 1s linear infinite;
display: flex;
justify-content: center;
align-items: center;
span {
font-size: 36px;
&:hover { // === span:hover 와 같음! <<<----
font-size 80px;
}
}
`;
span 태그를 p태그로 바뀐다면?span 태그가 아니게 되었으니!const Emoji = styled.span`
font-size: 36px;
`;
const Box = styled.div`
height: 200px;
width: 200px;
background-color: tomato;
animation: ${rotationAnimation} 1s linear infinite;
display: flex;
justify-content: center;
align-items: center;
${Emoji} { <<<----
&:hover {
font-size 98px;
}
}
`;
function App() {
return
<Wrapper>
<Box>
<Emoji as="p">😎</Emoji>
</Box>
</Wrapper>
}
Emoji 가 span 태그이든, p 태그이든 상관없이 적용된다!!${Emoji}:hover {
font-size 98px;
}
import { ThemeProvider } from "styled-components";
<ThemeProvider theme={theme}>
</ThemeProvider>
// -index.js
const root = ReactDOM.createRoot(
document.getElementById("root") as HTMLElement
);
root.render(
<React.StrictMode>
<ThemeProvider>
<App />
</ThemeProvider>
</React.StrictMode>
);
ThemeProvider 를 import 하고,<App /> 컴포넌트를 <ThemeProvider> 로 감쌌다.ThemeProvider 는 1개의 prop 을 가진다. theme!// -index.js
const darkTheme = { <<<----
textColor:"whitesmoke",
backgroundColor:"#111",
}
const lightTheme = { <<<----
textColor:"#111",
backgroundColor:"whitesmoke",
}
const root = ReactDOM.createRoot(
document.getElementById("root") as HTMLElement
);
root.render(
<React.StrictMode> <<<----
<ThemeProvider theme={lightTheme}>
<App />
</ThemeProvider>
</React.StrictMode>
);
ThemeProvider 에 theme={darkTheme} prop 으로 주면서, 다크모드로 설정<App /> 이 ThemeProvider 안에 있기 때문에<App /> 안에 있는 컴포넌트들이 색상(textColor, backgroundColor)에 접근할 수 있다!// -App.js
const Title = styled.h1`
color: ${(props) => props.theme.textColor}
`;
ThemeProvider 안에 둘러싸인 모든 컴포넌트는 ThemeProvider 에 접근하여 값을 사용할 수 있다.

이제 라이트 모드를 설정해보자
// -index.js
const darkTheme = {
textColor:"whitesmoke",
backgroundColor:"#111",
}
const lightTheme = {
textColor:"#111",
backgroundColor:"whitesmoke",
}
const root = ReactDOM.createRoot(
document.getElementById("root") as HTMLElement
);
root.render(
<React.StrictMode>
<ThemeProvider theme={lightTheme}> <<<----
<App />
</ThemeProvider>
</React.StrictMode>
);
ThemeProvider 의 prop 에 theme={lightTheme} 라이트모드로 바꿔주기만 하면된다.