npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tingyuan/react-native-tailwindcss

v0.0.18

Published

use tailwindcss in react-native project

Downloads

212

Readme

react-native-tailwindcss npm version

Use tailwindcss in react-native project

install

npm install @tingyuan/react-native-tailwindcss --save-dev

usage

  1. Run npx tailwindcss init if tailwind.config.js does not exist. (see installation).

  2. Add the babel plugin to babel.config.js.

    module.exports = function (api) {
      api.cache(true)
      return {
        presets: ['babel-preset-expo'],
        plugins: ['@tingyuan/react-native-tailwindcss/babel'],
      }
    }
  3. Start tailwindcss watch task.

for development, run:

npx react-native-tailwindcss-start

for build, run this before the building app command:

npx react-native-tailwindcss-build

example

function App(props: { textColor: string }) {
  const renderItem = ({ item }) => <Text>{item.title}</Text>
  return (
    <View className="flex-1 flex-row h-full">
      <Text className="font-bold text-green-500 text-lg" style={{ color: props.textColor }}>
        Hello Tailwindcss
      </Text>
      <FlashList
        data={DATA}
        renderItem={renderItem}
        estimatedItemSize={200}
        contentContainerStyle={tw('px-3 bg-slate-500')}
      />
    </View>
  )
}

jsx attribute className will be transformed to style and tw('') will be transformed to style object.

TypeScript support

To support infer global tw helper function type and className jsx attribute, you can:

add three slash directive in a .d.ts file:

/// <reference types="@tingyuan/react-native-tailwindcss/types" />

or import the types in a .d.ts file:

import "@tingyuan/react-native-tailwindcss/types"

or add the types in your tsconfig.json:

{ "types": [ "@tingyuan/react-native-tailwindcss/types"] }

Tailwind CSS IntelliSense

support tw() code hints and completion

"tailwindCSS.experimental.classRegex": [
  [
    "tw\\(([^)]*)\\)",
    "[\"'`]([^\"'`]*).*?[\"'`]"
  ],
]

Eslint

{
  globals: { tw: 'readonly' },
}

Caveats

If you encounter any problem, please try to restart your app without cache:

  • Expo npx expo start --clear
  • React Native CLI npx react-native start --reset-cache

The babel plugin will automatically inject const tw = useTw() in each react component function.

useTw is a hook callee, so using className or tw() must be in react component function.

When the function name is "UpperCamelCase" style and has jsx statement, the function self will be treated as a react component function.

So when you encounter error like Rendered fewer hooks than expected or Property 'tw' doesn't exist, you need to obey the limitation mentioned above.

If you see error like class xxx does not exist in generated tailwind styles, please make sure to run command mentioned above in No.3 (Start tailwindcss watch task for development.)

Limitation ⚠

I have to say, this project is not a comprehensive solution to use all features of tailwindcss in react-native(In fact, a considerable portion of the tailwindcss functionality is inherently exclusive to the web).

But it will cover the most useful features of tailwindcss, including most of utilities classes and some prefix modifiers such as dark mode, responsive break point, vw/vh unit, viewport orientation.

If you seek more enhanced and comprehensive solution, you can try NativeWind.