CSS to React Native Style Transformer

3.2.0 · active · verified Sun Apr 19

css-to-react-native is a utility library that converts standard CSS-like style declarations into React Native stylesheet objects. Currently at version 3.2.0, it is actively maintained by the styled-components organization, indicating a stable release cadence. This library is essential for bridging web CSS styling conventions with React Native's unique styling model, automatically handling unit conversions, shorthand expansions, and platform-specific property transformations (e.g., `text-shadow-offset` to `{ width, height }`). A key differentiator is its ability to handle complex transformations like reversing `transform` order for React Native compatibility and expanding various shorthands such as `font` and `margin`. It focuses on transforming already parsed CSS property-value pairs, delegating the initial CSS parsing to external tools like PostCSS.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates converting an array of CSS property-value tuples, including shorthands and complex values, into a React Native stylesheet object. Shows how `transform` order is handled.

import transform from 'css-to-react-native';

const cssStyleTuples = [
  ['font', 'bold 14px/16px "Helvetica"'],
  ['margin', '5px 7px 2px'],
  ['border-left-width', '5px'],
  ['text-shadow-offset', '10px 5px'],
  ['transform', 'translate(10px, 5px) scale(2) rotate(45deg)']
];

const reactNativeStyles = transform(cssStyleTuples);

console.log(reactNativeStyles);
/*
Expected output similar to:
{
  fontFamily: 'Helvetica',
  fontSize: 14,
  fontWeight: 'bold',
  fontStyle: 'normal',
  fontVariant: [],
  lineHeight: 16,
  marginTop: 5,
  marginRight: 7,
  marginBottom: 2,
  marginLeft: 7,
  borderLeftWidth: 5,
  textShadowOffset: { width: 10, height: 5 },
  transform: [
    { rotate: '45deg' },
    { scale: 2 },
    { translateY: 5 },
    { translateX: 10 }
  ]
}
*/

view raw JSON →