노마드코더의 무료강의 리액트 스타일? Styled Components! 강의노트 ✏️
import React, { Component, Fragment } from "react";
import styled, { createGlobalStyle } from "styled-components";
const GlobalStyle = createGlobalStyle`
body {
padding: 0;
margin: 0;
}
`;
class App extends Component {
render() {
return (
<Fragment>
<GlobalStyle />
<Button>Hello</Button>
<Button danger>Hello</Button>
</Fragment>
);
}
}
const Button = styled.button`
~
background-color: ${(props) => (props.danger ? "#e74c3c" : "#2ecc71")};
`;
const Anchor = styled(Button.withComponent("a"))`
text-decoration: none;
font-size: 12px;
`;
import styled, { keyframes, css } from "styled-components";
const Button = styled.button`
~
${(props) => {
if (props.danger) {
return css`
animation: ${rotation} 2s linear infinite;
`;
}
}}
`;
const rotation = keyframes`
from{
transform: rotate(0deg);
} to{
transform : rotate(360deg);
}
`;
const Input = styled.input.attrs({
required: true,
})`
border-radius: 5px;
`;
class App extends Component {
render() {
return (
<Container>
<Input placeholder="hello" />
</Container>
);
}
}
import styled, { css } from "styled-components";
const awesomeCard = css`
box-shadow: 0 4px 6px rgba(50, 50, 93, 0.11), 0 1px 3px rgba(0, 0, 0, 0.08);
background-color: white;
border-radius: 10px;
padding: 20px;
`;
const Input = styled.input`
${awesomeCard}
`;
const theme = {
mainColor: "#3498db",
dangerColor: "#e74c3c",
successColor: "#2ecc71",
};
export default theme;
import styled, { ThemeProvider } from "styled-components";
import theme from "./theme";
const Card = styled.div`
background-color: pink;
`;
const Button = styled.button`
border-radius: 30px;
padding: 25px 15px;
// theme.js에서 원하는 색 불러오는 방법❗
background-color: ${(props) => props.theme.dangerColor};
`;
const Form = () => (
<Card>
<Button>Hello</Button>
</Card>
);
class App extends Component {
render() {
return (
<ThemeProvider theme={theme}>
<Form />
</ThemeProvider>
);
}
}
const Card = styled.div`
background-color: tomato;
`;
const Container = styled.div`
height: 100vh;
width: 100%;
background-color: pink;
${Card} {
background-color: blue;
}
`;
styled-component를 정리가 되지 않았는데, 이렇게 정리를 잘해 주셔서 이해하기 쉬웠습니다. 감사합니다. :)