Merge Tailwind CSS classes without style conflicts
—
Core functionality for merging and joining Tailwind CSS classes with intelligent conflict resolution and conditional handling.
Main function to merge Tailwind CSS classes without style conflicts. Uses the default configuration and intelligent conflict resolution.
/**
* Default function to merge Tailwind CSS classes without style conflicts
* @param classLists - Class name values to merge
* @returns Merged class string with conflicts resolved
*/
function twMerge(...classLists: ClassNameValue[]): string;Usage Examples:
import { twMerge } from "tailwind-merge";
// Basic conflict resolution - later classes override earlier ones
twMerge('px-2 py-1 bg-red hover:bg-dark-red', 'p-3 bg-[#B91C1C]');
// → 'hover:bg-dark-red p-3 bg-[#B91C1C]'
// Handle conditional classes
const isActive = true;
const size = 'large';
twMerge(
'px-4 py-2 rounded-md',
isActive && 'bg-blue-500 text-white',
!isActive && 'bg-gray-200 text-gray-700',
size === 'large' && 'px-6 py-3 text-lg'
);
// Complex merging with multiple conflicts
twMerge('text-sm text-red-500 hover:text-blue-500', 'text-lg hover:text-green-500');
// → 'text-lg hover:text-green-500'
// Handle arrays and nested values
twMerge('p-4', ['m-2', null, false, ['text-center', undefined]]);
// → 'p-4 m-2 text-center'Function to join className strings conditionally without resolving conflicts. Similar to clsx but optimized for Tailwind classes.
/**
* Join className strings conditionally without resolving conflicts
* @param classLists - Class name values to join
* @returns Joined class string without conflict resolution
*/
function twJoin(...classLists: ClassNameValue[]): string;Usage Examples:
import { twJoin } from "tailwind-merge";
// Basic joining without conflict resolution
twJoin('px-2 py-1 bg-red', 'p-3 bg-blue');
// → 'px-2 py-1 bg-red p-3 bg-blue' (no conflicts resolved)
// Conditional joining
const hasBackground = true;
const hasLargeText = false;
const hasLargeSpacing = true;
twJoin(
'border border-red-500',
hasBackground && 'bg-red-100',
hasLargeText && 'text-lg',
hasLargeSpacing && ['p-2', hasLargeText ? 'leading-8' : 'leading-7']
);
// → 'border border-red-500 bg-red-100 p-2 leading-7'
// Handle falsy values
twJoin('base-class', null, undefined, false, 0, '', 'actual-class');
// → 'base-class actual-class'Use twMerge when:
Use twJoin when:
type ClassNameValue = ClassNameArray | string | null | undefined | 0 | 0n | false;
type ClassNameArray = ClassNameValue[];
type TailwindMerge = (...classLists: ClassNameValue[]) => string;Install with Tessl CLI
npx tessl i tessl/npm-tailwind-merge