A Prettier plugin for sorting Tailwind CSS classes based on the recommended class order
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
The plugin integrates with Prettier's parser system to provide Tailwind class sorting across multiple file formats and languages. Users configure the plugin through Prettier settings, and the plugin automatically handles class sorting in supported file types.
The plugin exports parsers that extend Prettier's built-in parsers with Tailwind class sorting capabilities.
// Plugin exports (used internally by Prettier)
const parsers: Record<string, Parser>;
const printers: Record<string, Printer>;The plugin automatically sorts Tailwind classes in the following file types when configured in Prettier:
.html) - Standard HTML documents.html) - Angular component templates.vue) - Vue.js single file components.svelte) - Svelte components.astro) - Astro components.hbs) - Ember.js Handlebars templates.css) - Standard CSS files with @apply directives.scss) - Sass stylesheets.less) - Less stylesheets.js, .mjs) - JavaScript files with JSX.ts, .tsx) - TypeScript filesclassName attributesThe plugin sorts classes in different attributes based on the file format:
// HTML-based formats
class="..." // HTML, Angular, Vue
className="..." // React/JSX
// Framework-specific
[class]="..." // Angular property binding
:class="..." // Vue.js binding
v-bind:class="..." // Vue.js binding (full syntax)// Angular
[ngClass]="expression"
// Vue.js
:class="expression"
v-bind:class="expression"
// Astro
class:list="expression"When configured with tailwindFunctions, the plugin sorts classes inside function calls:
// Example with clsx function
clsx("px-4 bg-blue-500 text-white py-2")
// Becomes: clsx("bg-blue-500 px-4 py-2 text-white")
// Template literals
tw`px-4 bg-blue-500 text-white py-2`
// Becomes: tw`bg-blue-500 px-4 py-2 text-white`The plugin sorts Tailwind utilities in CSS @apply directives:
.my-component {
@apply px-4 bg-blue-500 text-white py-2;
}
/* Becomes: */
.my-component {
@apply bg-blue-500 px-4 py-2 text-white;
}Add custom attributes to sort with the tailwindAttributes option:
{
"tailwindAttributes": ["myClass", "customClasses"]
}This enables sorting in custom attributes:
<div myClass="px-4 bg-blue-500 text-white py-2">Content</div>Add custom function names with the tailwindFunctions option:
{
"tailwindFunctions": ["clsx", "cn", "classNames"]
}This enables sorting inside specified function calls:
const classes = cn("px-4 bg-blue-500 text-white py-2");class attributes in templates[ngClass] directive expressionsclass attributes in templates:class and v-bind:class expressionsclass and className attributesclass attributes in components!important flags in @apply directivesInstall with Tessl CLI
npx tessl i tessl/npm-prettier-plugin-tailwindcss