59

I'm using the tailwind CSS intellisense vscode extension, and it seems to only work when typing inside a className property.

I'm using a package called cntl https://www.npmjs.com/package/cntl to help write more maintainable classNames, however, using this package, I lose the intelliSense support.

Is there any way to manually configure so that I get the intelliSense when writing my cntl?

const title = cntl`
  text-3xl
  // I'd like intellisense here
`
2

12 Answers 12

59

I realize this Q is old, but it still shows up in search so I wanted to share my workflow :)

Here's my VS Code settings.json to add Tailwind IntelliSense within objects and variables who's name ends with "Classes":

  "tailwindCSS.experimental.classRegex": [
    ["Classes \\=([^;]*);", "'([^']*)'"],
    ["Classes \\=([^;]*);", "\"([^\"]*)\""],
    ["Classes \\=([^;]*);", "\\`([^\\`]*)\\`"]
  ],

Tailwind IntelliSense will now recognize all of the following strings:

const defaultClasses = `text-grey`;

const componentClasses = {
  default: 'text-grey',
  danger: `text-red`,
  warning: "text-yellow",
};

Note: the regex matches code blocks that start with Classes = and ends with ; — you can replace Classes = with your own matcher, like. cntl :)

Sign up to request clarification or add additional context in comments.

8 Comments

great, even you can get rid of the semi if you're not using it, "tailwindCSS.experimental.classRegex": [ ["ClassNames \\=([^]*)", "'([^']*)'"], ["ClassNames \\=([^]*)","\"([^\"]*)\""], ["ClassNames \\=([^]*)","\`([^\`]*)\`"] ],
@M.Ubeyd it's a good idea to use the semi-colon, or some identifier to end the regex match. The pattern you shared — ([^]*) — will basically match anything so it will match until the end of the file. That might slow down your IDE as your project grows.
Thanks, that's a nice tip, I didn't any slowness since my codebase was small, but absolutely it will start slowing on bigger codebases.
This one worked for me... I modified the regex like this ["classNames=([^;]*);", "[\"'`]([^\"'`]*).*?[\"'`]"]
Thanks @ztrat4dkyle! I updated this slightly to work with typescript e.g. declarations that include a type like const fooClasses: Bar = {first: 'pt-2'}. Just replace Classes \\ with Classes.*\\
|
41

None of the answer worked for me. But it workes based on the guide from https://www.jussivirtanen.fi/writing/using-tailwind-intellisense-within-strings

If you're using VS Code and writing your Tailwind classes within a variable, you've probably noticed that Tailwind IntelliSense doesn't work. However, if you're using a consistent naming scheme, you can fix this issue.

I use a consistent naming scheme with the word Styles at the end. In this case, I can go to Tailwind IntelliSense settings and add .*Styles to the tailwindCSS.classAttributes array:

// settings.json within VS Code
{
  // Add ".*Styles" (or whatever matches your naming scheme)
  "tailwindCSS.classAttributes": ["class", "className", "ngClass", ".*Styles"]
}

example usage

  const contentStyles = "py-1 bg-white dark:bg-gray-700"

4 Comments

One small alteration that I found useful: add a .* at end of your custom item. For me, this looks like this: "tailwindCSS.classAttributes": [..., ".*Class.*", ".*Classes.*"]. That has allowed me to get intellisense in a variety of names in my components such as somethingClasses = { ... }, somethingClassesProp = { ...} , etc.
This worked for me "tailwindCSS.classAttributes": ["class", "className", "ngClass", ".*Styles.*"] . Suggestions show for any variable that has Styles in its name.
Don't forget to restart VS Code for the changes to be applied
By using the latest version of TailwindCSS IntelliSense, you can easily do this. See the classFunctions in my answer. For me, it works without restarting.
20

Here's how I solved it.

In VSCode settings.json add the following:

 "tailwindCSS.experimental.classRegex": [
    "cntl`([^`]*)", // cntl`...`
 ],

2 Comments

Thanks! That works great with classnames too. I just have to remove the `
By using the latest version of TailwindCSS IntelliSense, you can easily do this. See the classFunctions in my answer.
4

Linting is not supported yet as per: https://github.com/tailwindlabs/tailwindcss/issues/7553. Hover seem to be supported now though

For clsx

"tailwindCSS.experimental.classRegex": ["clsx\\(([^)]*)\\)"]

For classnames

"tailwindCSS.experimental.classRegex": ["classnames\\(([^)]*)\\)"]

2 Comments

I find that using the regex you have for classnames will fail to suggest classes until the first occurrence of whitespace in the string. This seems to be because the regex also matches the first quote character, and Tailwind thinks the quote is part of a class, and won't show any suggestions since none start with a quote. This regex avoids that: classnames\\(.([^)]*).\\) (this uses . for any char, but could be made stricter by only matching ", ' and ` itself)
Feel free to edit the answer if you tried them out! @MagnusBull
4

As others mentioned, 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.

From TailwindCSS IntelliSense v0.14.10

{
  "tailwindCSS.classFunctions": ["cntl"]
}

example

Comments

2

This will detect a container consisting of className = [] string and its variants such as ButtonClassNamesXyz Classname and whatever is inside [ ] will be processed.

image

  "tailwindCSS.experimental.classRegex": [
    ["\\S*[Cc]lass[nN]ame\\S* = \\[([\\s\\S]+?)(?=][\\s;,.])", "'([^']*)'"],
    "return '(.*)'",
  ],

enter image description here

enter image description here Adjust regex here https://www.debuggex.com/r/yhCYrsFdzXRWQEhP

v2 note

  • I have added detection for ] inside the actual classname string.

tailwindlabs / tailwindcss : [IntelliSense] Custom class name completion contexts #7554

    ["\\S*[Cc]lass[nN]ame\\S* = \\[([\\s\\S]+?)(?=][\\n;.])", "'([^']*)'"],
    ["\\S*[Cc]lass[nN]ame\\S* = {([\\s\\S]+?)}\\s", "'([^']*)'"],
    "\\S*[Cc]lass[nN]ame\\S* ?[:=] ['`](.*)['`]",
    "return '(.*)'",
    ["return \\(?{([\\s\\S]+?)}\\(?\\s", "'([^']*)'"],
    ["twMerge\\(([\\s\\S]+?)\\)\\s", "'([^']*)'"],

Comments

1

For those who want to enable tailwind IntelliSense for react props or other HTML attributes, add the following to the user settings.json.

{
"tailwindCSS.classAttributes": ["class", "className", "ngClass", "extra", "someOtherAttribute"]
}

Comments

1

This question is quite old and by now even the most liked answer is old, but this still comes up first on google search.

@ztrat4dkyle answer relies on your formatting using semis and doesn't allow for typescript types between the name and = {}.

After looking at the implementation in the tailwind plugin

I came up with the following regex:

"tailwindCSS.experimental.classRegex": [
  [
    "(Classes.*=\\s*\\{(?:[^{}]*|\\{[^{}]*\\})*\\})",
    "[\"'`]([^\"'`]*)[\"'`]"
  ],
  "Classes.*=\\s*[\"'`]([^\"'`]*)[\"'`]"
],

You can replace Classes to what ever identifier you are using. The first array element matches Classes: type (or not) = {} (and supports nested objects). The next line matches basic strings Classe = "" (supports ", ' and `)

Here is an example of what that might look like in a svelte file

enter image description here

2 Comments

This was the most helpful regex to me. I combined it with the tw identity function for template literals as suggested by prettier-plugin-tailwindcss and added (?:tw)? immediately before both [\"'`] in the two class regexes. This has given me sorting and intellisense in variables by defining them like const baseClasses = tw`text-red-200 bg-white`;.
Worth noting that after adding the regex setting, I had to do "Developer: Reload Window" in VS Code a couple of times to get it working.
1

cva docs

As I was stuck on this problem, I write it here maybe it helps someone.

If you are using a profile in your VS Code, you should add it in your current profile setting.json.

To open current profile setting.json use the VS Code command (open command with F1):

>preferences:open User Settings (JSON)

then type in this:

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

1 Comment

Worth noting that after adding the regex setting, I had to do "Developer: Reload Window" in VS Code a couple of times to get it working.
0

I understand this question has been answered, but I was still facing some trouble because I wanted to use Tailwind's intellisense with react's classnames library.

Here's what worked for me after adding it to VSC's settings.json:

"tailwindCSS.experimental.classRegex": ["classNames\\(([^)]*)\\)"],

Comments

0

I know it has nothing to do with it, but I'll leave this answer here for anyone who needs it because I couldn't find it anywhere and I managed to do it! This configuration works for both classList and className in pure javascript! Just put in the user's settings.json file in VSCode (Ctrl + Shift + P and search User Settings)

    "tailwindCSS.includeLanguages": {
        "javascript": "javascript"
    },
    "tailwindCSS.experimental.classRegex": [

        ["classList\\.add\\(([^)]*)", "['\"`]([^'\"`\\s]*)['\"`]"],
        ["classList\\.contains\\(([^)]*)", "['\"`]([^'\"`\\s]*)['\"`]"],
        ["classList\\.entries\\(([^)]*)", "['\"`]([^'\"`\\s]*)['\"`]"],
        ["classList\\.forEach\\(([^)]*)", "['\"`]([^'\"`\\s]*)['\"`]"],
        ["classList\\.item\\(([^)]*)", "['\"`]([^'\"`\\s]*)['\"`]"],
        ["classList\\.keys\\(([^)]*)", "['\"`]([^'\"`\\s]*)['\"`]"],
        ["classList\\.length\\(([^)]*)", "['\"`]([^'\"`\\s]*)['\"`]"],
        ["classList\\.remove\\(([^)]*)", "['\"`]([^'\"`\\s]*)['\"`]"],
        ["classList\\.replace\\(([^)]*)", "['\"`]([^'\"`\\s]*)['\"`]"],
        ["classList\\.supports\\(([^)]*)", "['\"`]([^'\"`\\s]*)['\"`]"],
        ["classList\\.toggle\\(([^)]*)", "['\"`]([^'\"`\\s]*)['\"`]"],
        ["classList\\.value\\(([^)]*)", "['\"`]([^'\"`\\s]*)['\"`]"],
        ["classList\\.values\\(([^)]*)", "['\"`]([^'\"`\\s]*)['\"`]"],
        ["className\\s*=\\s*['\"`]([^'\"`]*)['\"`]"]
    ],
    "editor.quickSuggestions": {
        "strings": true
    },
    "editor.inlineSuggest.enabled": true

It works for all classList methods and also works with the className, you can use ", ' or ` which will work!

2 Comments

I bet you could just do (add|contains|entries|forEach|item|...) or even clasList\.[a-zA-Z]+\(.
I tried, but it didn't work! It was my first attempt to get there!
0

Go to your tailwind css extension on vscode and you'll find this

enter image description here

then add a name you want and restart vscode.

1 Comment

The names are dynamic. It won't make sense to put them in a static list.

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.