prettier-plugin-tailwindcss
A Prettier plugin for sorting Tailwind CSS classes.
Last updated 7 months ago by thecrypticace .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install prettier-plugin-tailwindcss 
SYNC missed versions from official npm registry.
prettier-plugin-tailwindcss

A Prettier v3+ plugin for Tailwind CSS v3.0+ that automatically sorts classes based on our recommended class order.

Installation

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 configuration:

// .prettierrc
{
  "plugins": ["prettier-plugin-tailwindcss"]
}

Upgrading to v0.5.x

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.

Options

Customizing your Tailwind config path

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.

// .prettierrc
{
  "tailwindConfig": "./styles/tailwind.config.js"
}

If a local configuration file cannot be found the plugin will fallback to the default Tailwind configuration.

Sorting non-standard attributes

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:

// .prettierrc
{
  "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>
  );
}

Sorting classes in function calls

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:

// .prettierrc
{
  "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>
  )
}

Sorting classes in template literals

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:

// .prettierrc
{
  "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`

Preserving whitespace

This plugin automatically removes unnecessary whitespace between classes to ensure consistent formatting. If you prefer to preserve whitespace, you can use the tailwindPreserveWhitespace option:

// .prettierrc
{
  "tailwindPreserveWhitespace": true,
}

With this configuration, any whitespace surrounding classes will be preserved:

import clsx from 'clsx'

function MyButton({ isHovering, children }) {
  return (
    <button className=" rounded  bg-blue-500 px-4  py-2     text-base text-white ">
      {children}
    </button>
  )
}

Preserving duplicate classes

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 the tailwindPreserveDuplicates option to disable this behavior:

// .prettierrc
{
  "tailwindPreserveDuplicates": true,
}

With this configuration, anything we perceive as duplicate classes will be preserved:

<div
  class="
    {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>

Compatibility with other Prettier plugins

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
  • prettier-plugin-sort-imports

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
  ]
}

Current Tags

  • 0.2.0-beta.3                                ...           beta (2 years ago)
  • 0.0.0-insiders.90b0253                                ...           insiders (17 days ago)
  • 0.6.9                                ...           latest (a month ago)

184 Versions

  • 0.0.0-insiders.90b0253                                ...           17 days ago
  • 0.6.9                                ...           a month ago
  • 0.0.0-insiders.cdbc764                                ...           a month ago
  • 0.0.0-insiders.962e4ed                                ...           a month ago
  • 0.0.0-insiders.9e58db2                                ...           a month ago
  • 0.0.0-insiders.9ce7d37                                ...           a month ago
  • 0.0.0-insiders.26a4754                                ...           a month ago
  • 0.6.8                                ...           3 months ago
  • 0.0.0-insiders.64373cf                                ...           3 months ago
  • 0.0.0-insiders.081b4f5                                ...           3 months ago
  • 0.0.0-insiders.d4f80ab                                ...           3 months ago
  • 0.6.7                                ...           3 months ago
  • 0.0.0-insiders.b1e25fd                                ...           3 months ago
  • 0.0.0-insiders.385e7c2                                ...           3 months ago
  • 0.0.0-insiders.cdb3b98                                ...           3 months ago
  • 0.0.0-insiders.15e8009                                ...           4 months ago
  • 0.0.0-insiders.9844623                                ...           4 months ago
  • 0.6.6                                ...           5 months ago
  • 0.0.0-insiders.0368ffb                                ...           5 months ago
  • 0.0.0-insiders.b7830c6                                ...           5 months ago
  • 0.0.0-insiders.ee50b9b                                ...           6 months ago
  • 0.0.0-insiders.f69f739                                ...           6 months ago
  • 0.0.0-insiders.c0c5040                                ...           6 months ago
  • 0.0.0-insiders.c5eed72                                ...           6 months ago
  • 0.0.0-insiders.f101793                                ...           6 months ago
  • 0.0.0-insiders.031e5f1                                ...           6 months ago
  • 0.6.5                                ...           6 months ago
  • 0.0.0-insiders.efea6f9                                ...           6 months ago
  • 0.0.0-insiders.642a97c                                ...           6 months ago
  • 0.0.0-insiders.a5506a9                                ...           6 months ago
  • 0.0.0-insiders.ec92b76                                ...           6 months ago
  • 0.6.4                                ...           6 months ago
  • 0.0.0-insiders.2a9d702                                ...           6 months ago
  • 0.0.0-insiders.a13021c                                ...           6 months ago
  • 0.0.0-insiders.0ea2dc0                                ...           6 months ago
  • 0.0.0-insiders.9b9bfe4                                ...           6 months ago
  • 0.6.3                                ...           6 months ago
  • 0.0.0-insiders.bd3d5b0                                ...           6 months ago
  • 0.0.0-insiders.1010b51                                ...           6 months ago
  • 0.0.0-insiders.b047e02                                ...           6 months ago
  • 0.0.0-insiders.6ded534                                ...           6 months ago
  • 0.6.2                                ...           7 months ago
  • 0.0.0-insiders.aa6ec0a                                ...           7 months ago
  • 0.0.0-insiders.57afde1                                ...           7 months ago
  • 0.0.0-insiders.82ea71a                                ...           7 months ago
  • 0.6.1                                ...           7 months ago
  • 0.0.0-insiders.731ae22                                ...           7 months ago
  • 0.0.0-insiders.8a9094f                                ...           7 months ago
  • 0.6.0                                ...           7 months ago
  • 0.0.0-insiders.afbc487                                ...           7 months ago
  • 0.0.0-insiders.1f83aae                                ...           7 months ago
  • 0.5.14                                ...           8 months ago
  • 0.0.0-insiders.3c9ce4e                                ...           8 months ago
  • 0.0.0-insiders.3df9921                                ...           8 months ago
  • 0.0.0-insiders.360cb77                                ...           9 months ago
  • 0.0.0-insiders.b4aa821                                ...           9 months ago
  • 0.5.13                                ...           9 months ago
  • 0.0.0-insiders.3201e5f                                ...           9 months ago
  • 0.0.0-insiders.d66a6d5                                ...           9 months ago
  • 0.0.0-insiders.35fe358                                ...           9 months ago
  • 0.0.0-insiders.54f0d43                                ...           9 months ago
  • 0.0.0-insiders.10e0c19                                ...           9 months ago
  • 0.5.12                                ...           10 months ago
  • 0.0.0-insiders.f563336                                ...           10 months ago
  • 0.0.0-insiders.b019552                                ...           10 months ago
  • 0.0.0-insiders.81c446e                                ...           10 months ago
  • 0.0.0-insiders.1fa24c2                                ...           a year ago
  • 0.0.0-insiders.4338bd0                                ...           a year ago
  • 0.5.11                                ...           a year ago
  • 0.0.0-insiders.bf40417                                ...           a year ago
  • 0.0.0-insiders.08f5c4c                                ...           a year ago
  • 0.5.10                                ...           a year ago
  • 0.0.0-insiders.885b14f                                ...           a year ago
  • 0.0.0-insiders.9f8780c                                ...           a year ago
  • 0.5.9                                ...           a year ago
  • 0.0.0-insiders.e3229c2                                ...           a year ago
  • 0.0.0-insiders.fa38999                                ...           a year ago
  • 0.5.8                                ...           a year ago
  • 0.0.0-insiders.dd4bfce                                ...           a year ago
  • 0.0.0-insiders.4cc6319                                ...           a year ago
  • 0.0.0-insiders.cf301ca                                ...           a year ago
  • 0.5.7                                ...           a year ago
  • 0.0.0-insiders.38f2be3                                ...           a year ago
  • 0.0.0-insiders.92f10af                                ...           a year ago
  • 0.0.0-insiders.15c1196                                ...           a year ago
  • 0.5.6                                ...           a year ago
  • 0.0.0-insiders.c51df4d                                ...           a year ago
  • 0.0.0-insiders.2968765                                ...           a year ago
  • 0.0.0-insiders.b5a251e                                ...           a year ago
  • 0.5.5                                ...           a year ago
  • 0.0.0-insiders.12d175e                                ...           a year ago
  • 0.0.0-insiders.047d591                                ...           a year ago
  • 0.0.0-insiders.b300077                                ...           a year ago
  • 0.0.0-insiders.38fed86                                ...           a year ago
  • 0.5.4                                ...           a year ago
  • 0.0.0-insiders.93fd16d                                ...           a year ago
  • 0.0.0-insiders.3ebd2f9                                ...           a year ago
  • 0.0.0-insiders.7694e65                                ...           a year ago
  • 0.0.0-insiders.be81958                                ...           a year ago
  • 0.0.0-insiders.e5605ec                                ...           a year ago
  • 0.0.0-insiders.14979f4                                ...           a year ago
  • 0.5.3                                ...           a year ago
  • 0.0.0-insiders.3c4989c                                ...           a year ago
  • 0.0.0-insiders.b062544                                ...           a year ago
  • 0.0.0-insiders.468d3bf                                ...           a year ago
  • 0.0.0-insiders.dad4b36                                ...           a year ago
  • 0.5.2                                ...           a year ago
  • 0.0.0-insiders.72cbb6b                                ...           a year ago
  • 0.0.0-insiders.985302f                                ...           a year ago
  • 0.5.1                                ...           a year ago
  • 0.0.0-insiders.4b768f7                                ...           a year ago
  • 0.0.0-insiders.f365a2b                                ...           a year ago
  • 0.5.0                                ...           a year ago
  • 0.0.0-insiders.7dab79c                                ...           a year ago
  • 0.0.0-insiders.ac70a66                                ...           a year ago
  • 0.0.0-insiders.121f4fa                                ...           a year ago
  • 0.0.0-insiders.36f92f3                                ...           a year ago
  • 0.0.0-insiders.ad48889                                ...           a year ago
  • 0.0.0-insiders.e642c31                                ...           a year ago
  • 0.0.0-insiders.1bc4f3e                                ...           a year ago
  • 0.4.1                                ...           a year ago
  • 0.0.0-insiders.f667b21                                ...           a year ago
  • 0.0.0-insiders.a13d661                                ...           a year ago
  • 0.0.0-insiders.dfc23a8                                ...           a year ago
  • 0.4.0                                ...           a year ago
  • 0.0.0-insiders.532a996                                ...           a year ago
  • 0.0.0-insiders.0184e34                                ...           a year ago
  • 0.0.0-insiders.f713976                                ...           a year ago
  • 0.0.0-insiders.6ea4491                                ...           a year ago
  • 0.0.0-insiders.17ceff1                                ...           a year ago
  • 0.0.0-insiders.fe49d70                                ...           a year ago
  • 0.0.0-insiders.4443561                                ...           2 years ago
  • 0.0.0-insiders.0a1b1a6                                ...           2 years ago
  • 0.0.0-insiders.46d60d5                                ...           2 years ago
  • 0.0.0-insiders.59d8f46                                ...           2 years ago
  • 0.3.0                                ...           2 years ago
  • 0.0.0-insiders.cc1b192                                ...           2 years ago
  • 0.0.0-insiders.d3f787d                                ...           2 years ago
  • 0.0.0-insiders.6ba6faa                                ...           2 years ago
  • 0.0.0-insiders.78bd35b                                ...           2 years ago
  • 0.0.0-insiders.474c344                                ...           2 years ago
  • 0.0.0-insiders.71f41a4                                ...           2 years ago
  • 0.0.0-insiders.fcbe554                                ...           2 years ago
  • 0.0.0-insiders.a78dd6e                                ...           2 years ago
  • 0.0.0-insiders.c5ff418                                ...           2 years ago
  • 0.0.0-insiders.5f9db4b                                ...           2 years ago
  • 0.2.8                                ...           2 years ago
  • 0.0.0-insiders.dfc4f64                                ...           2 years ago
  • 0.0.0-insiders.df84d8f                                ...           2 years ago
  • 0.2.7                                ...           2 years ago
  • 0.0.0-insiders.d539a72                                ...           2 years ago
  • 0.0.0-insiders.af16880                                ...           2 years ago
  • 0.0.0-insiders.70ea7aa                                ...           2 years ago
  • 0.2.6                                ...           2 years ago
  • 0.0.0-insiders.134c19b                                ...           2 years ago
  • 0.0.0-insiders.4815839                                ...           2 years ago
  • 0.0.0-insiders.2aabc3c                                ...           2 years ago
  • 0.0.0-insiders.41c7738                                ...           2 years ago
  • 0.0.0-insiders.7878ea1                                ...           2 years ago
  • 0.0.0-insiders.dfadcf7                                ...           2 years ago
  • 0.0.0-insiders.6533049                                ...           2 years ago
  • 0.2.5                                ...           2 years ago
  • 0.2.4                                ...           2 years ago
  • 0.2.3                                ...           2 years ago
  • 0.2.2                                ...           2 years ago
  • 0.2.1                                ...           2 years ago
  • 0.2.0                                ...           2 years ago
  • 0.2.0-beta.3                                ...           2 years ago
  • 0.2.0-beta.2                                ...           2 years ago
  • 0.2.0-beta.1                                ...           2 years ago
  • 0.1.13                                ...           2 years ago
  • 0.1.12                                ...           2 years ago
  • 0.1.11                                ...           3 years ago
  • 0.1.10                                ...           3 years ago
  • 0.1.9                                ...           3 years ago
  • 0.1.8                                ...           3 years ago
  • 0.1.7                                ...           3 years ago
  • 0.1.6                                ...           3 years ago
  • 0.1.5                                ...           3 years ago
  • 0.1.4                                ...           3 years ago
  • 0.1.3                                ...           3 years ago
  • 0.1.2                                ...           3 years ago
  • 0.1.1                                ...           3 years ago
  • 0.1.0                                ...           3 years ago

© 2010 - cnpmjs.org x YWFE | Home | YWFE