![Introducing Dashboard Analytics](https://cdn.sanity.io/images/cgdhsj6q/production/af327c5bd94708db501516a75da6870aef894c64-2286x1522.png?w=400&fit=max&auto=format)
Product
Introducing Dashboard Analytics
We're introducing a new Analytics feature in the Socket dashboard so you can view changes in your organization's and repositories' alerts over time.
prettier-plugin-tailwindcss
Advanced tools
The prettier-plugin-tailwindcss npm package is a plugin for Prettier, the code formatter, that sorts Tailwind CSS classes automatically. It ensures that your Tailwind CSS classes are ordered consistently according to the recommended order from the Tailwind CSS documentation. This can help improve readability and maintainability of your code.
Sorting Tailwind CSS classes
This plugin will reorder the classes to follow the default Tailwind CSS order, for example, positioning classes like 'flex' and 'justify-center' would come before display and box model classes like 'p-4' and 'bg-red-500'.
"<div class='bg-red-500 p-4 flex justify-center'>Hello World</div>"
Headwind is a Visual Studio Code extension that also sorts Tailwind CSS classes automatically. It is similar to prettier-plugin-tailwindcss but is specifically designed for VS Code and does not require Prettier.
This package is a typed utility function to combine Tailwind CSS classes. While it does not sort classes, it provides a way to conditionally join class names together, which can be useful in conjunction with a sorting tool.
tailwind-merge is a utility to merge Tailwind CSS classes without duplicates and with optimized class precedence. It does not sort classes but ensures that the final class string is optimized for Tailwind CSS.
A Prettier v3+ plugin for Tailwind CSS v3.0+ that automatically sorts classes based on our recommended class order.
To get started, install prettier-plugin-tailwindcss
as a dev-dependency:
npm install -D prettier prettier-plugin-tailwindcss
Then add the plugin to your Prettier config:
// prettier.config.js
module.exports = {
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 via require()
. For more information see our upgrade guide.
To ensure that the class sorting takes into consideration any of your project's Tailwind customizations, it needs access to your Tailwind 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 the tailwindConfig
option in your Prettier configuration.
Note that paths are resolved relative to the Prettier configuration file.
// prettier.config.js
module.exports = {
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 only sorts classes in the class
attribute as well as any framework-specific equivalents like class
, className
, :class
, [ngClass]
, etc.
You can sort additional attributes using the tailwindAttributes
option, which takes an array of attribute names:
// prettier.config.js
module.exports = {
tailwindAttributes: ['myClassList'],
}
With this configuration, any classes found in the myClassList
attribute will be sorted:
function MyButton({ children }) {
return (
<button myClassList="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 like clsx or cva.
You can sort classes in function calls using the tailwindFunctions
option, which takes a list of function names:
// prettier.config.js
module.exports = {
tailwindFunctions: ['clsx'],
}
With this configuration, any classes in clsx()
function calls will be sorted:
import clsx from 'clsx'
function MyButton({ isHovering, children }) {
let classes = clsx(
'rounded bg-blue-500 px-4 py-2 text-base text-white',
{
'bg-blue-700 text-gray-100': isHovering,
},
)
return (
<button className={classes}>
{children}
</button>
)
}
This plugin also enables sorting of classes in tagged template literals.
You can sort classes in template literals using the tailwindFunctions
option, which takes a list of function names:
// prettier.config.js
module.exports = {
tailwindFunctions: ['tw'],
}
With this configuration, any classes in template literals tagged with tw
will automatically be sorted:
import { View, Text } from 'react-native'
import tw from 'twrnc'
function MyScreen() {
return (
<View style={tw`bg-white p-4 dark:bg-black`}>
<Text style={tw`text-md text-black dark:text-white`}>Hello World</Text>
</View>
)
}
This feature can be used with third-party libraries like twrnc
or you can create your own tagged template by defining this "identity" function:
const tw = (strings, ...values) => String.raw({ raw: strings }, ...values)
Once added, tag your strings with the function and the plugin will sort them:
const mySortedClasses = tw`bg-white p-4 dark:bg-black`
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-import-sort
prettier-plugin-jsdoc
prettier-plugin-organize-attributes
prettier-plugin-organize-imports
prettier-plugin-style-order
prettier-plugin-svelte
One limitation with this approach is that prettier-plugin-tailwindcss
must be loaded last.
// .prettierrc
{
// ..
"plugins": [
"prettier-plugin-svelte",
"prettier-plugin-organize-imports",
"prettier-plugin-tailwindcss" // MUST come last
]
}
FAQs
A Prettier plugin for sorting Tailwind CSS classes.
The npm package prettier-plugin-tailwindcss receives a total of 1,651,218 weekly downloads. As such, prettier-plugin-tailwindcss popularity was classified as popular.
We found that prettier-plugin-tailwindcss demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
We're introducing a new Analytics feature in the Socket dashboard so you can view changes in your organization's and repositories' alerts over time.
Security News
A new OpenSSF report uncovers critical gaps in secure software training, with 75% of new developers unfamiliar with secure practices, highlighting urgent educational needs.
Security News
The 2023 Python Developers Survey reveals key trends in packaging, web frameworks, and developer demographics, highlighting a shift toward innovative tools as the Python community diversifies and grows among less experienced developers.