2

I am using twrnc package for using Tailwind CSS in React Native Project.

The syntax for that is,

<View style={styles.container}>
  <Text style={tw.style`text-green-500 font-bold`}>Open up App.js to start working on your app!</Text>
  <StatusBar style="auto" />
</View>

But I am not getting suggestions from VSCode for Tailwind CSS classes. Can anyone suggest or help to get suggestions for classes?

1

5 Answers 5

7
 - //Use TailwindCss in ReactNative:

  - Install twrnc: npm install twrnc
  - import tw from "twrnc";
  - Be sure to install the TailWind CSS Intellisense extension in VS code 
  - Go to the extension settings and
  add 'style' in Class Attributes 
  - Go to your project root folder and
  manually create a file named 'tailwind.js' or 'tailwind.config.js'
  - Start using TWCSS as in the docs: eg; <View style={tw`bg-gray-300
  flex flex-1 px-5 pt-20`}>
Sign up to request clarification or add additional context in comments.

3 Comments

manually create a file named 'tailwind.js' or 'tailwind.config.js' this is what I missed
Instead of manually create 'tailwind.config.ja' run npx tailwindcss init this will create empty one with basic content.
To me, this seems like a somewhat incomplete answer. I've explained it in more detail here.
6

First install Tailwind CSS IntelliSense extenstion. Then in the setting, add style to class attributes

4 Comments

Can you please show me where to add and what to add exactly?
When you open the settings of that extension, there are first a few attributes that you can add your attribute (style)
@AkshitaKaretiya Did this work for you? I tried but it did not.
To me, this seems like a somewhat incomplete answer. I've explained it in more detail here.
3

If you haven’t already, install the Tailwind CSS IntelliSense extension.

This extension’s autocomplete and code hinting will only work on props named className or class, which React Native’s components do not have. So I’ve found that a worthwhile workaround is to implement your own versions of the React Native components and use those instead. Your version can have a className prop, which enables the suggestions from VSCode for Tailwind.

For example, your implementation of Text:

import { Text as TextRn } from "react-native";
import tw from "twrnc";

const Text = ({ className, children, ...rest }) => {
  return <TextRn style={tw.style(className)} {...rest}>{children}</TextRn>
};

export default Text;

And the usage:

import View from "./src/Text"; // or wherever you added the Text.js file

// ...

<Text className="text-green-500 font-bold">Open up App.js to start working on your app!</Text>

4 Comments

Are you using a Tailwind CSS extension for VS code?
@JamieGarcia Yes, I am using Tailwind CSS IntelliSense marketplace.visualstudio.com/…
You can also use the experimental class regex setting so you don't need to use. a wrapper component: github.com/jaredh159/tailwind-react-native-classnames/…
To me, this seems like a somewhat incomplete answer. I've explained it in more detail here.
1

I'll extend @Brandon's answer ( https://stackoverflow.com/a/71718286/7733418 ).

After installing Tailwind CSS IntelliSense extension, what worked for me was to add a new attribute named "style" under the extension's settings.

Tailwind CSS IntelliSense extension settings

3 Comments

You know that commenting requires a privilegre you do not have yet. In that situation please do not decide to misuse a different mechanism (and answer post) for something it is not meant for and which you are not yet allowed to do. See meta.stackexchange.com/questions/214173/… Please delete this, if you still think it is a comment.
Please don't add "thank you" as an answer. Once you have sufficient reputation, you will be able to vote up questions and answers that you found helpful. - From Review
To me, this seems like a somewhat incomplete answer. I've explained it in more detail here.
0

As others have suggested, use TailwindCSS IntelliSense. However, for hinting within special attributes or functions, custom configuration is required. By default, hints will only appear in HTML class and className attributes, but this can be extended to JavaScript functions, additional HTML attributes, or even custom regex patterns.

Specifically, for displaying hints when passing parameters in functions, up until v0.14.9, could only reference them using regex by classRegex. However, from v0.14.10 onward, this was extended with a simpler classFunctions setting.

Specifically, for the twrnc package, I would follow the settings below based on the different IntelliSense versions.

From TailwindCSS IntelliSense v0.14.10

For the TailwindCSS IntelliSense to work within the tw function added by twrnc, you need to add the function name to tailwindCSS.classFunctions, like this:

{
  "tailwindCSS.classFunctions": ["tw", "tw.style"]
}

Note: This only replaces classRegex; however, using classAttributes alongside it is still recommended.

Until TailwindCSS IntelliSense v0.14.9

However, if you want it to work in the style attribute within an HTML structure, you need to add the following alongside the default class and className:

{
  "tailwindCSS.classAttributes": [
    // ...
    "style"
  ],
}

Additionally, you can use the classRegex expression you mentioned to declare the hint to be requested when calling the function, based on twrnc's recommendation:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.