25
loading...
This website collects cookies to deliver better user experience
styled-components
is a CSS-in-JS library that lets you write css in javascript that is scoped to one component. It's sort of meant to be a successor of css modules. Let's look at an example of how to use styled-components.import styled from 'styled-components'
const Button = styled.button`
background: transparent;
border-radius: 3px;
border: 2px solid palevioletred;
color: palevioletred;
margin: 0 1em;
padding: 0.25em 1em;
`
import styled, { css } from 'styled-components'
const Button = styled.button`
background: transparent;
border-radius: 3px;
border: 2px solid palevioletred;
color: palevioletred;
margin: 0 1em;
padding: 0.25em 1em;
${props => props.fontSize ? css`
font-size: props.fontSize;
`: ''}
`
<Button fontSize='2rem'>My button</Button>
import styled from 'styled-components'
import { color } from 'styled-system'
const Box = styled.div`
${color}
`
color
to set foreground color, and bg
to set background color. (You can also use backgroundColor
).<Box color="#eee" bg="orange">
Orange
</Box>
<Button/>
component.import styled from 'styled-components'
import { color } from 'styled-system'
const Button = styled.button`
border: 0;
outline: 0;
${color}
`
<Button color="white" backgroundColor="tomato">
Hello, world!
</Button>
import styled from 'styled-components'
import { color, space, fontSize } from 'styled-system'
const Button = styled.button`
border: 0;
outline: 0;
${color}
${space}
${fontSize}
`
<Button color="white" backgroundColor="tomato" px='2rem' mr='1rem' fontSize='2rem'>
Hello, world!
</Button>
import styled, { ThemeProvider } from 'styled-components'
import { color, space, fontSize } from 'styled-system'
const theme = {
colors: {
custom: '#444',
yellow: 'yellow'
}
}
const Button = styled.button`
border: 0;
outline: 0;
${color}
${space}
${fontSize}
`
Button.defaultProps = {
backgroundColor: 'blue'
}
const App = () => {
return (
<ThemeProvider theme={theme}>
<Button color='custom'>Styled Button</Button>
</ThemeProvider>
)
}
import styled, { ThemeProvider } from 'styled-components'
import { color, space, fontSize, buttonStyle } from 'styled-system'
const theme = {
colors: {
custom: '#444',
yellow: 'yellow'
},
buttons: {
primary: {
color: 'white',
backgroundColor: 'blue'
},
secondary: {
color: 'white',
backgroundColor: 'green'
}
}
}
const Button = styled.button`
border: 0;
outline: 0;
${color}
${space}
${fontSize}
${buttonStyle}
`
Button.defaultProps = {
variant: 'primary',
backgroundColor: 'blue'
}
const App = () => {
return (
<ThemeProvider theme={theme}>
<Button color='custom' variant='secondary'>Styled Button</Button>
</ThemeProvider>
)
}
import styled, { ThemeProvider } from 'styled-components'
import { color, space, fontSize, buttonStyle, variant } from 'styled-system'
const buttonSize = variant({
prop: 'size',
key: 'buttonSizes'
})
const theme = {
colors: {
custom: '#444',
yellow: 'yellow'
},
buttons: {
primary: {
color: 'white',
backgroundColor: 'blue'
},
secondary: {
color: 'white',
backgroundColor: 'green'
}
},
buttonSizes: {
small: {
fontSize: '15px',
padding: `7px 15px`
},
medium: {
fontSize: '18px',
padding: `9px 20px`
},
large: {
fontSize: '22px',
padding: `15px 30px`
}
}
}
const Button = styled.button`
border: 0;
outline: 0;
${color}
${space}
${fontSize}
${buttonStyle}
`
Button.defaultProps = {
variant: 'primary',
backgroundColor: 'blue',
size: 'medium'
}
const App = () => {
return (
<ThemeProvider theme={theme}>
<Button color='custom' variant='secondary' size='large'>Styled Button</Button>
</ThemeProvider>
)
}