Skip to content

Instantly share code, notes, and snippets.

@itsjavi
Created August 10, 2024 06:21
Show Gist options
  • Save itsjavi/41e11befb3e6df6c5a15f961c865bb6a to your computer and use it in GitHub Desktop.
Save itsjavi/41e11befb3e6df6c5a15f961c865bb6a to your computer and use it in GitHub Desktop.
Better TailwindCSS class completion in VSCode
{
"tailwindCSS.experimental.classRegex": [
"[cC]lass\\: '([^']*)'",
"[cC]lassName\\: '([^']*)'",
"tw`([^`]*)"
]
}
import { type ClassValue, clsx } from 'clsx'
import { twMerge } from 'tailwind-merge'
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
/**
* @example
* ```ts
* const panelStyles = tw`flex gap-4 p-8 rounded-lg bg-blue-600 text-white`
* ```
*/
export function tw(strings: TemplateStringsArray, ...params: Array<string | number | undefined | null>): string {
let str = ''
strings.forEach((string, i) => {
str += string + (params[i] ?? '')
})
return cn(str)
}
@itsjavi
Copy link
Author

itsjavi commented Aug 10, 2024

Requires the TailwindCSS extension for VSCode, and to have .vscode/settings.json.

Example usage:

const panelStyles = tw`flex gap-4 p-8 rounded-lg bg-blue-600 text-white`

With the tw tagged template function, and the provided settings you will get Tailwind classname completion no matter where you assign it to, compared to plain strings (which are harder to detect for the plugin). The caveat is that you need to import it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment