React Native CSS Interop

0.2.3 · active · verified Sun Apr 19

react-native-css-interop provides a foundational layer for integrating CSS stylesheets into React Native and React Native Web projects. It is a core dependency for libraries like NativeWind, enabling the use of CSS as a styling language, including advanced features like CSS Variables, Viewport Units (rem, vw, vh, vmax, vmin), Media Queries, and Container Queries. The library is not a full CSS specification implementation but offers an opinionated approach to CSS in React Native. The current stable version is 0.2.3, with active maintenance through patch releases. A new major version of NativeWind (v5) is in preview, which renames `react-native-css-interop` to `react-native-css` and shifts it to a peer dependency, indicating a significant architectural change and decoupling for future versions.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates registering `Text` and `View` components with `cssInterop` to accept `className` props, enabling CSS styling.

import React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import { cssInterop } from 'react-native-css-interop';

// Register a custom component to accept className props
cssInterop(Text, { className: 'style' });
cssInterop(View, { className: 'style' });

// Assuming you have a global.css file imported somewhere (e.g., via NativeWind setup)
// which defines .container and .text-style classes
// import './global.css';

export default function App() {
  return (
    <View className="container p-4 bg-blue-500 rounded-lg">
      <Text className="text-xl font-bold text-white text-center">
        Hello, CSS Interop!
      </Text>
      <Text className="mt-2 text-base text-gray-200">
        This component uses CSS classes processed by react-native-css-interop.
      </Text>
    </View>
  );
}

// Minimal StyleSheet to ensure React Native doesn't complain about missing styles
// (actual styles would come from your CSS file/Tailwind processing)
const baseStyles = StyleSheet.create({
  container: {
    // These would be overridden by CSS/Tailwind
    padding: 16,
    backgroundColor: 'blue',
    borderRadius: 8,
  },
  'text-xl': {
    fontSize: 20,
  },
  'font-bold': {
    fontWeight: 'bold',
  },
  'text-white': {
    color: 'white',
  },
  'text-center': {
    textAlign: 'center',
  },
  'mt-2': {
    marginTop: 8,
  },
  'text-base': {
    fontSize: 16,
  },
  'text-gray-200': {
    color: 'lightgray',
  },
});

view raw JSON →