Skip to Content
vitus·labs
Unistyle

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, direction mapped 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 (extendCss callbacks, coolgrid colCss / rowCss, Element css callbacks reading props.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 css result (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 gives sm: 12 (was 15), and [null, 4] leaves xs unset (was 4).
  • The output cache is scoped per styles function and normalize flag, so components sharing a theme object no longer share output.
  • createMediaQueries defaults rootSize to 16 (was NaNem).
  • 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/unistyle

Requires @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

ExportDescription
makeItResponsive()Core responsive engine — returns styled interpolation
breakpointsDefault 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

ExportDescription
styles()Process the 257 CSS property descriptors
alignContent()Convert semantic alignment to flex properties
extendCss()Evaluate custom CSS (callback or string)
ALIGN_CONTENT_DIRECTIONDirection value mappings
ALIGN_CONTENT_MAP_XX-axis alignment mappings
ALIGN_CONTENT_MAP_YY-axis alignment mappings

Unit Utilities

ExportDescription
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

ExportDescription
ProviderTheme provider enriched with breakpoints and media queries
contextReact 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

  1. Reads theme.breakpoints — object mapping names to pixel values
  2. Sorts breakpoints by pixel value ascending via sortBreakpoints()
  3. Creates media object via createMediaQueries() — converts pixels to em for accessibility (respects browser font-size settings), then creates tagged-template wrappers
  4. Attaches both to theme.__VITUS_LABS__ (internal namespace)
  5. Memoizes the enriched theme (recomputes only when the theme, breakpoints or rootSize reference changes). On React Native, the createMediaQueries from the platform connector (config.createMediaQueries) is used instead of the web one
  6. 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

Edit on GitHub

Last updated on

On this page