Sub topic 5
Variants Through Props
This chapter assumes you know Tailwind utility classes, basic React (components, props, JSX), and how Tailwind finds class names in your source files. It builds on the extracted Button component from the previous chapter.
Why variants belong in props
Once a button is a component, you will want more than one look: primary, secondary, danger. Copying the component for each look brings the duplication back. Instead, the component takes a variant prop and picks the matching classes.
The classes that every variant shares are written once. The classes that differ live in a lookup object.
A lookup object of complete class strings
const variants = {
primary: "bg-blue-600 text-white hover:bg-blue-700",
secondary: "bg-gray-200 text-gray-900 hover:bg-gray-300",
};
export function Button({ variant = "primary", children, ...props }) {
return (
<button
className={`rounded-md px-4 py-2 font-semibold ${variants[variant]}`}
{...props}
>
{children}
</button>
);
}Here is how it works:
variantsmaps a variant name to a full string of utilities.variant = "primary"is a default, so<Button>Save</Button>needs no prop....propsforwards other attributes such asonClickortypeto the<button>.- Shared classes (
rounded-md px-4 py-2 font-semibold) appear once.
Usage looks like this:
<Button>Save</Button>
<Button variant="secondary">Cancel</Button>Keep class names complete
Tailwind does not run your code. It scans source files as plain text and generates CSS for every class name it finds. A class that exists only after string assembly is never seen.
// Broken: Tailwind never sees "bg-red-600"
const color = "red";
const className = `bg-${color}-600`;The scanner finds bg- and -600 but not bg-red-600, so no CSS is generated and the button has no background. The lookup object avoids this because every class is written out in full, even though the choice between them happens at runtime.
Handling an unknown variant
If a caller passes variant="danger" and the object has no such key, variants[variant] is undefined. The template literal then inserts the text undefined into the class list. The button renders without variant styles and no error appears.
A simple guard is to fall back to the default:
const style = variants[variant] ?? variants.primary;In TypeScript, typing variant as keyof typeof variants catches the mistake at compile time instead.
Adding more dimensions
Variants often come in several independent groups, such as color and size. Give each group its own lookup object and combine them.
const colors = {
primary: "bg-blue-600 text-white hover:bg-blue-700",
secondary: "bg-gray-200 text-gray-900 hover:bg-gray-300",
};
const sizes = {
sm: "px-3 py-1 text-sm",
md: "px-4 py-2 text-base",
};
export function Button({ variant = "primary", size = "md", children, ...props }) {
return (
<button
className={`rounded-md font-semibold ${colors[variant]} ${sizes[size]}`}
{...props}
>
{children}
</button>
);
}Each group stays small, and you add a new option by adding one line.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Variant has no styling | Class name built from fragments | Write complete class names in the lookup |
| Text `undefined` in `class` | Unknown variant key | Add a default fallback or use types |
| Caller's `className` ignored | `...props` placed before `className`, or `className` overwritten | Merge the caller's classes explicitly (next chapter) |
| Two conflicting utilities, wrong one wins | Order in the stylesheet decides, not order in the string | Resolve conflicts with a merge helper |
Exercise
A teammate writes className={bg-${tone}-500 px-4} with tone set to "green" or "red", and the buttons have no background. Why, and how would you fix it?
Show answer
Tailwind scans source text and never sees the full names bg-green-500 or bg-red-500, so it generates no CSS for them. Fix it with a lookup object such as const tones = { green: "bg-green-500", red: "bg-red-500" } and use tones[tone], so each class appears in full in the source.
Exercise
Add a danger variant to the colors object that uses a red background, white text, and a darker red on hover.
Show answer
Add the line danger: "bg-red-600 text-white hover:bg-red-700", to colors. The component code does not change, and <Button variant="danger"> now works.