Performance: Conditional classes with clsx and twMerge
MEDIUM IMPACT
This concept affects how CSS classes are combined and applied, impacting runtime style recalculations.
import clsx from 'clsx'; import { twMerge } from 'tailwind-merge'; const buttonClass = twMerge(clsx('bg-blue-500', { 'text-white': isActive, 'text-gray-500': !isActive, 'opacity-50 cursor-not-allowed': isDisabled }));
const buttonClass = `bg-blue-500 ${isActive ? 'text-white' : 'text-gray-500'} ${isDisabled ? 'opacity-50 cursor-not-allowed' : ''}`;| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual string concatenation with conditional classes | Low (few nodes affected) | Multiple reflows if classes conflict | Medium due to redundant styles | [X] Bad |
| clsx + twMerge for conditional Tailwind classes | Low (few nodes affected) | Single reflow due to merged classes | Low paint cost from optimized styles | [OK] Good |