Unistyle
Responsive CSS property mapping and design system utilities.
Unistyle is a data-driven CSS property mapping and responsive design system. It provides 250+ CSS property mappings with automatic unit conversion, responsive breakpoints, and semantic alignment utilities.
Overview
- 250+ CSS property mappings (257 descriptors) organized by descriptor type
- Responsive design engine with mobile-first media queries
- Automatic unit conversion — numbers to rem, px to rem
- Semantic alignment —
alignX,alignY,directionmapped to flex properties - Spacing shorthands — margin, padding, border with full/x/y/side syntax
- Theme Provider with pre-computed breakpoints and media queries
What's new in 2.9
Fixes to the responsive pipeline:
- Styles that contain function interpolations (
extendCsscallbacks, coolgridcolCss/rowCss, Elementcsscallbacks readingprops.theme) are now resolved with the real render-time props when breakpoints exist. Before, they could render the wrong value or drop the line. See Function interpolations. - A
cssresult (or any non-plain object) used as a theme value is no longer mistaken for a breakpoint map. - Behaviour change: holes in responsive arrays inherit the previous breakpoint.
[12, null, 15]now givessm: 12(was15), and[null, 4]leavesxsunset (was4). - The output cache is scoped per
stylesfunction andnormalizeflag, so components sharing a theme object no longer share output. createMediaQueriesdefaultsrootSizeto 16 (wasNaNem).- The breakpoint delta optimizer re-emits declarations reset by related properties (
border-top/border-color,grid-area/grid-row, logical / physical sides) and ignores/* … */comments. - Faster responsive cache misses.
Installation
npm install @vitus-labs/unistyleRequires @vitus-labs/core and a CSS engine connector.
Try It Live
Element's layout props go through Unistyle: direction, alignX and alignY are mapped to flex properties by alignContent, and a numeric gap is converted to rem (relative to rootSize, 16 by default). Change them and inspect the generated CSS:
Quick Example
import { makeItResponsive, styles } from '@vitus-labs/unistyle'
import { config } from '@vitus-labs/core'
const Box = config.styled('div')`
${makeItResponsive({
key: '$box',
css: config.css,
styles, // receives { theme, css, rootSize } per breakpoint
})}
`
// Responsive styling (needs the unistyle Provider with breakpoints)
<Box $box={{
display: 'flex',
fontSize: [12, 14, 16], // xs: 12, sm: 14, md+: 16
padding: { xs: 8, md: 16, lg: 24 }, // Breakpoint object
gap: 16, // Static value
alignItems: 'center',
}} />Exports
Responsive Utilities
| Export | Description |
|---|---|
makeItResponsive() | Core responsive engine — returns styled interpolation |
breakpoints | Default breakpoint config |
sortBreakpoints() | Sort breakpoint keys by pixel value |
createMediaQueries() | Build breakpoint → @media template functions |
between() | Closed-range @media (min-width) and (max-width) string between two breakpoints |
transformTheme() | Pivot theme from property-centric to breakpoint-centric |
normalizeTheme() | Expand arrays/objects to full breakpoint maps |
CSS Utilities
| Export | Description |
|---|---|
styles() | Process the 257 CSS property descriptors |
alignContent() | Convert semantic alignment to flex properties |
extendCss() | Evaluate custom CSS (callback or string) |
ALIGN_CONTENT_DIRECTION | Direction value mappings |
ALIGN_CONTENT_MAP_X | X-axis alignment mappings |
ALIGN_CONTENT_MAP_Y | Y-axis alignment mappings |
Unit Utilities
| Export | Description |
|---|---|
stripUnit() | Remove CSS unit suffix from a value |
value() | Convert number to CSS value with unit conversion |
values() | Pick first non-null value, convert, join arrays |
Context
| Export | Description |
|---|---|
Provider | Theme provider enriched with breakpoints and media queries |
context | React context from @vitus-labs/core |
Provider Setup
import { Provider } from '@vitus-labs/unistyle'
const theme = {
rootSize: 16,
breakpoints: {
xs: 0,
sm: 576,
md: 768,
lg: 992,
xl: 1200,
xxl: 1440,
},
// ... your theme values
}
<Provider theme={theme}>
<App />
</Provider>The Provider enriches the theme with pre-computed __VITUS_LABS__ data:
theme.__VITUS_LABS__ = {
sortedBreakpoints: ['xs', 'sm', 'md', 'lg', 'xl', 'xxl'],
media: {
xs: (css) => css`...`, // No @media wrapper (value=0)
sm: (css) => css`@media ...`, // min-width: 36em
md: (css) => css`@media ...`, // min-width: 48em
// ...
},
}How the Provider Works
- Reads
theme.breakpoints— object mapping names to pixel values - Sorts breakpoints by pixel value ascending via
sortBreakpoints() - Creates
mediaobject viacreateMediaQueries()— converts pixels toemfor accessibility (respects browser font-size settings), then creates tagged-template wrappers - Attaches both to
theme.__VITUS_LABS__(internal namespace) - Memoizes the enriched theme (recomputes only when the theme,
breakpointsorrootSizereference changes). On React Native, thecreateMediaQueriesfrom the platform connector (config.createMediaQueries) is used instead of the web one - Wraps children with the core Provider passing the enriched theme
The smallest breakpoint (typically xs: 0) produces no @media wrapper — its styles are the baseline. All other breakpoints use @media only screen and (min-width: <em>).
API Reference
TProvider
Prop
Type
AlignContent
Prop
Type
Last updated on