styled-system
Design system utilities for styled-components and other css-in-js libraries
npm i styled-system
Features
- Add style props that hook into your own theme
- Responsive prop values for quickly setting responsive font-size, margin, padding, width, and more
- Influenced by constraint-based design system principles
- Typographic scale
- Spacing scale for margin and padding
- Default 8px grid
- Works with any color palette
- Works with most css-in-js libraries, including styled-components, glamorous, emotion, fela, and cxs
- Used in Rebass, Grid Styled, and the Priceline Design System
"The future of css-in-js is going to look something like styled-system with its responsive values."
– Kye Hohenberger
"Fantastic set of tools that offer the ease and API of tachyons/functional CSS but, are way more customisable."
– Varun Vachhar
"Coming from @tachyons_css, the styled-system utilities from @jxnblk is the missing link I’ve been looking for."
– Nathan Young
Table of Contents
Usage
import styled from 'styled-components'
import { space, width, fontSize, color } from 'styled-system'
const Box = styled.div`
${space}
${width}
${fontSize}
${color}
`
Each style function exposes its own set of props that style
elements based on values defined in a theme.
<Box width={1/2} />
<Box fontSize={4} />
<Box m={2} />
<Box p={3} />
<Box color='tomato' />
<Box color='grays.0' />
<Box bg='tomato' />
Responsive Style Props
Set responsive width, margin, padding, font-size, and other properties with a shorthand array syntax.
Read more
<Box width={[ 1, 1/2, 1/4 ]} />
<Box fontSize={[ 2, 3, 4 ]} />
<Box m={[ 1, 2, 3 ]} />
<Box p={[ 1, 2, 3 ]} />
Getting Started
Although it's not required, styled-system works best with a theme that's tailored to your own custom styles.
Create a theme.js
file that exports an object and add a ThemeProvider
to the root of your application.
const theme = {}
export default theme
import React from 'react'
import { ThemeProvider } from 'styled-components'
import theme from './theme'
const App = props => (
<ThemeProvider theme={theme}>
{/* ... */}
</ThemeProvider>
)
Most utility functions in styled-system will attempt to find a value from your theme first,
then fallback to a hard-coded value if it hasn't been defined in your theme.
For example, defining a colors
object can make using a common color palette across your app simpler.
const colors = {
text: '#024',
blue: '#07c'
}
const theme = {
colors
}
export default theme
With a component that uses the color
function from styled-system, the name of the color defined in your theme can be used as a prop.
<Box color='blue' />
When a value is passed that's not in the theme, it will be passed as a raw value.
<Box color='tomato' />
It's recommended to add objects and array scales to your theme to ensure consistent, constraint-based values are used throughout your app.
All theme values are optional, so use your own discretion when creating a theme.
See the Default Theme section for a reference to the default fallback values.
const breakpoints = [
'40em', '52em', '64em'
]
const colors = {
text: '#024',
blue: '#07c',
dark: {
blue: '#058'
},
gray: [
'#333',
'#666',
'#999',
'#ccc',
'#eee',
'#f6f6f6',
]
}
const space = [
0, 4, 8, 16, 32, 64, 128, 256, 512
]
const fontSizes = [
12, 14, 16, 20, 24, 32, 48, 64, 96, 128
]
const lineHeights = [
1, 1.125, 1.25, 1.5
]
const fontWeights = {
normal: 500,
bold: 700
}
const letterSpacings = {
normal: 'normal',
caps: '0.25em'
}
const radii = [
0, 2, 4, 8
]
const borderWidths = [
0, 1, 2
]
const shadows = [
`0 1px 2px 0 ${colors.text}`,
`0 1px 4px 0 ${colors.text}`
]
const theme = {
breakpoints,
colors,
space,
fontSizes,
lineHeights,
fontWeights,
letterSpacings,
radii,
borderWidths,
shadows,
}
export default theme
Next, create a set of UI components that provide convenient style props to the values defined in the theme.
It's recommended to keep components simple and focused on doing one thing well.
For UI components, it's common to separate them according to different concerns, such as layout, typography, and other styles.
However, there may be some general purpose style props that you'd like to apply consistently across your entire component set, such as margin, padding, and color.
import styled from 'styled-components'
import {
space,
color,
width,
fontSize,
fontWeight,
textAlign,
lineHeight
} from 'styled-system'
export const Box = styled.div`
${space}
${color}
${width}
`
export const Text = styled.div`
${space}
${color}
${fontSize}
${fontWeight}
${textAlign}
${lineHeight}
`
How it Works
Most CSS-in-JS libraries accept functions as arguments to create dynamic styles based on props.
For example, the following sets color dynamically in styled-components based on the color
prop:
import styled from 'styled-components'
const Box = styled.div`
color: ${props => props.color};
`
Beyond just passing a dynamic value, an entire style declaration can be returned in functions like this.
import styled from 'styled-components'
const getColor = props => `color: ${props.color};`
const Box = styled.div`
${getColor}
`
Style object can also be returned, which is a much simpler way to handle dynamic values in JavaScript.
import styled from 'styled-components'
const getColor = props => ({
color: props.color
})
const Box = styled.div`
${getColor}
`
By using style objects instead of embedded CSS strings, styled-system is compatible with other libraries,
such as glamorous and emotion.
The core utilities in styled-system are built on this pattern and consist of functions that take props
as an argument
and return style objects,
while making it simpler to use values from a theme and apply styles responsively across breakpoints.
These style functions can be written on a one-off basis, but styled-system is meant to help reduce boilerplate, ensure a consistent styling API, and speed the development of React-based design systems.
Responsive Styles
Often when working on responsive layouts, it's useful to adjust styles across a singular dimension –
such as font-size, margin, padding, and width.
Instead of manually managing media queries and adding nested style objects throughout a code base,
styled-system offers a convenient shorthand syntax for adding responsive styles with a mobile-first approach.
While this syntax can seem odd at first, it can become a powerful way to manage responsive typography and layouts.
All core props accept arrays as values for mobile-first responsive styles.
<Box
width={[
1,
1/2,
1/4
]}
/>
<Box fontSize={[ 1, 2, 3, 4 ]} />
<Box m={[ 1, 2, 3, 4 ]} />
<Box p={[ 1, 2, 3, 4 ]} />
API
Core
space (responsive)
import { space } from 'styled-system'
The space utility converts shorthand margin and padding props to margin and padding CSS declarations.
- Numbers from 0-4 (or the length of
theme.space
) are converted to values on the spacing scale. - Negative values can be used for negative margins.
- Numbers greater than the length of the
theme.space
array are converted to raw pixel values. - String values are passed as raw CSS values.
- And array values are converted into responsive values.
Margin and padding props follow a shorthand syntax for specifying direction.
m
: marginmt
: margin-topmr
: margin-rightmb
: margin-bottomml
: margin-leftmx
: margin-left and margin-rightmy
: margin-top and margin-bottomp
: paddingpt
: padding-toppr
: padding-rightpb
: padding-bottompl
: padding-leftpx
: padding-left and padding-rightpy
: padding-top and padding-bottom
<Box m={2} />
<Box m={-2} />
<Box m={16} />
<Box m='auto' />
<Box m={[ 1, 2 ]} />
width (responsive)
import { width } from 'styled-system'
The width utility parses a component's width
prop and converts it into a CSS width declaration.
- Numbers from 0-1 are converted to percentage widths.
- Numbers greater than 1 are converted to pixel values.
- String values are passed as raw CSS values.
- And arrays are converted to responsive width styles.
<Box width={1/2} />
<Box width={256} />
<Box width='2em' />
<Box width={[ 1, 1/2 ]} />
fontSize (responsive)
import { fontSize } from 'styled-system'
The fontSize utility parses a component's fontSize
prop and converts it into a CSS font-size declaration.
- Numbers from 0-8 (or
theme.fontSizes.length
) are converted to values on the font size scale. - Numbers greater than
theme.fontSizes.length
are converted to raw pixel values. - String values are passed as raw CSS values.
- And array values are converted into responsive values.
<Text fontSize={3} />
<Text fontSize={32} />
<Text fontSize='2em' />
<Text fontSize={[ 10, 12 ]} />
color (responsive)
import { color } from 'styled-system'
The color utility parses a component's color
and bg
props and converts them into CSS declarations.
By default the raw value of the prop is returned.
Color palettes can be configured with the ThemeProvider to use keys as prop values, with support for dot notation.
Array values are converted into responsive values.
<Box color='blue' />
<Box color='gray.0' />
<Box color='#f00' />
These functions are for adding other theme-based style props to a component.
For practical reasons, some props do not accept arrays for responsive styles.
textAlign (responsive)
import { textAlign } from 'styled-system'
<Text align='center' />
lineHeight
import { lineHeight } from 'styled-system'
<Text lineHeight={1} />
fontWeight
import { fontWeight } from 'styled-system'
<Text fontWeight='bold' />
letterSpacing
import { letterSpacing } from 'styled-system'
<Text letterSpacing={1} />
alignItems (responsive)
import { alignItems } from 'styled-system'
<Flex align='center' />
justifyContent (responsive)
import { justifyContent } from 'styled-system'
<Flex justify='center' />
flexWrap (responsive)
import { flexWrap } from 'styled-system'
<Flex wrap />
flexDirection (responsive)
import { flexDirection } from 'styled-system'
<Flex flexDirection='column' />
flex (responsive)
import { flex } from 'styled-system'
<Box flex='none' />
alignSelf (responsive)
import { alignSelf } from 'styled-system'
<Box alignSelf='baseline' />
borderRadius
import { borderRadius } from 'styled-system'
<Box borderRadius={1} />
borderColor
import { borderColor } from 'styled-system'
<Box borderColor='blue' />
borderWidth
import { borderWidth } from 'styled-system'
<Box borderWidth={1} />
<Box borderWidth={1} borderBottom />
<Box borderWidth={1} borderTop borderBottom />
boxShadow
import { boxShadow } from 'styled-system'
<Box boxShadow={1} />
<Box boxShadow='large' />
<Box boxShadow='1px 1px 0 black' />
Pseudo-classes
Pseudo-class utility props accept style objects that can pick up certain values, such as color, from a theme.
hover
import { hover } from 'styled-system'
<Box
hover={{
textDecoration: 'underline',
color: 'blue'
}}
/>
focus
import { focus } from 'styled-system'
<Box focus={{ color: 'blue' }} />
active
import { active } from 'styled-system'
<Box active={{ color: 'navy' }} />
disabled
import { disabled } from 'styled-system'
<Box disabledStyle={{ color: 'gray' }} />
Table of Style Props
Function Name | Prop | CSS Property | Theme Field | Responsive |
---|
space | m | margin | space | yes |
space | mt | margin-top | space | yes |
space | mr | margin-right | space | yes |
space | mb | margin-bottom | space | yes |
space | ml | margin-left | space | yes |
space | p | padding | space | yes |
space | pt | padding-top | space | yes |
space | pr | padding-right | space | yes |
space | pb | padding-bottom | space | yes |
space | pl | padding-left | space | yes |
width | width w | width | none | yes |
fontSize | fontSize f | font-size | fontSizes | yes |
color | color | color | colors | yes |
color | bg | background-color | colors | yes |
textAlign | align | text-align | none | yes |
lineHeight | lineHeight | line-height | lineHeights | no |
fontWeight | fontWeight | font-weight | fontWeights | no |
letterSpacing | letterSpacing | letter-spacing | letterSpacings | no |
alignItems | align | align-items | none | yes |
justifyContent | justify | justify-content | none | yes |
flexWrap | wrap (boolean) | flex-wrap | none | yes |
flexDirection | flexDirection | flex-direction | none | yes |
flex | flex | flex (shorthand) | none | yes |
alignSelf | alignSelf | align-self | none | yes |
borderRadius | borderRadius | border-radius | radii | no |
borderColor | borderColor | border-color | colors | no |
borderWidth | borderWidth | border-width | borderWidths | no |
boxShadow | boxShadow | box-shadow | shadows | no |
Utilities
theme
The theme function is an existential getter function
that can be used in any style declaration to get a value
from your theme, with support for fallback values.
This helps prevent errors from throwing when a theme value is missing,
which can be helpful when unit testing styled-components.
theme(objectPath, fallbackValue)
import styled from 'styled-components'
import { theme } from 'styled-system'
const Box = styled.div`
border-radius: ${theme('radii.small', '4px')};
`
propTypes
Prop type definitions are available for each style function to add to your component's propTypes object.
Each value in propTypes
is an object which should be assigned (or spread) to the component's propTypes
.
import styled from 'styled-components'
import { width, propTypes } from 'styled-system'
const Box = styled.div`
${width}
`
Box.propTypes = {
...propTypes.width
}
cleanElement
Styled-components and other libraries attempt to remove invalid HTML attributes from props using a whitelist,
but do not remove width
, fontSize
, color
, or other valid HTML attributes when used as props.
To ensure that style props are not passed on to the underlying DOM element,
even in cases where a prop is a valid HTML attribute, like width
or align
, use the cleanElement
higher order component to create a base component
that remove props defined in propTypes
.
import styled from 'styled-components'
import { textAlign, propTypes, cleanElement } from 'styled-system'
const CleanDiv = cleanElement('div')
CleanDiv.propTypes = {
...propTypes.textAlign
}
const Box = styled(CleanDiv)`
${textAlign}
`
Manually omitting props
As an alternative to using the cleanElement
function, removing style props from styled-components can be done manually, with a more React-like approach.
import React from 'react'
import styled from 'styled-components'
import { width, color } from' styled-system'
const Box = styled(({
width,
color,
bg,
...props
}) => <div {...props} />)`
${width}
${color}
`
See this discussion for more information:
https://github.com/styled-components/styled-components/issues/439
Low-level Style Functions
To create custom utilities for other CSS properties,
use the following low-level utility functions.
style
Create a non-responsive style utility.
import styled from 'styled-components'
import { style } from 'styled-system'
const textShadow = style({
prop: 'shadow',
cssProperty: 'textShadow',
key: 'shadows'
numberToPx: false
})
const ShadowText = styled(Text)`
${textShadow}
`
const App = props => (
<ShadowText shadow={0}>
Shady
</ShadowText>
)
responsiveStyle
Create a responsive style utility that accepts array-based responsive prop values.
import styled from 'styled-components'
import { responsiveStyle } from 'styled-system'
const borderRadius = responsiveStyle({
prop: 'borderRadius',
cssProperty: 'borderRadius',
numberToPx: true,
key: 'radii'
})
const RoundedBox = styled.div`
${borderRadius}
`
const App = props => (
<RoundedBox borderRadius={[ 0, 2 ]} />
)
pseudoStyle
Create a pseudo-class style utility that accepts a style object prop value.
import styled from 'styled-components'
import { pseudoStyle } from 'styled-system'
const checkedStyle = pseudoStyle('checked', 'checkedStyle')({
color: 'colors',
backgroundColor: 'colors',
})
const FancyCheckbox = styled.input`
/* ...base styles */
${checkedStyle}
`
FancyCheckbox.defaultProps = {
type: 'checkbox'
}
system-components
For an even simpler authoring experience when using styled-system with styled-components, see system-components, which is a lightweight wrapper around the two libraries.
import system from 'system-components'
const Box = system({
p: 2,
bg: 'blue'
})
Default Theme
If no theme is provided, styled-system uses smart defaults for breakpoints, the typographic scale, and the spacing scale.
const breakpoints = [ '40em', '52em', '64em' ]
const fontSizes = [ 12, 14, 16, 20, 24, 32, 48, 64, 72 ]
const space = [ 0, 8, 16, 32, 64 ]
Troubleshooting
Unknown attribute warnings in React 16
See cleanElement
Issues with prop-types
If you encounter issues while using this library alongside the prop-types
npm package,
webpack's resolve.modules
option might be misconfigured using a relative (instead of absolute) path.
This changes the way CommonJS modules work in a way that will likely cause other issues,
and it's recommended that you only use absolute paths with the resolve.modules
option.
If you're using resolve.modules
to avoid import syntax with lots of directory changes,
you might want to consider using a flatter or better organized folder structure in your app.
See https://github.com/jxnblk/grid-styled/issues/51#issuecomment-336116426
Related
MIT License