Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typed classnames for autocompletion #15

Closed
ecklf opened this issue May 19, 2020 · 21 comments
Closed

Typed classnames for autocompletion #15

ecklf opened this issue May 19, 2020 · 21 comments
Labels
enhancement New feature or request

Comments

@ecklf
Copy link

ecklf commented May 19, 2020

I like this API the most from the available Tailwind / RN projects but it's a bit sad to lose the ability for auto-completion.

What about going from this:

tailwind('bg-blue-200 px-3 py-1 rounded-full')

to something like this:

tailwind('bg-blue-200', 'px-3', 'py-1', 'rounded-full')
@matepapp
Copy link

I have an idea of how to support a typed API, but we have to migrate to TypeScript in that case. From TypeScript 2.9 you can import JSON modules in TS files with full typed support. We can create a tailwind() method where we expose the keys of the JSON and get amazing autocompletion and type-safety ✨

Screenshot 2020-05-19 at 16 39 32

Screenshot 2020-05-19 at 16 39 57

@ecklf
Copy link
Author

ecklf commented May 19, 2020

@matepapp nice find!

However, I can't think of a sensible TypeScript solution that would not require the API change I mentioned above. Your current implementation would only allow one className in the tailwind function unless you generate all combinations of className strings.

My proposal would look something like this:

import JSON from "./styles.json";
type Keys = keyof typeof JSON;

const tailwind = (...props: Keys[]) => {
  return props.reduce(
    (styles, curr) => ({
      ...styles,
      ...JSON[curr],
    }),
    {}
  );
}

image

@matepapp
Copy link

Yes, this was only just a quick example to demonstrate the possibility of type-safety. Your solution is definitely a step ahead, but I think we should support regular string type as well to keep the API backward compatible. Switching based on the type could be a temporary workaround. What do you think?

I might can create a draft pull request next week if it’s okay for you @vadimdemedes

@ecklf
Copy link
Author

ecklf commented May 19, 2020

Well we could either add an additional function to the core or add readme instructions on how to achieve this with a custom tailwind function (would be neat if useVariables would get exported then). I think the opt-in instruction might be the better choice since it wouldn't force the codebase to TypeScript.

@vadimdemedes
Copy link
Owner

@matepapp Yeah, I think something like that would work. Would be still great to preserve support for specifying multiple classes in one string for people that don't care about autocomplete (like me).

The only thing I'm wondering about is how will it work with custom configs?

@vadimdemedes vadimdemedes added the enhancement New feature or request label Jun 5, 2020
@vadimdemedes vadimdemedes changed the title Idea: Typed classnames for autocompletion Typed classnames for autocompletion Jun 5, 2020
@tommulkins
Copy link

@vadimdemedes You may be interested in following this Tailwind Intellisense issue (https://github.com/tailwindcss/intellisense/issues/129) in regards to this.

@vadimdemedes
Copy link
Owner

@tommulkins Thanks for linking it here!

@leggomuhgreggo
Copy link

I think this might have just become more feasible with the improvements in TS 4.1

https://twitter.com/diegohaz/status/1309489079378219009

@gitstud
Copy link

gitstud commented Dec 21, 2020

Is there a PR on the horizon for this?

@ecklf
Copy link
Author

ecklf commented Dec 30, 2020

There is now a way to achieve this with the official VSCode extension.
https://twitter.com/ecklflorentin/status/1344242188574720000

@jonlambert
Copy link

For those who want a quick copy and paste:

"tailwindCSS.experimental.classRegex": ["tailwind\\('|\"([^'|\")]*)"]

^ add to your VS Code settings.json file. I've adjusted the regex to pick up single or double quotes.

@tommulkins
Copy link

To make intellisense work in a tailwind-rn project, i.e. style={tailwind('mb-1')} I use the following:

  /* TailwindCSS */
  "tailwindCSS.experimental.classRegex": ["tailwind\\('([^)]*)\\')", "'([^']*)'"]

@ecklf
Copy link
Author

ecklf commented May 26, 2021

I think we can close this issue 👍🏻

@ecklf ecklf closed this as completed May 26, 2021
@lyzMaster
Copy link

I must run npm install tailwindcss to have Tailwind CSS IntelliSense autocomplete feature, can I ignore this step?

@ecklf
Copy link
Author

ecklf commented Jun 23, 2021

I must run npm install tailwindcss to have Tailwind CSS IntelliSense autocomplete feature, can I ignore this step?

Afaik you only need a tailwind.config.js in your project root for IntelliSense to work.

@tommulkins
Copy link

I must run npm install tailwindcss to have Tailwind CSS IntelliSense autocomplete feature, can I ignore this step?

You can run npx tailwindcss init instead to make your tailwind.config.js file.

@lyzMaster
Copy link

I must run npm install tailwindcss to have Tailwind CSS IntelliSense autocomplete feature, can I ignore this step?

You can run npx tailwindcss init instead to make your tailwind.config.js file.

Before this, I have run npx tailwindcss init and so I got tailwind.config.js, but IntelliSense still not work and I got this error in the OUTPUT of vscode(I tried restarting vscode):
截屏2021-06-24 上午11 58 26

And I check out doc of Tailwindcss IntelliSense, which says: In order for the extension to activate you must have tailwindcss installed and a Tailwind config file named tailwind.config.js in your workspace.. So I try npm install tailwindcss, and after restarting vscode, autocomplete works. So I guess maybe IntelliSense check package.json -> dependencies, if "tailwindcss" in dependencies, IntelliSense works?

@ecklf
Copy link
Author

ecklf commented Jun 24, 2021

Seems you need to have it installed then. Note that the autocomplete will adhere to your tailwind configuration and not to tailwind-rn's compatibility limitations. You can disable core plugins for this.

@harshjadon9
Copy link

To make intellisense work in a tailwind-rn project, i.e. style={tailwind('mb-1')} I use the following:

  /* TailwindCSS */
  "tailwindCSS.experimental.classRegex": ["tailwind\\('([^)]*)\\')", "'([^']*)'"]

this isn't working for me...

i have pasted the above in setting.json

My tailwind.js

import { create } from 'tailwind-rn';
import styles from '../styles.json';

const { tailwind, getColor } = create(styles);
export { tailwind, getColor };

My home.js

...
import { getColor, tailwind } from '../tailwind';

const home = () =>{
    return(
        <View>
            <Text>hi there</Text>
            <Text style={tailwind('mb-1')}>hi there</Text>
        </View>
    )
}

export default home

@tommulkins , @jonlambert or anyone could please resolve this

@tommulkins
Copy link

To make intellisense work in a tailwind-rn project, i.e. style={tailwind('mb-1')} I use the following:

  /* TailwindCSS */
  "tailwindCSS.experimental.classRegex": ["tailwind\\('([^)]*)\\')", "'([^']*)'"]

this isn't working for me...

i have pasted the above in setting.json

My tailwind.js

import { create } from 'tailwind-rn';
import styles from '../styles.json';

const { tailwind, getColor } = create(styles);
export { tailwind, getColor };

My home.js

...
import { getColor, tailwind } from '../tailwind';

const home = () =>{
    return(
        <View>
            <Text>hi there</Text>
            <Text style={tailwind('mb-1')}>hi there</Text>
        </View>
    )
}

export default home

@tommulkins , @jonlambert or anyone could please resolve this

I currently use tailwind-react-native-classnames with the following regex settings in my project:

vscode/settings.json

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

I believe that last value in the array will do the trick, "'([^']*)'"

@karimessouabni
Copy link

karimessouabni commented Mar 9, 2023

You need to open the VS Code setting file as a json. It should be under this Path (~/Library/Application Support/Code/User/settings.json)

Then if you're using "import tw from 'twrnc';" and your code looks like style={tw'min-w-full '}

update the section of tailwind on your setting file as follows :

"tailwindCSS.classAttributes": [ "class", "className", "ngClass" ], "tailwindCSS.experimental.classRegex": ["style={tw\\('|\"([^'|\")]*)"], "tailwindCSS.experimental.configFile": null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

10 participants