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
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/content/1.getting-started/2.options.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,26 @@ export default defineNuxtConfig({
}
})
```

## `addTwUtil `

- Default: `false`

This option adds the utility function `tw` that will provide IntelliSense suggestions in JS/TS strings if the [Tailwind CSS IntelliSense](https://marketplace.visualstudio.com/items?itemName=bradlc.vscode-tailwindcss) extension is installed. For this to work, you will have to add the following VSCode setting:

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

Once added, the new utility function can be used as follows, providing IntelliSense suggestions when writing Tailwind classes:

```vue [index.vue]
<script setup lang="ts">
const variantClasses = {
primary: tw`bg-red-400`,
secondary: tw('bg-green-400')
}
</script>
```
12 changes: 10 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {
resolvePath,
addVitePlugin,
useNuxt,
addTemplate
addTemplate,
addImportsDir,
createResolver
} from '@nuxt/kit'

// @ts-expect-error
Expand Down Expand Up @@ -45,12 +47,14 @@ const defaults = (nuxt = useNuxt()): ModuleOptions => ({
exposeConfig: false,
exposeLevel: 2,
injectPosition: 'first',
disableHmrHotfix: false
disableHmrHotfix: false,
addTwUtil: false,
})

export default defineNuxtModule<ModuleOptions>({
meta: { name, version, configKey, compatibility }, defaults,
async setup (moduleOptions, nuxt) {
const { resolve } = createResolver(import.meta.url);
const [configPaths, contentPaths] = await resolveModulePaths(moduleOptions.configPath, nuxt)

const tailwindConfig = await Promise.all((
Expand Down Expand Up @@ -133,6 +137,10 @@ export default defineNuxtModule<ModuleOptions>({
await installModule('@nuxt/postcss8')
}

if(moduleOptions.addTwUtil) {
atinux marked this conversation as resolved.
Show resolved Hide resolved
addImportsDir(resolve('./runtime/utils'));
}

// enabled only in development
if (nuxt.options.dev) {
// Watch the Tailwind config file to restart the server
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/utils/tw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const tw = function (tailwindClasses: TemplateStringsArray | string) {
return tailwindClasses
}
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,10 @@ export interface ModuleOptions {
* @default false
*/
disableHmrHotfix: boolean;
/**
* Add util to write Tailwind CSS classes inside strings with `` tw`{classes}` ``
*
* @default false
*/
addTwUtil: boolean;
}
Loading