Sub topic 11
Merging and overriding classes safely
This chapter assumes you can write React components with props and JSX. It also assumes you have used Tailwind utility classes and have seen the typed variant pattern from the previous chapter. Here you will learn why conflicting classes behave unpredictably, and how clsx and tailwind-merge fix that.
The problem: conflicting classes
A reusable component usually sets default classes and lets callers add more through a className prop. Problems start when a caller's class conflicts with a default.
<button className="px-4 py-2 px-8">Save</button>Both px-4 and px-8 set horizontal padding. The order of names in the class attribute does not decide which one wins. The order of the rules in the generated stylesheet decides, and you do not control that. So a caller who writes className="px-8" may find the override ignored.
Step 1: join conditional classes with clsx
clsx builds a class string from strings, objects, arrays and falsy values. It removes the falsy ones. It does not know anything about Tailwind, so it cannot resolve conflicts.
Step 2: resolve conflicts with tailwind-merge
tailwind-merge understands Tailwind's class groups. When two classes target the same property, it keeps the last one and drops the earlier one. For example, twMerge('px-4 py-2 px-8') returns py-2 px-8.
Install both packages:
npm install clsx tailwind-mergeThe cn helper
Combine the two in one small function. Many projects name it cn.
import { clsx, type ClassValue } from 'clsx'
import { twMerge } from 'tailwind-merge'
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}clsx runs first and flattens everything into one string. twMerge then removes the losing classes from that string.
flowchart LRUsing cn in a component
The Button below puts base classes first, then the variant, then the caller's className. Because the last conflicting class wins, the caller can always override the defaults.
import { cn } from './cn'
const variants = {
primary: 'bg-blue-600 text-white hover:bg-blue-700',
secondary: 'bg-gray-100 text-gray-900 hover:bg-gray-200',
} as const
type ButtonProps = React.ButtonHTMLAttributes<HTMLButtonElement> & {
variant?: keyof typeof variants
}
export function Button({ variant = 'primary', className, ...props }: ButtonProps) {
return (
<button
className={cn(
'rounded-md px-4 py-2 font-medium disabled:opacity-50',
variants[variant],
className
)}
{...props}
/>
)
}A caller can now write <Button className="px-8 rounded-full">Save</Button>. The result keeps py-2 and the variant colors. It replaces px-4 with px-8 and rounded-md with rounded-full.
Common errors
- Caller classes placed first. Order matters. Put
classNamelast, or the defaults override the caller. - Using clsx alone. It joins strings but leaves both conflicting classes in place. The override may silently fail.
- Building class names from fragments.
twMergeworks on complete class names, and so does Tailwind's scanner. Keep using full strings such as'bg-blue-600'. - Expecting merge to cross variants.
twMergetreatshover:bg-blue-700andbg-blue-600as separate, since they apply under different conditions. Both stay.
Try it
- Create
cn.tswith the helper above. - Add the
Buttoncomponent. - Render
<Button className="px-8">and inspect the element in your browser's developer tools. - Confirm that the class list contains
px-8and nopx-4.
Exercise
A component renders className={cn('p-4 bg-white', className)}. A caller passes className="p-2 bg-gray-100". Which classes end up on the element, and what would happen if cn used only clsx?
Show answer
With cn, the element gets p-2 bg-gray-100, because twMerge drops the earlier p-4 and bg-white. With only clsx, all four classes remain (p-4 bg-white p-2 bg-gray-100). The winner would then depend on the order of rules in the generated CSS, not on the order in the string, so the override may not apply.