npm i styled-components
cssimport styled from "styled-components";
const Wrapper = styled.div`
width:100vw;
height:100vh;
background-color:black;
color:white;
`
function App() {
return <Wrapper></Wrapper>;
}
export default App;
import styled from "styled-components";
const Box = styled.div`
width: 200px;
height: 200px;
background-color: ${(props) => props.bgColor};
`;
function App() {
return (
<div>
<Box bgColor="teal" />
<Box bgColor="tomato" />
</div>
);
}
export default App;
< 추가할 css >import styled from "styled-components";
const Box = styled.div`
width: 200px;
height: 200px;
background-color: ${(props) => props.bgColor};
`;
// Box styled component의 속성들이 Circle에 복사된다.
const Circle = styled(Box)`
border-radius: 50%;
`;
function App() {
return (
<div className="App">
<Box bgColor="teal" />
<Circle bgColor="tomato" />
</div>
);
}
export default App;
as
- as 키워드를 사용하면 컴포넌트의 스타일을 유지한채 원하는 태그로 바꿔줄 수 있다.
const Box = styled.div`
display: block;
width: 200px;
height: 200px;
background-color: ${(props) => props.bgColor};
`;
...
<Box as="a" href="/" bgColor="teal" /> // a 태그로 변한다.
...
attrs
- attrs prop을 사용하면 원하는 옵션 값을 넣어 컴포넌트를 만들 수 있다.
const Input = styled.input.attrs({ required: true })`
width: 400px;
height: 60px;
background-color: orange;
`;
...
<Input /> // required 옵션을 가진 input 태그가 되었다.
...
import styled, { keyframes } from "styled-components";
const rotateAnimation = keyframes`
from{
transform: rotate(0deg);
}to{
transform: rotate(360deg);
}
`;
const Box = styled.div`
display: block;
width: 200px;
height: 200px;
background-color: ${(props) => props.bgColor};
animation: ${rotateAnimation} 1s linear infinite;
`;
const Box = styled.div`
display: block;
width: 200px;
height: 200px;
background-color: ${(props) => props.bgColor};
animation: ${rotateAnimation} 1s linear infinite;
span{
font-size:25px;
&:hover{
font-size:40px;
}
}
`;
const Imoji = styled.span`
font-size: 25px;
`;
const Box = styled.div`
display: block;
width: 200px;
height: 200px;
background-color: ${(props) => props.bgColor};
display: flex;
justify-content: center;
align-items: center;
animation: ${rotateAnimation} 1s linear infinite;
${Imoji} {
&:hover {
font-size: 80px;
}
}
`;
import React from "react";
import ReactDOM from "react-dom/client";
**import { ThemeProvider } from "styled-components";**
import App from "./App";
import reportWebVitals from "./reportWebVitals";
**const darkTheme = {
textColor: "whitesmoke",
bgColor: "#111",
};
const lightTheme = {
textColor: "#111",
bgColor: "whitesmoke",
};**
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
**<ThemeProvider theme={darkTheme}> // ThemeProvider로 app을 감싸준다.**
<App />
**</ThemeProvider>**
</React.StrictMode>
);
// styled.d.ts
import "styled-components";
declare module "styled-components" {
export interface DefaultTheme {
textColor: string;
bgColor: string;
}
}
// theme.ts
import { DefaultTheme } from "styled-components";
export const lightTheme: DefaultTheme = {
bgColor: "white",
textColor: "tomato",
};
export const darkTheme: DefaultTheme = {
bgColor: "black",
textColor: "white",
};
// index.tsx
root.render(
<React.StrictMode>
<ThemeProvider theme={lightTheme}>
<App />
</ThemeProvider>
</React.StrictMode>
);
const Wrapper = styled.div`
width: 100vw;
height: 100vh;
background-color: ${(props) => props.theme.bgColor || "#1f2937"};
color: ${(props) => props.theme.textColor || "#1f2937"};
`;
npm i styled-reset
// App.tsx
import {Reset} from 'styled-reset'
const App = () =>{
...
<Reset/>
...
}