# Sort Tailwind CSS Classes with Prettier

Yes. The agent adds prettier-plugin-tailwindcss, fixes the Prettier version it needs, sorts every class list in one commit and makes lint enforce the order.

[Run this on Rush](https://cloud.getrush.ai/run/add-class-sorting/tailwind/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The plugin is one line of config; the agent's work is making sure it runs where the team already runs Prettier and that the first sort is a commit nobody has to think about again. In a project like alan2207/bulletproof-react (`apps/react-vite`) there is no `format` script. `.prettierrc` holds five options, `.prettierignore` holds `*.hbs`, and Prettier runs only through ESLint: `.eslintrc.cjs` extends `plugin:prettier/recommended` and sets the `prettier/prettier` rule to `error` with `usePrettierrc: true`. That is good news for this job. Whatever the plugin wants sorted becomes a lint error, so `yarn lint` in `.github/workflows/react-vite-ci.yml` and the `lint-staged` entry that the root `.husky/pre-commit` runs will both enforce the order with no new step.

The first run fails, and not because of the classes. `yarn.lock` pins Prettier at 3.2.5. When this page was written the current plugin, `prettier-plugin-tailwindcss` 0.8.1, declares `prettier` `^3.0` as its peer, yet with 3.2.5 every file errors with `TypeError: e.charAt is not a function`, and `yarn lint` dies the same way. Moving Prettier to the newest 3.x fixes it. The agent does that as a commit of its own, because a newer Prettier formats one file differently with no plugin involved: the two long `font-family` declarations in `src/index.css` move onto their own line. Kept separate, that diff is not mistaken for class sorting.

With the plugin listed under `plugins` in `.prettierrc`, it reads `tailwind.config.cjs` from the project root and sorts every `class` and `className` attribute, including strings inside a `cn(...)` call written in the attribute. Class lists outside an attribute are another matter. `src/components/ui/button/button.tsx` defines its variants with `cva(...)`, and those strings are only sorted once `tailwindFunctions` names `cva` and `cn`. The agent adds that option. In this repository it changes nothing today, because those strings are already in order, and it keeps the next variant somebody writes from slipping through.

The sort itself is small here: three component files. In `src/components/layouts/dashboard-layout.tsx` `flex flex-1 w-full` becomes `flex w-full flex-1` in two places and a trailing `w-full` moves forward in a third; in `dialog.tsx` and `drawer.tsx` the only change is a doubled space collapsed. The project also runs `eslint-plugin-tailwindcss`, whose `classnames-order` rule sorts by its own logic. Lint exits 0 before and after the sort, so the two do not disagree on this code, but two sorters are one too many and the agent turns that rule off. It commits the reformat alone and adds the commit to `.git-blame-ignore-revs` so `git blame` skips it.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add automatic Tailwind CSS class sorting with prettier-plugin-tailwindcss. Read package.json, the lockfile entry for prettier, the Prettier config and ignore file, the ESLint config, the lint-staged and git hook setup, the Tailwind config and the CI workflow first. Install dependencies and record the lint, type check and test results before changing anything. Work in separate commits. First, install the plugin and check that Prettier runs with it on one file; if it throws, find the Prettier version the plugin needs, upgrade Prettier within its current major, and commit that upgrade together with any formatting change it causes by itself, with no plugin configured. Second, add the plugin to the Prettier config, and set tailwindFunctions for every helper the code uses to build class strings outside a class attribute, such as cva, cn or clsx; search the code to find them. If an ESLint rule also orders Tailwind classes, turn that rule off and keep the rest of its plugin. Third, run Prettier over the source and commit the result alone, then add that commit to .git-blame-ignore-revs. Do not change any class name, only order and whitespace. Confirm that the existing lint command now fails on an unsorted class list and passes on the sorted tree. Run lint, type check, tests and the build. Open a pull request that lists the commits, the files the sort touched, and the output of each check.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request with three commits meant to be read in order: the Prettier upgrade with the one stylesheet it reformats, the configuration (`.prettierrc`, the ESLint rule switched off, `package.json` and the lockfile), and the mechanical sort. The description lists the files the sort touched and shows one before-and-after line from each, so a reviewer can confirm that only order and whitespace moved.

It includes the evidence that the order is now enforced: the output of `yarn lint` on a scratch change that puts two classes out of order, failing with a `prettier/prettier` error, and on the committed tree, passing. The type check, tests and build output follow. Since sorting never changes which classes an element has, the built CSS should be the same; the agent builds before and after and reports whether the stylesheet differs.

One decision is left to the team and flagged as such. `.git-blame-ignore-revs` names the sort commit by hash, and a squash merge replaces that hash. The description says to merge with a merge commit or a rebase, or to update the file with the squashed commit afterwards, and gives the one `git config blame.ignoreRevsFile` line each developer needs locally.

## Run it on every repository with unsorted class lists

This is a job a developer hands off when a review has turned into an argument about class order. They send this body to `POST /o/{org}/sessions` from a terminal or an editor's MCP client and go back to the review; the pull request is on the session when the run completes. A `request_id`, a UUID made once for the job, makes a retried call return the same run and not a second pull request.

For an organisation with many front ends, a script sends the body once per repository, each with a `request_id` that is a UUID v5 of the repository name. Open branches in each repository will conflict with the sort commit in the files it touches, so time the merge for a quiet moment, or have authors run the formatter on their branch after rebasing, which resolves those conflicts by construction.

## Why run it on Rush?

The part of this job that takes time is not the config, it is the failure that comes before it: a plugin that throws on the pinned Prettier, visible only when the formatter is actually run on the code. In its own sandbox the agent installs the project from its lockfile, hits that error, finds the version that clears it and proves the result with the project's own lint command. Each command and its output is kept in the transcript at `GET /o/{org}/sessions/{id}/messages`.

It is also a change best made in one clean sweep on a fresh checkout, with nothing half-edited in the tree. The run's checkout is exactly the default branch, so the sort commit contains the sort and nothing else, and the developer who asked for it never has to stash their own work to produce it.

## Frequently asked questions

### Does the plugin work with Tailwind CSS v4?

Yes, with one more option. Version 4 keeps its theme in a stylesheet instead of a JavaScript config, so the plugin needs `tailwindStylesheet` pointing at that file to know custom utilities and colours. The agent reads which major the project uses and sets the matching option.

### Will it reorder classes built with template strings or conditionals?

It sorts each string literal it can see: inside `className`, and inside the functions named in `tailwindFunctions`. It does not reorder across separate strings or follow variables, so `cn('p-2', isActive && 'bg-accent')` gets each literal sorted and the arguments left where they are.

### Can I keep eslint-plugin-tailwindcss?

Yes. Only its ordering rule overlaps with the Prettier plugin; its other rules, such as the ones that flag contradicting or unknown classes, still earn their place. The agent turns off `tailwindcss/classnames-order` and leaves the rest of the recommended set on.

### What if we use Biome or dprint instead of Prettier?

Then this plugin does not apply, since it is a Prettier plugin. Say which formatter you use in the prompt; the agent checks whether it has a class sorting rule of its own, enables that instead and reports any difference in order from the official one.

## More Tailwind CSS jobs

- [Fix "It looks like you're trying to use `tailwindcss` directly as a PostCSS plugin" after Tailwind CSS 4](https://getrush.ai/run/fix-tailwindcss-directly-as-postcss-plugin/tailwind/)
- [Upgrade Tailwind CSS v3 to v4](https://getrush.ai/run/migrate-tailwind-v4/tailwind/)

## More jobs like this: Hand off a long task

- [Add Clippy to CI for a Tokio Project](https://getrush.ai/run/add-clippy-to-ci/tokio/)
- [Add CloudWatch Alarms to a Serverless Framework Service](https://getrush.ai/run/add-cloudwatch-alarms/serverless/)
- [Add CMakePresets.json to a CMake Project](https://getrush.ai/run/add-cmake-presets/cmake/)
- [Add a Custom Code Block Renderer to an Obsidian Plugin](https://getrush.ai/run/add-code-block-processor/obsidian-plugin/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
