What is @styled-system/css?
@styled-system/css is a utility function for creating style objects based on a theme. It allows you to use a JavaScript object to define styles, which can then be applied to components. This package is part of the Styled System family and is designed to work seamlessly with other Styled System utilities.
What are @styled-system/css's main functionalities?
Theming
Theming allows you to define a set of design tokens (e.g., colors, spacing, typography) that can be referenced throughout your application. This ensures consistency and makes it easy to update the design system.
{
"theme": {
"colors": {
"primary": "#07c",
"secondary": "#05a"
}
},
"styles": {
"button": {
"backgroundColor": "primary",
"color": "white",
"padding": "8px 16px",
"borderRadius": "4px"
}
}
}
Responsive Styles
Responsive styles allow you to define different styles for different screen sizes using an array syntax. This makes it easy to create responsive designs without writing media queries.
{
"styles": {
"button": {
"padding": ["8px 16px", "12px 24px", "16px 32px"]
}
}
}
Variant Styles
Variant styles allow you to define reusable style objects that can be applied to components. This is useful for creating consistent styles for different variants of a component (e.g., primary and secondary buttons).
{
"theme": {
"buttons": {
"primary": {
"backgroundColor": "primary",
"color": "white"
},
"secondary": {
"backgroundColor": "secondary",
"color": "white"
}
}
},
"styles": {
"button": {
"variant": "buttons.primary"
}
}
}
Other packages similar to @styled-system/css
styled-components
styled-components is a library for styling React components using tagged template literals. It allows you to write actual CSS code to style your components. Compared to @styled-system/css, styled-components offers a more traditional approach to styling with CSS, but it lacks the built-in theming and responsive style utilities.
emotion
Emotion is a library designed for writing CSS styles with JavaScript. It provides both a CSS-in-JS solution and a styled-components-like API. Emotion offers similar theming and responsive style capabilities as @styled-system/css, but it also includes additional features like server-side rendering and source maps.
tailwindcss
Tailwind CSS is a utility-first CSS framework that provides low-level utility classes to build custom designs. Unlike @styled-system/css, which uses JavaScript objects for styling, Tailwind CSS uses predefined utility classes in your HTML. Tailwind CSS is highly customizable and can be configured to work with a design system, but it requires a different approach to styling compared to CSS-in-JS solutions.
@styled-system/css
Styled System for your css
prop
npm i @styled-system/css
Styled System CSS lets you write style objects with responsive, theme-aware Styled System shortcuts.
import React from 'react'
import css from '@styled-system/css'
const Beep = props =>
<div
{...props}
css={css({
fontSize: [4, 5, 6],
color: 'primary',
})}
/>
import styled from '@emotion/styled'
import css from '@styled-system/css'
const Boop = styled(
css({
fontSize: [ 4, 5, 6 ],
color: 'primary',
bg: 'gray',
'&:hover': {
color: 'secondary',
},
})
)
Keys
The following keys in your style object will work the same as Styled System props, pulling values from your theme
object and converting arrays to responsive styles.
fontFamily
fontSize
fontWeight
lineHeight
color
bg
, backgroundColor
m
, margin
mt
, marginTop
mr
, marginRight
mb
, marginBottom
ml
, marginLeft
mx
my
p
, padding
pt
, paddingTop`pr
, paddingRight`pb
, paddingBottom`pl
, paddingLeft`px
py
In this example, fontSize
accepts an array, and borderBottom
is passed through as plain CSS.
<div
css={css({
fontSize: [ 3, 4, 5 ],
borderBottom: '2px solid tomato',
})}
/>
Props
When using @styled-system/css
with styled components,
Styled System props can be used on the resulting component.
<Beep
color='primary'
bg='black'
/>
MIT License