July 19, 2021
import styled from 'styled-components/macro';
const Button = styled.button`
background: ${props => (props.primary ? 'palevioletred' : 'white')};
color: ${props => (props.primary ? 'white' : 'palevioletred')};
border: 1px solid ${props => props.borderColor || 'black'};
`
render(
<div>
<Button>Normal</Button>
<Button primary>Primary</Button>
<Button borderColor="red">Primary</Button>
</div>
)
공통클래스 적용할 때 편하다.
const Button = styled.button`
color: palevioletred;
font-size: 1em;
margin: 1em;
border: 2px solid palevioletred;
`
const TomatoButton = styled(Button)`
// 이 형태는 Button 의 스타일을 상속하면서 지정한 스타일을 override 한다.
color: tomato;
border-color: tomato;
`
render(
<div>
<Button>Normal Button</Button>
<TomatoButton>Tomato Button</TomatoButton>
</div>
)
const Link = ({ className, children }) => (
<a className={className}>{children}</a>
)
//Extend Link
const StyledLink = styled(Link)`
color: palevioletred;
font-weight: bold;
`
render(
<div>
<Link>Unstyled, boring Link</Link>
<StyledLink>Styled, exciting Link</StyledLink>
</div>
)
const Input = styled.input.attrs(props => ({
// static props
type: 'text',
// dynamic ones
size: props.size || '1em',
}))`
// 동적으로 계산된 props 사용
margin: ${props => props.size};
padding: ${props => props.size};
`
const PasswordInput = styled(Input).attrs({
type: 'password',
})`
border: 1px solid red;
`
render(
<div>
<Input placeholder="text input" size="2em" />
<PasswordInput placeholder="pw input" />
</div>
)
reset도 편하게 사용할 수 있다.
// GlobalStyles.js
import { createGlobalStyle } from 'styled-components'
import reset from 'styled-reset'
const GlobalStyles = createGlobalStyle`
${reset}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
li {
list-style: none;
}
`
export default GlobalStyles
적용하기
// index.js
import React from 'react'
import ReactDOM from 'react-dom'
import GlobalStyles from './styles/GlobalStyles'
import Routes from './Routes'
ReactDOM.render(
<>
<Routes />
<GlobalStyles /> // 이렇게 넣어주면 전체 적용된다.
</>,
document.getElementById('root')
)
테마 만들기
// theme.js
const theme = {
mainBlue: '#00c7f5',
backgroundGrey: '#f5f5f5',
}
export default theme
적용하기
import React from 'react'
import ReactDOM from 'react-dom'
import { ThemeProvider } from 'styled-components'
import theme from './theme'
import Routes from './Routes'
ReactDOM.render(
<>
<ThemeProvider theme={theme}>
<Routes />
</ThemeProvider>
</>,
document.getElementById('root')
)
사용하기
//사용할 아무파일.js
const ProfileName = styled.span`
color: ${({ theme }) => theme.fontMainBlack};
`
만들기
// variables.js
import { css } from 'styled-components'
export const flexSet = (justify = 'center', align = 'center') => css`
display: flex;
justify-content: ${justify};
align-items: ${align};
`
export const mediaQuery = {
BREAK_POINT_MOBILE: 760,
BREAK_POINT_TABLET: 1060,
}
export const mainPadding = (desktop = '60px', tablet = '40px') => css`
padding: 10px 60px;
@media only screen and (max-width: ${mediaQuery.BREAK_POINT_TABLET}px) {
padding: 10px 40px;
}
`
사용하기
//사용할.js
import { flexSet } from '../../styles/Variable'
import { mediaQuery } from '../../styles/Variable'
import { mainPadding } from '../../styles/Variable'
const BottomNavContainerInnerWrapper = styled.div`
${flexSet()}
max-width: 1256px;
height: 50px;
margin: 0 auto;
${mainPadding()}
`
컴포넌트와 스타일드컴포넌트가 동일하게 생겨서 헷갈린다.
https://styled-components.com/docs