Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork171
A Prettier plugin for Tailwind CSS that automatically sorts classes based on our recommended class order.
License
tailwindlabs/prettier-plugin-tailwindcss
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
APrettier v3+ plugin for Tailwind CSS v3.0+ that automatically sorts classes based onour recommended class order.
To get started, installprettier-plugin-tailwindcss
as a dev-dependency:
npm install -D prettier prettier-plugin-tailwindcss
Then add the plugin to yourPrettier configuration:
// .prettierrc{"plugins":["prettier-plugin-tailwindcss"]}
When using a JavaScript config, you can import the types for IntelliSense:
// prettier.config.js/**@type {import('prettier').Config & import('prettier-plugin-tailwindcss').PluginOptions} */exportdefault{plugins:["prettier-plugin-tailwindcss"],}
As of v0.5.x, this plugin now requires Prettier v3 and is ESM-only. This means it cannot be loaded viarequire()
. For more information see ourupgrade guide.
When using Tailwind CSS v4 you must specify your CSS file entry point, which includes your theme, custom utilities, and other Tailwind configuration options. To do this, use thetailwindStylesheet
option in your Prettier configuration.
Note that paths are resolved relative to the Prettier configuration file.
// .prettierrc{"tailwindStylesheet":"./resources/css/app.css"}
To ensure that the class sorting takes into consideration any of your project's Tailwind customizations, it needs access to yourTailwind configuration file (tailwind.config.js
).
By default the plugin will look for this file in the same directory as your Prettier configuration file. However, if your Tailwind configuration is somewhere else, you can specify this using thetailwindConfig
option in your Prettier configuration.
Note that paths are resolved relative to the Prettier configuration file.
// .prettierrc{"tailwindConfig":"./styles/tailwind.config.js"}
If a local configuration file cannot be found the plugin will fallback to the default Tailwind configuration.
By default this plugin sorts classes in theclass
attribute, any framework-specific equivalents likeclassName
,:class
,[ngClass]
, and any Tailwind@apply
directives.
You can sort additional attributes using thetailwindAttributes
option, which takes an array of attribute names:
// .prettierrc{"tailwindAttributes":["myClassList"]}
With this configuration, any classes found in themyClassList
attribute will be sorted:
functionMyButton({ children}){return(<buttonmyClassList="rounded bg-blue-500 px-4 py-2 text-base text-white">{children}</button>);}
In addition to sorting classes in attributes, you can also sort classes in strings provided to function calls. This is useful when working with libraries likeclsx orcva.
You can sort classes in function calls using thetailwindFunctions
option, which takes a list of function names:
// .prettierrc{"tailwindFunctions":["clsx"]}
With this configuration, any classes inclsx()
function calls will be sorted:
importclsxfrom'clsx'functionMyButton({ isHovering, children}){letclasses=clsx('rounded bg-blue-500 px-4 py-2 text-base text-white',{'bg-blue-700 text-gray-100':isHovering,},)return(<buttonclassName={classes}>{children}</button>)}
This plugin also enables sorting of classes in tagged template literals.
You can sort classes in template literals using thetailwindFunctions
option, which takes a list of function names:
// .prettierrc{"tailwindFunctions":["tw"],}
With this configuration, any classes in template literals tagged withtw
will automatically be sorted:
import{View,Text}from'react-native'importtwfrom'twrnc'functionMyScreen(){return(<Viewstyle={tw`bg-white p-4 dark:bg-black`}><Textstyle={tw`text-md text-black dark:text-white`}>Hello World</Text></View>)}
This feature can be used with third-party libraries liketwrnc
or you can create your own tagged template by defining this "identity" function:
consttw=(strings, ...values)=>String.raw({raw:strings}, ...values)
Once added, tag your strings with the function and the plugin will sort them:
constmySortedClasses=tw`bg-white p-4 dark:bg-black`
This plugin automatically removes unnecessary whitespace between classes to ensure consistent formatting. If you prefer to preserve whitespace, you can use thetailwindPreserveWhitespace
option:
// .prettierrc{"tailwindPreserveWhitespace":true,}
With this configuration, any whitespace surrounding classes will be preserved:
importclsxfrom'clsx'functionMyButton({ isHovering, children}){return(<buttonclassName=" rounded bg-blue-500 px-4 py-2 text-base text-white ">{children}</button>)}
This plugin automatically removes duplicate classes from your class lists. However, this can cause issues in some templating languages, like Fluid or Blade, where we can't distinguish between classes and the templating syntax.
If removing duplicate classes is causing issues in your project, you can use thetailwindPreserveDuplicates
option to disable this behavior:
// .prettierrc{"tailwindPreserveDuplicates":true,}
With this configuration, anything we perceive as duplicate classes will be preserved:
<divclass=" {f:if(condition: isCompact, then: 'grid-cols-3', else: 'grid-cols-5')} {f:if(condition: isDark, then: 'bg-black/50', else: 'bg-white/50')} grid gap-4 p-4"></div>
This plugin uses Prettier APIs that can only be used by one plugin at a time, making it incompatible with other Prettier plugins implemented the same way. To solve this we've added explicit per-plugin workarounds that enable compatibility with the following Prettier plugins:
@ianvs/prettier-plugin-sort-imports
@prettier/plugin-pug
@shopify/prettier-plugin-liquid
@trivago/prettier-plugin-sort-imports
prettier-plugin-astro
prettier-plugin-css-order
prettier-plugin-jsdoc
prettier-plugin-multiline-arrays
prettier-plugin-organize-attributes
prettier-plugin-organize-imports
prettier-plugin-svelte
prettier-plugin-sort-imports
One limitation with this approach is thatprettier-plugin-tailwindcss
must be loaded last.
// .prettierrc{// .."plugins":["prettier-plugin-svelte","prettier-plugin-organize-imports","prettier-plugin-tailwindcss"// MUST come last]}
About
A Prettier plugin for Tailwind CSS that automatically sorts classes based on our recommended class order.
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.