Responsive System
Breakpoints, media queries, responsive value formats, and the transformation pipeline.
Unistyle's responsive system transforms responsive value declarations into optimized, mobile-first media queries.
Default Breakpoints
{
xs: 0, // Mobile (no media query — baseline)
sm: 576, // Small devices
md: 768, // Tablets
lg: 992, // Desktops
xl: 1200, // Large desktops
xxl: 1440, // Extra-large
}Media queries use em units for accessibility (respects browser font-size settings):
| Breakpoint | Pixel | Em | Media Query |
|---|---|---|---|
xs | 0 | — | No wrapper (baseline) |
sm | 576 | 36em | @media only screen and (min-width: 36em) |
md | 768 | 48em | @media only screen and (min-width: 48em) |
lg | 992 | 62em | @media only screen and (min-width: 62em) |
xl | 1200 | 75em | @media only screen and (min-width: 75em) |
xxl | 1440 | 90em | @media only screen and (min-width: 90em) |
Responsive Value Formats
Every theme property supports three responsive formats:
Scalar (Single Value)
Applied to all breakpoints:
{ fontSize: 16, color: 'red' }
// All breakpoints: fontSize=16, color=redArray (Positional)
Values map to breakpoints in order. Last value carries forward:
{ fontSize: [12, 14, 16, 18] }
// xs: 12, sm: 14, md: 16, lg: 18, xl: 18, xxl: 18
// ↑ last value repeatsA null / undefined hole inherits the previous breakpoint, just like a gap in the object form:
{ fontSize: [12, null, 15] }
// xs: 12, sm: 12, md: 15, lg: 15, ...
{ fontSize: [null, 4] }
// xs: unset, sm: 4, md: 4, ...Behaviour change in 2.9
Before 2.9, [12, null, 15] gave sm: 15 and a leading hole ([null, 4]) gave xs: 4. Holes now inherit from the previous breakpoint as documented, and a leading hole leaves xs unset.
Object (Breakpoint-Keyed)
Explicit breakpoint assignment. Gaps filled by previous breakpoint. Only plain objects count as breakpoint maps. A css result, keyframes or a class instance used as a value is passed to the styles callback untouched (before 2.9 such values were mistaken for breakpoint maps and dropped):
{ fontSize: { xs: 12, md: 16, lg: 18 } }
// xs: 12, sm: 12, md: 16, lg: 18, xl: 18, xxl: 18
// ↑ inherited from xsTransformation Pipeline
The responsive engine applies 5 transformations (normalization can be turned off with normalize: false):
1. Normalize
Expands arrays and objects to full breakpoint maps:
// Input
{ fontSize: [12, 14, 16], padding: { xs: 8, md: 16 }, color: 'red' }
// After normalize
{
fontSize: { xs: 12, sm: 14, md: 16, lg: 16, xl: 16, xxl: 16 },
padding: { xs: 8, sm: 8, md: 16, lg: 16, xl: 16, xxl: 16 },
color: { xs: 'red', sm: 'red', md: 'red', /* … */ }, // scalars repeated
}Fast path: If no arrays or objects are detected among the values, normalization is skipped entirely. The theme passes through unchanged, which avoids the overhead of creating full breakpoint maps.
2. Transform
Pivots from property-centric to breakpoint-centric layout:
// Input (normalized)
{
fontSize: { xs: 12, sm: 14, md: 16 },
padding: { xs: 8, md: 16 },
color: 'red'
}
// After transform
{
xs: { fontSize: 12, padding: 8, color: 'red' },
sm: { fontSize: 14 },
md: { fontSize: 16, padding: 16 },
}3. Optimize
Removes duplicate breakpoints with identical styles:
// Input
{
xs: { fontSize: 16, color: 'red' },
sm: { fontSize: 16, color: 'red' }, // Identical to xs
md: { fontSize: 18, color: 'red' },
lg: { fontSize: 18, color: 'red' }, // Identical to md
}
// After optimize
{
xs: { fontSize: 16, color: 'red' },
md: { fontSize: 18, color: 'red' },
}
// sm and lg removed — avoids duplicate @media blocks4. Per-Breakpoint Delta Diff
After each breakpoint's CSS is rendered, the cascade optimizer compares it to the running cascade and drops declarations whose prop: value is unchanged. Mobile-first @media (min-width: …) already inherits earlier declarations, so re-emitting them at every breakpoint is pure byte waste.
/* Without delta diff — every breakpoint repeats every property */
{ position: absolute; bottom: -4.375rem; right: -12.5rem; height: 28.75rem; }
@media (min-width: 36em) {
position: absolute; bottom: -4.375rem; right: -11.25rem; height: 28.75rem;
}
@media (min-width: 48em) {
position: absolute; bottom: 0; right: -11.25rem; height: 40rem;
}
/* With delta diff — each breakpoint only emits what actually changed */
{ position: absolute; bottom: -4.375rem; right: -12.5rem; height: 28.75rem; }
@media (min-width: 36em) { right: -11.25rem; }
@media (min-width: 48em) { bottom: 0; height: 40rem; }The diff operates on the rendered CSS strings, parsing top-level declarations + opaque blocks (nested selectors / at-rules) with quote-, paren-, and brace-aware scanning. Selector blocks like &:hover { … } are deduped by exact text match.
Related properties — declarations that reset each other invalidate each other's cascade entries, so a value repeated after a reset is re-emitted rather than dropped. This covers shorthand ↔ longhand (padding ↔ padding-top), shorthands whose longhands use a different prefix (border-radius ↔ border-top-left-radius, inset ↔ top, place-items ↔ align-items, gap ↔ row-gap, font ↔ line-height, …). Since 2.9 it also covers cross-family resets (border-top ↔ border-color, border-width ↔ border-top, grid-area ↔ grid-row) and logical ↔ physical sides (margin-inline-start ↔ margin-left). Example: padding: 1rem → padding-top: 0 → padding: 1rem re-emits the third. /* … */ comments are ignored.
Limitation — nested blocks are compared by exact text, so &:hover{color:red} and &:hover { color: red; } would both be emitted.
When the optimizer is skipped — the diff runs on stringified results. A breakpoint result can only be stringified faithfully when it contains no function interpolation, because a css result's toString() resolves with empty props. If any breakpoint's result contains a function, the whole style falls back to the unoptimized path, where the engine resolves the results with the real render-time props. Function interpolations include ${(p) => p.theme…} from extendCss callbacks, coolgrid's colCss / rowCss, and Element css callbacks. The same fallback applies to results that aren't template results at all: Emotion functions, styled-components arrays, connector-native objects. See Function interpolations below.
5. Media Query Wrapping
Each remaining breakpoint gets wrapped in its media query:
/* xs (baseline, no @media) */
font-size: 0.75rem;
color: red;
/* md and larger */
@media only screen and (min-width: 48em) {
font-size: 1.125rem;
}makeItResponsive()
The core responsive engine used by styled components:
import { makeItResponsive, styles } from '@vitus-labs/unistyle'
import { config } from '@vitus-labs/core'
const ResponsiveBox = config.styled('div')`
${makeItResponsive({
key: '$box',
css: config.css,
// called once per breakpoint with { theme, css, rootSize, globalTheme }
styles,
})}
`Options
| Option | Type | Description |
|---|---|---|
key | string | Prop name holding the theme (e.g., '$box') |
theme | object | Fixed theme to use instead of props[key] |
css | function | CSS tagged template function |
styles | function | Style callback ({ theme, css, rootSize, globalTheme }) => css — theme is the per-breakpoint slice, rootSize comes from the Provider theme, globalTheme is the rest of the Provider theme |
normalize | boolean | Enable/disable normalization (default: true) |
Function Interpolations
Styles may contain function interpolations that read render-time props:
const Card = config.styled('div')`
${makeItResponsive({
key: '$card',
css: config.css,
styles: ({ theme, css }) => css`
padding: ${theme.padding}px;
color: ${(props) => props.theme.colors.text};
`,
})}
`
<Card $card={{ padding: [8, 16] }} />Since 2.9, styles like this take the unoptimized path (no delta diff) and are resolved with the component's real props. Before 2.9, makeItResponsive stringified them with empty props whenever breakpoints were configured. The function line then rendered the wrong value or was silently dropped. This affected extendCss callbacks, coolgrid colCss / rowCss and Element css callbacks that read props.theme.
No-Breakpoint Fast Path
When the theme has no breakpoints at all (no __VITUS_LABS__ data), makeItResponsive skips the entire normalize/transform/optimize pipeline and renders plain CSS directly — a single pass through the styles function.
Caching
makeItResponsive caches per styles function and normalize flag. Two components that share one theme object but use different styles callbacks never share output (before 2.9 they could):
- Optimized theme —
WeakMapkeyed by the theme prop object; reused while the sorted breakpoints are unchanged, so normalize → transform → optimize runs once per theme object. - Rendered output — keyed by the theme prop object and the outer (Provider) theme object. When both are stable, the previous output is returned immediately.
- Content-keyed fallback — a fresh plain-data theme object (e.g. an inline
$element={{ … }}created every render) misses by identity, so it is also looked up by its JSON content. This is a bounded LRU of 128 entries per outer theme. Themes containing functions, CSS results or class instances skip this cache.
Responsive Alignment
The alignment system supports responsive values:
const theme = {
display: 'flex',
direction: { xs: 'rows', md: 'inline' }, // Column on mobile, row on desktop
alignX: { xs: 'center', md: 'left' },
alignY: 'center',
gap: [8, 12, 16], // Scales up with breakpoints
}Custom Breakpoints
Configure custom breakpoints via the Provider:
<Provider
theme={{
rootSize: 16,
breakpoints: {
mobile: 0,
tablet: 600,
desktop: 1024,
wide: 1440,
},
}}
>
<App />
</Provider>
// Usage with custom breakpoint names
<Box $box={{
fontSize: { mobile: 14, tablet: 16, desktop: 18 },
padding: { mobile: 8, desktop: 24 },
}} />API Reference
Breakpoints
Prop
Type
Value
Prop
Type
MakeItResponsive
Prop
Type
Last updated on