— 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.

@vitus-labs/unistylev2.9.1 · MIT
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.

01

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.

02

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.

03

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.

04

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.

beforeHand-written media queries
Card.tsx
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.

after@vitus-labs/unistyle
Card.tsx
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, alignY to flex properties, with axes swapped for rows

  • Edge shorthands

    inset, margin, padding and border width, style and color from full, x, y and side keys

  • Special keys

    fullScreen, hideEmpty, clearFix, backgroundImage (wrapped in url()) and keyframe + animation

  • between

    a closed-range @media string between two breakpoints, with a 0.02px gap against overlap

  • Typo warnings

    in development an unknown key like paddng warns 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

    styles maps properties, not selectors: a hover: { … } object is an unknown key. Map it to &:hover in your styles callback, as rocketstyle setups do, or write it with css.

  • 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 Provider and a breakpoints map, makeItResponsive renders 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-width media queries on the viewport. For component-sized layouts, write @container rules with css; the property map has containerType but no container breakpoints.

— Recipes

Copy, paste, ship.

01

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.

App.tsx
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>
)
02

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.

Hero.tsx
<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 },
  }}
/>
03

Convert values by hand

The unit helpers are exported, for the places where you write CSS yourself.

units.ts
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']
04

Your own styles callback

styles is optional. Pass any callback; it runs once per breakpoint with that breakpoint's slice of the theme.

Stack.tsx
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/core
Sources · 40 claims, each traced to v2.9.1 code or docs
  1. 250+ property descriptors (about 257 in the property map)ui-system:packages/unistyle/src/styles/styles/index.ts
  2. Pipeline: normalize, transform (property to breakpoint pivot), optimize, wrap in media queriesui-system:packages/unistyle/src/responsive/makeItResponsive.ts
  3. Arrays map to breakpoints by position; holes inherit the previous breakpointui-system:packages/unistyle/src/responsive/normalizeTheme.ts
  4. Breakpoint maps are plain objects (css results / class instances are not mistaken for maps)ui-system:packages/unistyle/src/responsive/isBreakpointMap.ts
  5. Holes behavior change in 2.9: [12, null, 15] gives sm: 12ui-system:packages/unistyle/CHANGELOG.md
  6. Identical consecutive breakpoints are removedui-system:packages/unistyle/src/responsive/optimizeTheme.ts
  7. Each breakpoint emits only declarations that differ from the cascade so farui-system:packages/unistyle/src/responsive/optimizeBreakpointDeltas.ts
  8. Shorthand and longhand invalidate each other in the delta diffui-system:packages/unistyle/src/responsive/optimizeBreakpointDeltas.ts
  9. Function interpolations / foreign engine results take the unoptimized path with real propsui-system:packages/unistyle/src/responsive/makeItResponsive.ts
  10. styled-components RuleSet arrays and Emotion functions are not stringified for the diffui-system:packages/unistyle/src/responsive/makeItResponsive.ts
  11. No breakpoints → plain CSS, no pipelineui-system:packages/unistyle/src/responsive/makeItResponsive.ts
  12. Output cached by theme identity plus a bounded content cache (128 entries)ui-system:packages/unistyle/src/responsive/makeItResponsive.ts
  13. Numbers divided by rootSize and output as rem on web, px on nativeui-system:packages/unistyle/src/units/value.ts
  14. px values convert to rem; rootSize defaults to 16ui-system:packages/unistyle/src/units/value.ts
  15. Zero stays unitlessui-system:packages/unistyle/src/units/value.ts
  16. Media queries in em; breakpoint 0 has no media wrapperui-system:packages/unistyle/src/responsive/createMediaQueries.ts
  17. em breakpoints follow the user's browser font sizeui-system:packages/unistyle/src/responsive/createMediaQueries.ts
  18. Breakpoints are min-width viewport media queriesui-system:packages/unistyle/src/responsive/createMediaQueries.ts
  19. Provider sorts breakpoints and builds media helpers, then wraps core's Providerui-system:packages/unistyle/src/context.tsx
  20. A connector can supply its own createMediaQueries (React Native)ui-system:packages/unistyle/src/context.tsx
  21. Native connector media queries use the window widthui-system:packages/connector-native/src/createMediaQueries.ts
  22. Edge shorthands produce the most compact CSSui-system:packages/unistyle/src/styles/shorthands/edge.ts
  23. Edge keys full / x / y / sides (e.g. paddingX)ui-system:packages/unistyle/src/styles/styles/propertyMap.ts
  24. Logical properties such as marginInlineui-system:packages/unistyle/src/styles/styles/propertyMap.ts
  25. containerType is a property, no container breakpointsui-system:packages/unistyle/src/styles/styles/propertyMap.ts
  26. alignContent swaps X/Y axes for inline directionui-system:packages/unistyle/src/styles/alignContent.ts
  27. backgroundImage wrapped in url()ui-system:packages/unistyle/src/styles/styles/processDescriptor.ts
  28. Special keys fullScreen, hideEmpty, clearFix, animation + keyframeui-system:packages/unistyle/src/styles/styles/processDescriptor.ts
  29. between() is a closed range with a 0.02px decrementui-system:packages/unistyle/src/responsive/between.ts
  30. Dev-only warning for unknown theme keysui-system:packages/unistyle/src/styles/styles/index.ts
  31. Theme types accept var(--x), calc() / clamp() / min() / max()npm:@vitus-labs/unistyle/lib/index.d.ts
  32. Units include ch, cm, mm, in, pt, pc, vmin, vmaxnpm:@vitus-labs/unistyle/lib/index.d.ts
  33. Custom properties, clamp(), min(), ch pass through value()npm:@vitus-labs/unistyle/README.md
  34. hover / focus sub-objects must be mapped to selectors in the styles callbacksite:src/ui/rocket.ts
  35. Elements uses makeItResponsive and alignContentui-system:packages/elements/src/helpers/Wrapper/styled.ts
  36. Coolgrid uses makeItResponsiveui-system:packages/coolgrid/src/Col/styled.ts
  37. Rocketstyle .styles() typically uses makeItResponsivenpm:@vitus-labs/rocketstyle/README.md
  38. This site's theme Provider is unistyle'ssite:src/ui/Providers.tsx
  39. README: 250+ CSS propertiesnpm:@vitus-labs/unistyle/README.md
  40. Peer dependencies react >= 19 and @vitus-labs/corenpm:@vitus-labs/unistyle/package.json