Tailwind CSS API Utilities

1.0.3 · active · verified Tue Apr 21

tailwind-api-utils is a utility library designed to expose and simplify interaction with the internal Tailwind CSS API. It enables programmatic access to Tailwind's configuration and class processing functionalities, allowing developers to, for example, load a Tailwind configuration object and sort CSS class names according to Tailwind's internal rules. The current stable version is 1.0.3. The package has seen consistent updates, particularly to maintain compatibility with both Tailwind CSS v3 and the upcoming v4. Key differentiators include its explicit support for different Tailwind CSS versions and functionalities like class name sorting, which can be useful for linting or maintaining consistent class order in projects. It primarily targets build tools and environments where programmatic interaction with Tailwind's core features is beneficial.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates loading a Tailwind CSS configuration and sorting a string of class names using the library's utilities, compatible with Tailwind CSS v3 and v4 configurations.

import { loadConfig, sortClassname } from 'tailwind-api-utils';
import path from 'path';
import process from 'process';

// A dummy Tailwind CSS configuration file content for demonstration
const tailwindConfigFileContent = `
  module.exports = {
    content: ['./index.html', './src/**/*.{js,ts,jsx,tsx}'],
    theme: {
      extend: {},
    },
    plugins: [],
  };
`;

async function runExample() {
  // In a real application, you would load your actual tailwind.config.js
  // For this example, we'll simulate a config file.
  // This path needs to be valid in the execution environment.
  const dummyConfigPath = path.join(process.cwd(), 'tailwind.config.js');
  // You might write this content to a temporary file if truly testing `loadConfig`
  // For simplicity, `loadConfig` tries to find it, so ensuring it exists is key.

  try {
    // Load the Tailwind config. This will look for tailwind.config.js by default.
    // Make sure 'tailwindcss' is installed as a peer dependency.
    const config = await loadConfig({ configPath: dummyConfigPath });
    console.log('Loaded Tailwind config:', config.theme.extend);

    const classesToSort = 'p-4 text-lg font-bold mx-auto flex items-center bg-blue-500 rounded-md';
    const sortedClasses = sortClassname(classesToSort, config);
    console.log('Original classes:', classesToSort);
    console.log('Sorted classes:', sortedClasses);

    const v4Classes = 'group/item:hover:p-4 md:text-xl sm:flex-row';
    const sortedV4Classes = sortClassname(v4Classes, config);
    console.log('Original v4 classes:', v4Classes);
    console.log('Sorted v4 classes (with v4 compatibility):', sortedV4Classes);

  } catch (error) {
    console.error('Failed to run example:', error);
    console.warn('Ensure `tailwindcss` is installed as a peer dependency.');
    console.warn('For `loadConfig` to work, a `tailwind.config.js` file (or specified path) must be resolvable.');
  }
}

runExample();

view raw JSON →