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

feat: add util to write tw classes inside strings #761

Merged
merged 8 commits into from
Nov 21, 2023

Conversation

mitjans
Copy link
Contributor

@mitjans mitjans commented Nov 15, 2023

There are documentation changes missing in this PR. I want to know your opinion on this first before continuing the development.

Description

More often than not, there are times when you may want to write Tailwind CSS classes within TypeScript strings. For example:

<script setup lang="ts">
const variant = ref('primary');

const variantClasses = {
  primary: "bg-red-400",
  secondary: "bg-green-400",
};
</script>

<template>
  <button :class="variantClasses[variant]">Click me</button>
</template>

When doing so, you lose intellisense in VSCode through TailwindCSS extension. To overcome this problem, the extension team created a setting to allow for custom class name completion contexts.

I thought it would be nice if this library helped the DX of TailwindCSS classes by providing a util function that when paired with the correct VSCode setting, it would automatically generate suggestions.

Approach

We can provide the following auto-imported function under the flag addTwUtil:

export const tw = function (tailwindClasses: TemplateStringsArray | string) {
  return tailwindClasses
}

We can also add in the documentation that users should add the following configuration in VSCode:

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

With these modifications, users of this module will now be able to write TailwindCSS classes like this:

const variantClasses = {
  primary: tw`bg-red-400`,
  secondary: tw`bg-green-400`,
};

Here's a small video showcasing the use case:

Screen.Recording.2023-11-15.at.10.23.33.mov

I feel like this could improve a lot the DX when using Tailwind CSS classes. I'm not sure if this is something you are willing to add to this module since it's very coupled to VSCode development and it requires modification of VSCode settings to work (that's why I set addTwUtil to be false by default).

@mitjans mitjans force-pushed the feat/tw-util branch 2 times, most recently from 7d8a59e to b5cc8e8 Compare November 15, 2023 09:34
@atinux
Copy link
Collaborator

atinux commented Nov 15, 2023

Looks good to me!

Happy to update the documentation to explain all this? 🙏

Copy link

nuxt-studio bot commented Nov 15, 2023

Live Preview ready!

Name Edit Preview Latest Commit
TailwindCSS Edit on Studio ↗︎ View Live Preview 3f71d4b

@mitjans
Copy link
Contributor Author

mitjans commented Nov 15, 2023

@atinux added documentation for the new module option. Please, let me know if I missed anything! 🙏

src/module.ts Outdated Show resolved Hide resolved
@atinux atinux merged commit 0653b2d into nuxt-modules:main Nov 21, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants