You can use the Tailwind sorting logic programmatically by importing createSorter from prettier-plugin-tailwindcss/sorter. This is useful for sorting HTML class attributes (space-separated strings) or class lists (arrays of strings).
import { createSorter } from 'prettier-plugin-tailwindcss/sorter'
let sorter = await createSorter({
base: '/path/to/project',
stylesheetPath: './app.css',
})
// Sort HTML class attributes (space-separated strings)
let sorted = sorter.sortClassAttributes([
'sm:bg-tomato bg-red-500',
'p-4 m-2'
])
// Returns: ['bg-red-500 sm:bg-tomato', 'm-2 p-4']
// Sort class lists (arrays of class names)
let sortedLists = sorter.sortClassLists([
['sm:bg-tomato', 'bg-red-500'],
['p-4', 'm-2']
])
// Returns: [['bg-red-500', 'sm:bg-tomato'], ['m-2 p-4']]