npm install --save styled-components
import styled from 'styled-components'
// 렌더링 될 요소의 바깥에서 적어야하며, 아닐시 렌더링시마다 스타일링이 다시 진행됨
const Title = styled.h1`
font-size: 1.5em;
text-align: center;
color: palevioletred;
`;
const Wrapper = styled.section`
padding: 4em;
background: papayawhip;
`;
return(
<Wrapper>
<Title>
Hello World!
</Title>
</Wrapper>
);
결과:
const Button = styled.button`
background: ${props => props.primary ? "palevioletred" : "white"};
color: ${props => props.primary ? "white" : "palevioletred"};
font-size: 1em;
margin: 1em;
padding: 0.25em 1em;
border: 2px solid palevioletred;
border-radius: 3px;
`;
return(
<div>
<Button>Normal</Button>
<Button primary>Primary</Button>
</div>
);
결과:
const Input = styled.input`
padding: 0.5em;
margin: 0.5em;
color: ${props => props.inputColor || "palevioletred"};
background: papayawhip;
border: none;
border-radius: 3px;
`;
return(
<div>
<Input defaultValue="@probablyup" type="text" />
<Input defaultValue="@geelen" type="text" inputColor="rebeccapurple" />
</div>
);
결과:
const Link = ({ className, children }) => (
<a className={className}>
{children}
</a>
);
const StyledLink = styled(Link)`
color: palevioletred;
font-weight: bold;
`;
return(
<div>
<Link>Unstyled, boring Link</Link>
<br />
<StyledLink>Styled, exciting Link</StyledLink>
</div>
);
결과:
const Button = styled.button`
color: palevioletred;
font-size: 1em;
margin: 1em;
padding: 0.25em 1em;
border: 2px solid palevioletred;
border-radius: 3px;
`;
const TomatoButton = styled(Button)`
color: tomato;
border-color: tomato;
`;
return(
<div>
<Button>Normal Button</Button>
<TomatoButton>Tomato Button</TomatoButton>
</div>
);
결과:
// Button, TomatoButton은 바로 위의 소스코드와 동일
return(
<div>
<Button>Normal Button</Button>
<Button as="a" href="#">Link with Button styles</Button>
<TomatoButton as="a" href="#">Link with Tomato Button styles</TomatoButton>
</div>
);
결과:
// attrs -> 태그, 컴포넌트에 특정한 속성을 부여해줌
const Thing = styled.div.attrs((/* props */) => ({ tabIndex: 0 }))`
color: blue;
&:hover {
color: red; //마우스를 갖다댈 시 글자가 빨개짐
}
& ~ & {
background: tomato; // 주변에 &(Thing)이 없는 요소에 배경색 설정
}
& + & {
background: lime; // 인접한 위치에 &가 있는 요소의 배경색 설정
}
&.something {
background: orange; // & 중에서 something 클래스를 가지는 요소들의 배경색 변경
}
.something-else & {
border: 1px solid; // something-else 클래스 를 가진 요소의 자식으로 &가 존재하는 요소의 boarder 설정
} //&을 빼먹을 경우 Thing 내부에 something-else 클래스를 가진 요소를 선택한느 의미로 바뀜
`
return(
<React.Fragment>
<Thing>Hello world!</Thing>
<Thing>How ya doing?</Thing>
<Thing className="something">The sun is shining...</Thing>
<div>Pretty nice day today.</div>
<Thing>Don't you think?</Thing>
<div className="something-else">
<Thing>Splendid.</Thing>
</div>
</React.Fragment>
)
결과:
const Input = styled.input.attrs({ type: "checkbox" })``;
const Label = styled.label`
align-items: center;
display: flex;
gap: 8px;
margin-bottom: 8px;
`
const LabelText = styled.span`
${(props) => {
switch (props.$mode) {
case "dark":
return css`
background-color: black;
color: white;
${Input}:checked + && {
color: blue; //${} 표기를 통해 내부에 다른 컴포넌트를 불러올 수 있음
}
`;
default:
return css`
background-color: white;
color: black;
${Input}:checked + && {
color: red;
}
`;
}
}}
`;
return(
<>
<Label>
<Input defaultChecked />
<LabelText>Foo</LabelText>
</Label>
<Label>
<Input />
<LabelText $mode="dark">Foo</LabelText>
</Label>
<Label>
<Input defaultChecked />
<LabelText>Foo</LabelText>
</Label>
<Label>
<Input defaultChecked />
<LabelText $mode="dark">Foo</LabelText>
</Label>
<>
)
결과:
const Thing = styled.div`
&& {
color: blue;
}
`
const GlobalStyle = createGlobalStyle`
div${Thing} {
color: red;
}
`
return(
<>
<GlobalStyle />
<Thing>
I'm blue, da ba dee da ba daa
</Thing>
</>
)
결과:
const Input = styled.input.attrs(props => ({
type: "text",
size: props.size || "1em",
}))`
color: palevioletred;
font-size: 1em;
border: 2px solid palevioletred;
border-radius: 3px;
margin: ${props => props.size};
padding: ${props => props.size};
`;
return(
<div>
<Input placeholder="A small text input" />
<br />
<Input placeholder="A bigger text input" size="2em" />
</div>
);
결과:
const Input = styled.input.attrs(props => ({
type: "text",
size: props.size || "1em",
}))`
border: 2px solid palevioletred;
margin: ${props => props.size};
padding: ${props => props.size};
`;
const PasswordInput = styled(Input).attrs({
type: "password",
})`
// similarly, border will override Input's border
border: 2px solid aqua;
`;
return(
<div>
<Input placeholder="A bigger text input" size="2em" />
<br />
{/* size 속성은 여전히 사용가능함 */}
<PasswordInput placeholder="A bigger password input" size="2em" />
</div>
);
const rotate = keyframes`
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
`;
const Rotate = styled.div`
display: inline-block;
animation: ${rotate} 2s linear infinite;
padding: 2rem 1rem;
font-size: 1.2rem;
`;
return(
<Rotate>< 💅🏾 ></Rotate>
출처:
https://styled-components.com/docs/basics#getting-started