Skip to content

useSortedClasses (not released)

Diagnostic Category: lint/nursery/useSortedClasses

Enforce the sorting of CSS utility classes.

This rule implements the same sorting algorithm as Tailwind CSS, but supports any utility class framework including UnoCSS.

It is analogous to prettier-plugin-tailwindcss.

NOTE: this rule is only partially implemented. Progress is being tracked in the following GitHub issue: https://github.com/biomejs/biome/issues/1274

<div class="px-2 foo p-4 bar" />;
nursery/useSortedClasses.js:1:12 lint/nursery/useSortedClasses  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━

   These classes should be sorted.
  
  > 1 │ <div class="px-2 foo p-4 bar" />;
              ^^^^^^^^^^^^^^^^^^
    2 │ 
  
   The safe fix will automatically sort them.
  
   Safe fix: Sort the classes.
  
    1  - <div·class="px-2·foo·p-4·bar"·/>;
      1+ <div·class="foo·bar·p-4·px-2"·/>;
    2 2  
  
{
"options": {
"attributes": ["classList"],
"functions": ["clsx", "cva", "tw"]
}
}

Classes in the class and className JSX attributes are always sorted. Use this option to add more attributes that should be sorted.

If specified, strings in the indicated functions will be sorted. This is useful when working with libraries like clsx or cva.

Tagged template literals are also supported, for example:

tw`px-2`;
tw.div`px-2`;

NOTE: tagged template literal support has not been implemented yet.

NOTE: at the moment, this rule does not support customizing the sort options. Instead, the default Tailwind CSS configuration is hard-coded.

The main key difference is that Tailwind CSS and its Prettier plugin read the tailwind.config.js file, which Biome can’t access. Instead, Biome implements a simpler version of the configuration. The trade-offs are explained below.

Values are not known

Section titled test

The rule has no knowledge of values such as colors, font sizes, or spacing values, which are normally defined in a configuration file like tailwind.config.js. Instead, the rule matches utilities that support values in a simpler way: if they start with a known utility prefix, such as px- or text-, they’re considered valid.

This can result in false positives, i.e. classes that are wrongly recognized as utilities even though their values are incorrect. For example, if there’s a px- utility defined in the configuration, it will match all of the following classes: px-2, px-1337, px-[not-actually-valid], px-literally-anything.

Custom additions must be specified

Section titled test

The built-in Tailwind CSS preset (enabled by default) contains the set of utilities and variants that are available with the default configuration. More utilities and variants can be added through Tailwind CSS plugins. In Biome, these need to be manually specified in the Biome configuration file in order to “extend” the preset.

Presets can’t be modified

Section titled test

In Tailwind CSS, core plugins (which provide the default utilities and variants) can be disabled. In Biome, however, there is no way to disable parts of a preset: it’s all or nothing. A work-around is to, instead of using a preset, manually specify all utilities and variants in the Biome configuration file.

Whitespace is collapsed

Section titled test

The Tailwind CSS Prettier plugin preserves all original whitespace. This rule, however, collapses all whitespace (including newlines) into single spaces.

Section titled test