— Styling · @vitus-labs/unistyle
Write values, not media queries.
Describe styles as an object, { padding: { xs: 8, md: 16 }, fontSize: [14, 16] }, and unistyle writes the CSS: 250+ CSS property descriptors, px-to-rem conversion and one mobile-first @media block per breakpoint that actually changes something. It's the layer under elements, coolgrid and most rocketstyle setups.
npm i @vitus-labs/unistyle @vitus-labs/core- CSS property descriptors
- 250+
- value forms: scalar, array, breakpoint map
- 3
- numbers divided by rootSize
- px → rem
— Why unistyle
Responsive by value, not by block.
Every property is responsive
Any value can be a scalar, a mobile-first array or a breakpoint map. Unistyle pivots the object from property-per-breakpoint to breakpoint-per-property and wraps each breakpoint in its min-width query.
Only the CSS that changed
Breakpoints identical to the previous one are dropped, then each remaining breakpoint is diffed against the cascade so far. A color that never changes is written once, at the base, not five times.
Numbers become rem
padding: 16 renders 1rem at the default rootSize of 16, and '16px' converts too. Breakpoints are emitted in em, so layouts follow a user's browser font size. On React Native the same values stay in px.
Shorthands, compacted
paddingX, paddingY, paddingTop and friends resolve to the shortest valid CSS: padding: 0.5rem 1rem when all four sides are known, individual sides otherwise. Logical properties like marginInline are there too.
— Before / after
Same card. No media blocks.
A card whose padding and type size grow with the viewport. On the left the breakpoints are written as CSS; on the right they're values on the props that use them.
import { styled } from '@vitus-labs/styler'
const Card = styled.div`
padding: 0.5rem 1rem;
font-size: 0.875rem;
border-radius: 0.5rem;
@media (min-width: 48em) {
padding: 1rem 1.5rem;
font-size: 1rem;
}
@media (min-width: 62em) {
padding: 1.5rem 2rem;
}
`
<Card>…</Card>Every breakpoint repeats the selector, every value is converted to rem by hand, and the breakpoints live in strings.
import { config } from '@vitus-labs/core'
import { makeItResponsive, styles } from '@vitus-labs/unistyle'
const Card = config.styled('div')`
${makeItResponsive({ key: '$card', styles, css: config.css })};
`
<Card
$card={{
paddingY: { xs: 8, md: 16, lg: 24 },
paddingX: { xs: 16, md: 24, lg: 32 },
fontSize: { xs: 14, md: 16 },
borderRadius: 8,
}}
>
…
</Card>One interpolation, values in px or arrays or maps. Unistyle converts, groups by breakpoint and emits only what changed.
— Features
The pieces under every layout.
- makeItResponsive
a styled interpolation that turns a theme object into breakpoint-wrapped CSS
- styles
the data-driven processor: 250+ descriptors, looked up by the keys you actually use
- Array holes inherit
[12, null, 6]keeps 12 at the second breakpoint instead of skipping to 6 - Delta diff
each breakpoint emits only declarations that differ; shorthands and longhands invalidate each other
- Provider
sorts breakpoints and builds the media helpers once, then hands the theme to core's provider
- value · values · stripUnit
the unit conversion helpers, usable on their own; zero always stays unitless
- alignContent
semantic
direction,alignX,alignYto flex properties, with axes swapped for rows - Edge shorthands
inset,margin,paddingand border width, style and color fromfull,x,yand side keys - Special keys
fullScreen,hideEmpty,clearFix,backgroundImage(wrapped inurl()) andkeyframe+animation - between
a closed-range
@mediastring between two breakpoints, with a 0.02px gap against overlap - Typo warnings
in development an unknown key like
paddngwarns once instead of rendering nothing silently - Cached output
results cached by theme identity, plus a bounded content cache for fresh inline objects
— API
A pipeline, plus the parts.
Every name links to its reference in the docs.
— Honest limits
Where a style object falls short.
Where it isn't the right fit, and what to reach for instead.
Pseudo states aren't keys
stylesmaps properties, not selectors: ahover: { … }object is an unknown key. Map it to&:hoverin your styles callback, as rocketstyle setups do, or write it withcss.The finest diff needs plain results
Per-declaration diffing runs on strings and styler results without function interpolations. Function interpolations, styled-components or Emotion results take the unoptimized path: correct, but each changed breakpoint emits its full CSS.
Breakpoints come from the Provider
Without unistyle's
Providerand abreakpointsmap,makeItResponsiverenders the theme in one pass with no media queries, so array and map values aren't split per breakpoint. Wrap the app once.Viewport only
Breakpoints are
min-widthmedia queries on the viewport. For component-sized layouts, write@containerrules withcss; the property map hascontainerTypebut no container breakpoints.
— Recipes
Copy, paste, ship.
Set up the breakpoints once
The Provider sorts breakpoints and builds media helpers for every component below it. A breakpoint of 0 gets no media wrapper.
import { Provider } from '@vitus-labs/unistyle'
const theme = {
rootSize: 16,
breakpoints: { xs: 0, sm: 576, md: 768, lg: 992, xl: 1200 },
}
export const App = ({ children }) => (
<Provider theme={theme}>{children}</Provider>
)Three ways to say responsive
Arrays map to breakpoints by position, maps by name. A hole inherits the previous value, exactly like the map form.
<Box
$box={{
display: 'flex',
gap: 16, // every breakpoint
fontSize: [14, null, 18], // xs 14, sm 14, md+ 18
flexDirection: { xs: 'column', md: 'row' },
paddingX: { xs: 16, lg: 48 },
}}
/>Convert values by hand
The unit helpers are exported, for the places where you write CSS yourself.
import { stripUnit, value } from '@vitus-labs/unistyle'
value(24) // '1.5rem'
value('16px') // '1rem'
value(0) // 0 (unitless)
value(24, 16, 'px') // '24px'
stripUnit('24px', true) // [24, 'px']Your own styles callback
styles is optional. Pass any callback; it runs once per breakpoint with that breakpoint's slice of the theme.
import { config } from '@vitus-labs/core'
import { alignContent, makeItResponsive } from '@vitus-labs/unistyle'
const Stack = config.styled('div')`
display: flex;
${makeItResponsive({
key: '$stack',
css: config.css,
styles: ({ theme: t, css }) => css`
${alignContent({ direction: t.direction, alignX: t.alignX, alignY: t.alignY })};
`,
})};
`
<Stack $stack={{ direction: ['rows', 'inline'], alignX: 'center', alignY: 'top' }} />— FAQ
Questions, answered.
Do I need rocketstyle or elements to use it?
No. It needs @vitus-labs/core (for the configured css and the theme context) and any connector. Elements, coolgrid and rocketstyle setups use it underneath, but makeItResponsive works in any styled component.
How many CSS properties does it know?
The property map has 250+ descriptors, from layout and spacing to grid, typography, masks and print.
Can theme values be CSS variables or `clamp()`?
Yes. Strings pass through value() untouched, and the theme types accept var(--token) (colors too), calc(), clamp(), min(), max() and units such as ch, vmin or pt.
Does it work on React Native?
Yes. Values convert to px instead of rem, and a native connector can provide its own createMediaQueries that matches breakpoints against the window width.
Can I use different breakpoint names?
Yes. Breakpoints are any name: px map; they're sorted by value, and arrays map onto them in that order.
— @vitus-labs/unistyle
Give values. Get the media queries.
npm i @vitus-labs/unistyle @vitus-labs/coreSources · 40 claims, each traced to v2.9.1 code or docs
- 250+ property descriptors (about 257 in the property map)ui-system:packages/unistyle/src/styles/styles/index.ts
- Pipeline: normalize, transform (property to breakpoint pivot), optimize, wrap in media queriesui-system:packages/unistyle/src/responsive/makeItResponsive.ts
- Arrays map to breakpoints by position; holes inherit the previous breakpointui-system:packages/unistyle/src/responsive/normalizeTheme.ts
- Breakpoint maps are plain objects (css results / class instances are not mistaken for maps)ui-system:packages/unistyle/src/responsive/isBreakpointMap.ts
- Holes behavior change in 2.9: [12, null, 15] gives sm: 12ui-system:packages/unistyle/CHANGELOG.md
- Identical consecutive breakpoints are removedui-system:packages/unistyle/src/responsive/optimizeTheme.ts
- Each breakpoint emits only declarations that differ from the cascade so farui-system:packages/unistyle/src/responsive/optimizeBreakpointDeltas.ts
- Shorthand and longhand invalidate each other in the delta diffui-system:packages/unistyle/src/responsive/optimizeBreakpointDeltas.ts
- Function interpolations / foreign engine results take the unoptimized path with real propsui-system:packages/unistyle/src/responsive/makeItResponsive.ts
- styled-components RuleSet arrays and Emotion functions are not stringified for the diffui-system:packages/unistyle/src/responsive/makeItResponsive.ts
- No breakpoints → plain CSS, no pipelineui-system:packages/unistyle/src/responsive/makeItResponsive.ts
- Output cached by theme identity plus a bounded content cache (128 entries)ui-system:packages/unistyle/src/responsive/makeItResponsive.ts
- Numbers divided by rootSize and output as rem on web, px on nativeui-system:packages/unistyle/src/units/value.ts
- px values convert to rem; rootSize defaults to 16ui-system:packages/unistyle/src/units/value.ts
- Zero stays unitlessui-system:packages/unistyle/src/units/value.ts
- Media queries in em; breakpoint 0 has no media wrapperui-system:packages/unistyle/src/responsive/createMediaQueries.ts
- em breakpoints follow the user's browser font sizeui-system:packages/unistyle/src/responsive/createMediaQueries.ts
- Breakpoints are min-width viewport media queriesui-system:packages/unistyle/src/responsive/createMediaQueries.ts
- Provider sorts breakpoints and builds media helpers, then wraps core's Providerui-system:packages/unistyle/src/context.tsx
- A connector can supply its own createMediaQueries (React Native)ui-system:packages/unistyle/src/context.tsx
- Native connector media queries use the window widthui-system:packages/connector-native/src/createMediaQueries.ts
- Edge shorthands produce the most compact CSSui-system:packages/unistyle/src/styles/shorthands/edge.ts
- Edge keys full / x / y / sides (e.g. paddingX)ui-system:packages/unistyle/src/styles/styles/propertyMap.ts
- Logical properties such as marginInlineui-system:packages/unistyle/src/styles/styles/propertyMap.ts
- containerType is a property, no container breakpointsui-system:packages/unistyle/src/styles/styles/propertyMap.ts
- alignContent swaps X/Y axes for inline directionui-system:packages/unistyle/src/styles/alignContent.ts
- backgroundImage wrapped in url()ui-system:packages/unistyle/src/styles/styles/processDescriptor.ts
- Special keys fullScreen, hideEmpty, clearFix, animation + keyframeui-system:packages/unistyle/src/styles/styles/processDescriptor.ts
- between() is a closed range with a 0.02px decrementui-system:packages/unistyle/src/responsive/between.ts
- Dev-only warning for unknown theme keysui-system:packages/unistyle/src/styles/styles/index.ts
- Theme types accept var(--x), calc() / clamp() / min() / max()npm:@vitus-labs/unistyle/lib/index.d.ts
- Units include ch, cm, mm, in, pt, pc, vmin, vmaxnpm:@vitus-labs/unistyle/lib/index.d.ts
- Custom properties, clamp(), min(), ch pass through value()npm:@vitus-labs/unistyle/README.md
- hover / focus sub-objects must be mapped to selectors in the styles callbacksite:src/ui/rocket.ts
- Elements uses makeItResponsive and alignContentui-system:packages/elements/src/helpers/Wrapper/styled.ts
- Coolgrid uses makeItResponsiveui-system:packages/coolgrid/src/Col/styled.ts
- Rocketstyle .styles() typically uses makeItResponsivenpm:@vitus-labs/rocketstyle/README.md
- This site's theme Provider is unistyle'ssite:src/ui/Providers.tsx
- README: 250+ CSS propertiesnpm:@vitus-labs/unistyle/README.md
- Peer dependencies react >= 19 and @vitus-labs/corenpm:@vitus-labs/unistyle/package.json