tailwind-variants

repository·main·Indexed 25 days ago

https://github.com/heroui-inc/tailwind-variants

A fully typed, framework-agnostic variant API for Tailwind CSS supporting slots, composition, and built-in conflict resolution. It provides the tv() function for creating variant-aware components, createTV() for custom configurations, and utility functions like cx(), cn(), and cnMerge() for class name manipulation. A lite version is available via tailwind-variants/lite for smaller bundle sizes without built-in conflict resolution.

Tokens
1.5K
Snippets
3
Records
19
Agent score
84%

What's inside tailwind-variants

  1. Install tailwind-variants

    main

    You can install tailwind-variants using npm, yarn, or pnpm.

    For a smaller bundle size without built-in conflict resolution, import from tailwind-variants/lite instead of the default entry point.

    npm i tailwind-variants
    # or
    yarn add tailwind-variants
    # or
    pnpm add tailwind-variants
  2. Run the benchmark suite

    main

    The benchmark suite measures the performance of the built tailwind-variants package. It compares the current version against the latest released version of tailwind-variants and the latest version of class-variance-authority (CVA). It also compares utility functions like cx, cn, and cnMerge against alternatives like clsx, twJoin, and cnfast.

    To run the full benchmark suite:

    pnpm benchmark

    To run a fast smoke test (note: this is not suitable for making performance claims):

    pnpm benchmark --quick

    To disable ANSI colors in the terminal output, set the NO_COLOR=1 environment variable.

  3. Configure Tailwind Variants with twMerge options

    main

    When configuring tailwind-variants, you can control how Tailwind CSS class conflicts are resolved using the twMerge and twMergeConfig properties.

    • twMerge: A boolean that determines whether to merge conflicting Tailwind classes. Defaults to true.
    • twMergeConfig: An object used to pass custom configuration to the underlying tailwind-merge engine. It supports extend and override patterns for merging configurations, making it compatible with standard tailwind-merge extension shapes.
  4. Understand benchmark measurement rules

    main

    The benchmark suite follows specific rules to ensure accuracy:

    • Workload Separation: It separates one-time component construction from repeated invocation and complete create-and-call lifecycle workloads.
    • Entry Points: Current and released tailwind-variants are imported from their built dist/index.js entry points.
    • Consistency: Current and released TV use the same installed tailwind-merge version. Equivalent workloads are guaranteed to produce identical output before measurement begins.
    • Timing: The default measurement duration is 1,000 ms following a 200 ms warmup period.
    • Noise Threshold: Differences within ±5% are considered noise and should be confirmed with repeated runs on the same machine.
    • Throughput: Variant-matrix throughput is measured in batches per second, with five calls per batch.
    • Cache Isolation: Custom tailwind-merge configuration runs in a separate final phase because the TV merger cache is process-global.
  5. Flatten arrays with flatArray() and flatMergeArrays()

    main

    Use these utilities to handle nested arrays:

    • flatArray<T>(arr: unknown[]): T[]: Flattens a single array of unknown depth into a flat array of type T.
    • flatMergeArrays<T>(...arrays: unknown[][]): T[]: Flattens multiple arrays into a single flat array and filters out falsy values.
  6. Use cx() for class name manipulation

    main
    The cx function is a utility for joining class names. It accepts various inputs (strings, objects, arrays, etc.) and returns a normalized, single string of class names. It automatically handles whitespace normalization and removes extra spaces.
  7. Deeply merge objects with mergeObjects()

    main

    The mergeObjects function performs a deep merge of two objects.

    • If both values for a key are arrays, they are merged using flatMergeArrays.
    • If both values are objects, they are merged recursively.
    • For other types, it concatenates the values with a space.