• Stars
    star
    4,549
  • Rank 8,930 (Top 0.2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 2 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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

// prettier.config.js
module.exports = {
  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.

// 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.

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:

// 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>
  );
}

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:

// 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>
  )
}

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:

// 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>
  )
}

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

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

More Repositories

1

tailwindcss

A utility-first CSS framework for rapid UI development.
HTML
77,154
star
2

headlessui

Completely unstyled, fully accessible UI components, designed to integrate beautifully with Tailwind CSS.
TypeScript
23,838
star
3

heroicons

A set of free MIT-licensed high-quality SVG icons for UI development.
JavaScript
20,391
star
4

tailwindcss-forms

A plugin that provides a basic reset for form styles that makes form elements easy to override with utilities.
HTML
3,862
star
5

tailwindcss-typography

Beautiful typographic defaults for HTML you don't control.
JavaScript
3,513
star
6

tailwindcss.com

The Tailwind CSS documentation website.
MDX
3,066
star
7

tailwindcss-intellisense

Intelligent Tailwind CSS tooling for Visual Studio Code
TypeScript
2,635
star
8

tailwindcss-jit

JavaScript
2,197
star
9

tailwindcss-custom-forms

A better base for styling form elements with Tailwind CSS.
JavaScript
1,557
star
10

tailwindcss-line-clamp

A plugin that provides utilities for visually truncating text after a fixed number of lines.
JavaScript
1,152
star
11

tailwindcss-aspect-ratio

JavaScript
905
star
12

tailwindcss-container-queries

A plugin for Tailwind CSS v3.2+ that provides utilities for container queries.
TypeScript
877
star
13

designing-with-tailwindcss

Source code for the "Designing with Tailwind CSS" course.
CSS
705
star
14

heroicons.com

JavaScript
697
star
15

tailwindcss-setup-examples

679
star
16

tailwindui-vue

Deprecated, please use the Headless UI repo instead.
JavaScript
650
star
17

play.tailwindcss.com

JavaScript
638
star
18

tailwindcss-playground

HTML
516
star
19

tailwindui-react

Deprecated, please use the Headless UI repo instead.
TypeScript
432
star
20

tailwindcss-from-zero-to-production

Source code for the "Tailwind CSS Up and Running" screencast series.
JavaScript
369
star
21

blog.tailwindcss.com

JavaScript
340
star
22

tailwindui-issues

Bug fixes and feature request tracking for Tailwind UI.
231
star
23

webpack-starter

A quick and simple example of using Tailwind CSS with Webpack.
JavaScript
216
star
24

tailwindcss-plugin-examples

Examples to help you get started building your own Tailwind CSS plugins.
JavaScript
208
star
25

discuss

A place to ask questions, get help, or share what you've built with Tailwind CSS.
171
star
26

tailwind-play-api

PHP
44
star
27

minimal-tailwind-postcss-plugin

A simple PostCSS plugin designed to cover all of the tricky integration points a build-tool needs to support for Tailwind CSS.
JavaScript
10
star
28

reproduction-headlessui-vue

JavaScript
8
star