Sub topic 9
Conditional and dynamic class names
This chapter assumes you know how Tailwind scans your source files for class names, and that you can write React components with props and TypeScript types. You will learn why some class names fail to appear in the generated CSS and how to write conditional styling that always works.
Why dynamic names fail
Tailwind does not run your code. At build time it reads your source files as plain text and looks for strings that match utility class names. It then generates CSS for each match. If a complete class name never appears in the text, no CSS is produced for it.
A template literal such as ` text-${color}-600 never contains text-red-600` as written. The browser receives the class at runtime, but the stylesheet has no rule for it. The element renders unstyled, and no error appears.
Map values to full class strings
The fix is to write every complete class name in the source, then select among them at runtime.
type Status = 'error' | 'success'
const statusClasses: Record<Status, string> = {
error: 'text-red-600',
success: 'text-green-600',
}
export function StatusText({ status }: { status: Status }) {
return <p className={statusClasses[status]}>Status: {status}</p>
}The scanner sees text-red-600 and text-green-600 in the object, so both rules are generated. At runtime, the lookup picks the right string.
The Record<Status, string> type adds a second benefit. If you add a new Status value and forget its classes, TypeScript reports an error.
Conditional classes
For a simple on/off condition, choose between two complete strings with a ternary expression. Keep the fixed classes in the string itself, and add the conditional part at the end.
export function Tag({ active, label }: { active: boolean; label: string }) {
return (
<span
className={`rounded-full px-3 py-1 text-sm ${
active ? 'bg-blue-600 text-white' : 'bg-gray-100 text-gray-700'
}`}
>
{label}
</span>
)
}Both branches are complete strings, so both are detected. Interpolating a whole class string into a template literal is fine. Only building a class name from fragments is a problem.
When several conditions combine, nested ternaries get hard to read. A lookup object keyed by state is usually clearer. The next chapters cover helpers that handle this more cleanly.
Prefer variants and data attributes for state
Some "conditions" do not need JavaScript. Hover, focus and disabled are handled by state variants, so avoid tracking them in React state just to change a class.
export function Toggle({ on }: { on: boolean }) {
return (
<button
data-on={on}
className="rounded-md bg-gray-200 px-4 py-2 data-[on=true]:bg-green-600 data-[on=true]:text-white"
>
{on ? 'On' : 'Off'}
</button>
)
}React renders data-on={true} as data-on="true". The data-[on=true]: variant matches that attribute, and the class name is still a complete string in the source.
Common mistakes
| Mistake | Why it fails | Fix |
|---|---|---|
| `` `bg-${color}-500` `` | The full name is never in the source | Map each value to a full class |
| `'p-' + size` | Same problem with concatenation | Use a lookup object |
| Classes only in a database or API response | The scanner never sees them | Store a key, and map it to classes in code |
| Classes in a file outside the scanned sources | Tailwind skips files it does not scan | Move them into scanned files, or register the path with `@source` in your CSS |
Exercise
A teammate writes <div className={w-${columns}/12}> and the width never applies. Why, and how would you fix it with a prop columns of type 3 | 4 | 6?
Show answer
Tailwind scans source text, and w-3/12, w-4/12 and w-6/12 never appear as complete strings, so no CSS is generated. Define const widths = { 3: 'w-3/12', 4: 'w-4/12', 6: 'w-6/12' } as const and use className={widths[columns]}.