Skip to Content
vitus·labs
Connectors

React Native Connector

CSS engine adapter for React Native — parses CSS template literals into style objects.

@vitus-labs/connector-native bridges the Vitus Labs component system to React Native. Instead of generating CSS class names and injecting <style> rules (as web connectors do), it parses CSS template literals into React Native StyleSheet-compatible objects and applies them via the style prop.

Installation

npm install @vitus-labs/connector-native

Peer dependencies: @vitus-labs/core, react >= 19, react-native >= 0.76

What's new in 2.9

parseCSS now parses leading-dot and +-signed numbers (.5 → 0.5, +2 → 2) and skips balanced nested {…} blocks (&:hover { … }, @media … { … }) instead of letting their declarations corrupt the surrounding ones. See CSS Parsing.

Configuration

src/init.ts
import { init } from '@vitus-labs/core'
import { View, Text } from 'react-native'
import {
  css,
  styled,
  provider,
  useTheme,
  createMediaQueries,
} from '@vitus-labs/connector-native'

init({
  css,
  styled,
  provider,
  useTheme,
  component: View,
  textComponent: Text,
  createMediaQueries,
})

Import this file early in your app entry point (e.g., App.tsx or index.ts).

How It Works

css()

The css tagged template parses CSS declarations into a flat style object. It supports the same tagged template syntax as the web connectors, but the output is a React Native style object instead of a CSS string.

import { css } from '@vitus-labs/connector-native'

// Static — parsed once at creation time
const base = css`
  width: 100px;
  background-color: blue;
  border-radius: 8px;
`
base.resolve({}) // { width: 100, backgroundColor: 'blue', borderRadius: 8 }

// Dynamic — resolved per render with props
const dynamic = css`
  width: ${(p) => p.$size}px;
  color: ${(p) => p.$color};
`
dynamic.resolve({ $size: 200, $color: 'red' }) // { width: 200, color: 'red' }

The result is a plain object — { statics, dynamics, resolve(props) } — not a CSS string. Static templates are parsed once; dynamic ones are parsed per distinct resolved CSS text (small per-template cache). A nested css result that has its own function interpolations makes the outer template dynamic too.

CSS Parsing

  • CSS property names are converted to camelCase (background-color → backgroundColor)
  • px values become numbers (100px → 100)
  • Plain numbers become numbers, including signed and leading-dot forms (0.5, .5 → 0.5, -4, +2 → 2)
  • font-weight always stays a string ('700'), as React Native requires
  • rem / em values are not converted (no root size on native) and stay strings; unitless line-height is kept as written (RN's lineHeight is absolute, not a multiplier) — use px for both
  • Color values, keywords and other non-numeric values stay as strings
  • !important is stripped
  • Multi-value box shorthands expand to RN longhands: margin, padding, inset, border-width (top/right/bottom/left), border-radius (per corner) and gap (rowGap / columnGap) — e.g. padding: 8px 16px → paddingTop: 8, paddingRight: 16, …. Values containing ( or / are not split
  • /* block */ comments and // line comments (at the start of a declaration) are stripped; ; inside parentheses or quotes (url(data:…;base64,…), content: ";") does not split a declaration
  • Nested blocks (&:hover { … }, @media … { … }) are skipped entirely — RN style objects can't express them

styled()

Creates a React component that resolves the CSS template into a style object and applies it via the style prop. Works with any React Native component. Only the call form styled(Component) exists — there is no styled.View shorthand.

import { styled } from '@vitus-labs/connector-native'
import { View, Text } from 'react-native'

const Box = styled(View)`
  padding: 16px;
  background-color: #f0f0f0;
  border-radius: 8px;
`

const Label = styled(Text)`
  font-size: 14px;
  color: ${(p) => p.$active ? 'blue' : 'gray'};
`

// Usage
<Box>
  <Label $active>Hello React Native</Label>
</Box>

Prop filtering: By default, as, $-prefixed, and data-* props are not forwarded to the underlying component; everything else is. children and style are always passed through. Replace the default filter with shouldForwardProp:

const Custom = styled(View, {
  shouldForwardProp: (prop) => !prop.startsWith('internal'),
})`
  padding: 16px;
`

Style merging: An explicit style prop (object or nested array) is merged with the template styles (explicit wins):

<Box style={{ backgroundColor: 'red' }} />
// backgroundColor overrides #f0f0f0 from the template

Theme: dynamic templates receive the context theme as props.theme for resolution only — it is not forwarded to the RN component. An explicitly passed theme prop takes precedence and is forwarded as-is.

Re-renders: components with dynamic interpolations subscribe to useWindowDimensions(), so responsive styles update on rotation / resize. Fully static templates skip the hook (and the theme read) and never re-render for dimension changes.

ref is a regular prop (React 19) and is passed to the underlying component.

ThemeProvider & useTheme

Provides a theme object via React context (exported as both ThemeProvider and provider; useTheme() returns {} without a provider). Unlike styler's ThemeProvider, the native provider takes a theme object only — function themes are not supported:

import { ThemeProvider, useTheme } from '@vitus-labs/connector-native'

const theme = {
  colors: { primary: '#0d6efd', text: '#333' },
  spacing: 8,
}

<ThemeProvider theme={theme}>
  <App />
</ThemeProvider>

createMediaQueries

The web unistyle system uses CSS @media queries for responsive styles. React Native has no CSS media queries, so createMediaQueries provides an equivalent by evaluating the current screen width against breakpoints at render time (mobile-first).

import { createMediaQueries } from '@vitus-labs/connector-native'

This is passed to init() and used automatically by @vitus-labs/unistyle when rendering on React Native. You don't call it directly — it's an internal integration point.

How it works:

  • Base breakpoint (value 0) — styles always apply
  • Other breakpoints — styles apply only when Dimensions.get('window').width >= breakpointValue
  • The width is checked lazily when the style is resolved, not when the breakpoint function is called, so upstream caches (e.g. unistyle's) never bake in a stale width; combined with the useWindowDimensions() subscription in styled, responsive styles follow rotation / resize

Differences from Web Connectors

FeatureWeb (styler/SC/emotion)Native
OutputCSS string → class nameStyle object → style prop
@media queriesNative CSSDimensions.get('window').width check
CSS nesting (&:hover)SupportedIgnored (no CSS in RN)
keyframesSupportedNot available
createGlobalStyleSupportedNot available
as propPolymorphic elementNot supported (not forwarded)
data-* propsForwarded to DOMNot forwarded
styled.tag shorthandYesNo — styled(Component) only
Function themesYesNo
Transient $ propsNot forwarded to DOMNot forwarded to component
shouldForwardPropSupportedSupported

Supported CSS Properties

The parser does not validate property names — every declaration is camelCased and passed through, so use properties React Native supports:

  • Layout: width, height, padding, margin, flex, align-items, justify-content, etc.
  • Visual: background-color, border-radius, border-width, opacity, etc.
  • Typography: font-size, font-weight, color, line-height, text-align, etc.
  • Transform: transform is kept as a string — it must use syntax React Native accepts

Properties React Native doesn't know are passed through unchanged; React Native decides whether to warn or ignore them.

Edit on GitHub

Last updated on

On this page