Created
August 10, 2024 06:21
-
-
Save itsjavi/41e11befb3e6df6c5a15f961c865bb6a to your computer and use it in GitHub Desktop.
Better TailwindCSS class completion in VSCode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"tailwindCSS.experimental.classRegex": [ | |
"[cC]lass\\: '([^']*)'", | |
"[cC]lassName\\: '([^']*)'", | |
"tw`([^`]*)" | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires the
TailwindCSS
extension for VSCode, and to have.vscode/settings.json
.Example usage:
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.