Skip to content

teamgroove/react-native-tailwind

Β 
Β 

Repository files navigation

logo

npm version npm total downloads npm monthly downloads npm license
build status coverage dependencies status

Overview

Compile-time Tailwind CSS for React Native with zero runtime overhead. Transform className props to optimized StyleSheet.create calls at build time using a Babel plugin.

Features

  • ⚑ Zero runtime overhead β€” All transformations happen at compile time
  • πŸ”§ No dependencies β€” Direct-to-React-Native style generation without tailwindcss package
  • 🎯 Babel-only setup β€” No Metro configuration required
  • πŸ“ TypeScript-first β€” Full type safety and autocomplete support
  • πŸš€ Optimized performance β€” Compiles down to StyleSheet.create for optimal performance
  • πŸ“¦ Small bundle size β€” Only includes actual styles used in your app
  • 🎨 Custom colors β€” Extend the default palette via tailwind.config.*
  • πŸ“ Arbitrary values β€” Use custom sizes and borders: w-[123px], rounded-[20px]
  • πŸ”€ Dynamic className β€” Conditional styles with hybrid compile-time optimization
  • πŸƒ Runtime option β€” Optional tw template tag for fully dynamic styling (~25KB)
  • 🎯 State modifiers β€” active:, hover:, focus:, and disabled: modifiers for interactive components
  • πŸ“± Platform modifiers β€” ios:, android:, and web: modifiers for platform-specific styling
  • πŸŒ“ Color scheme modifiers β€” dark: and light: modifiers for automatic theme adaptation
  • 🎨 Scheme modifier β€” scheme: convenience modifier that expands to both dark: and light: variants
  • πŸ“œ Special style props β€” Support for contentContainerClassName, columnWrapperClassName, and more
  • πŸŽ›οΈ Custom attributes β€” Configure which props to transform with exact matching or glob patterns

Demo

demo

Quick Start

Installation

npm install @mgcrea/react-native-tailwind
# or
yarn add @mgcrea/react-native-tailwind
# or
pnpm add @mgcrea/react-native-tailwind

1. Add Babel Plugin

Update your babel.config.js:

module.exports = {
  presets: ["module:@react-native/babel-preset"],
  plugins: [
    "@mgcrea/react-native-tailwind/babel", // Add this line
  ],
};

2. Enable TypeScript Support (Optional)

Create a type declaration file to enable className prop autocomplete:

// src/types/react-native-tailwind.d.ts
import "@mgcrea/react-native-tailwind/react-native";

3. Start Using className

import { View, Text } from "react-native";

export function MyComponent() {
  return (
    <View className="flex-1 bg-gray-100 p-4">
      <Text className="text-xl font-bold text-blue-500">Hello, Tailwind!</Text>
    </View>
  );
}

How It Works

The Babel plugin transforms your code at compile time:

Input (what you write):

<View className="m-4 p-2 bg-blue-500 rounded-lg" />

Output (what Babel generates):

import { StyleSheet } from "react-native";

<View style={_twStyles._bg_blue_500_m_4_p_2_rounded_lg} />;

const _twStyles = StyleSheet.create({
  _bg_blue_500_m_4_p_2_rounded_lg: {
    margin: 16,
    padding: 8,
    backgroundColor: "#3B82F6",
    borderRadius: 8,
  },
});

Documentation

πŸ“š Full Documentation

Examples

Dynamic Styles

import { useState } from "react";
import { Pressable, Text } from "react-native";

export function ToggleButton() {
  const [isActive, setIsActive] = useState(false);

  return (
    <Pressable
      onPress={() => setIsActive(!isActive)}
      className={isActive ? "bg-green-500 p-4" : "bg-red-500 p-4"}
    >
      <Text className="text-white">{isActive ? "Active" : "Inactive"}</Text>
    </Pressable>
  );
}

State Modifiers

import { Pressable, Text } from "@mgcrea/react-native-tailwind";

export function MyButton() {
  return (
    <Pressable className="bg-blue-500 active:bg-blue-700 p-4 rounded-lg">
      <Text className="text-white font-semibold">Press Me</Text>
    </Pressable>
  );
}

Dark Mode

import { View, Text } from "react-native";

export function ThemeCard() {
  return (
    <View className="bg-white dark:bg-gray-900 p-4 rounded-lg">
      <Text className="text-gray-900 dark:text-white">Adapts to device theme</Text>
    </View>
  );
}

Platform-Specific Styles

import { View, Text } from "react-native";

export function PlatformCard() {
  return (
    <View className="p-4 ios:p-6 android:p-8 bg-white rounded-lg">
      <Text className="text-base ios:text-blue-600 android:text-green-600">
        Platform-specific styles
      </Text>
    </View>
  );
}

Contributing

Contributions are welcome! Please read our Contributing Guide for details.

Authors

License

MIT License

Copyright (c) 2025 Olivier Louvignes <olivier@mgcrea.io>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

About

Compile-time Tailwind CSS for React Native with zero runtime overhead

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • TypeScript 99.2%
  • JavaScript 0.8%