Sub topic 4
Extracting a component
This chapter assumes you know basic HTML and CSS, common Tailwind utilities such as px-4 and bg-blue-600, and the basics of a component framework such as React. It covers the main way to reuse Tailwind styles: move repeated markup and classes into one component.
Why a component fixes the duplication
Long class lists are not a CSS problem. The problem is that the same markup appears in several places in your templates. Consider three buttons that share one class list:
<button class="rounded-md bg-blue-600 px-4 py-2 font-semibold text-white hover:bg-blue-700">Save</button>
<button class="rounded-md bg-blue-600 px-4 py-2 font-semibold text-white hover:bg-blue-700">Publish</button>
<button class="rounded-md bg-blue-600 px-4 py-2 font-semibold text-white hover:bg-blue-700">Send</button>If you change the hover color, you must edit three lines. A component gives the repeated markup a single home. You edit it once, and every use updates.
This works well with Tailwind because the styles live in the markup. The component carries its styling with it, so you don't need a separate stylesheet.
Step 1: Move the markup into a component
Start with the simplest version. Put the shared classes in one place and accept the label as children.
export function Button({ children, ...props }) {
return (
<button
className="rounded-md bg-blue-600 px-4 py-2 font-semibold text-white hover:bg-blue-700"
{...props}
>
{children}
</button>
);
}Now each call site shrinks to <Button>Save</Button>. The ...props spread forwards attributes such as type, onClick, and disabled to the underlying <button>.
Step 2: Add variants through props
Most designs need more than one button style. Keep the shared classes in the component and map each variant name to a complete class string.
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>
);
}Usage looks like this:
<Button>Save</Button>
<Button variant="secondary">Cancel</Button>How the pieces fit together
Common errors
- Dynamic class names. Building a class from a variable, as in the warning above, breaks detection. Use a lookup object.
- Unknown variant.
variants["danger"]returnsundefined, so the stringundefinedends up inclassName. Define every variant you use, or fall back to a default. - Forgetting to forward props. If you omit
...props, handlers such asonClicknever reach the DOM element. - Conflicting overrides. If a caller passes
className="p-3"and you append it afterpx-4 py-2, both rules apply. Which one wins depends on the order of rules in the generated CSS, not on the order in your string. The next chapter on merging classes shows howtailwind-mergeresolves this. Until then, avoid letting callers override spacing on the same component.
When to extract
Extract a component when the same markup structure repeats, not only the same classes. Buttons, cards, and form fields are typical. If only a class list repeats and the markup differs, a component is the wrong tool. The decision chapter covers those cases.
Exercise
A teammate writes className={bg-${color}-500 px-4} and passes color="red". The button has no background in production. Why, and how do you fix it?
Show answer
Tailwind scans source files for complete class names. The string bg-red-500 never appears in the source, so no CSS is generated for it. Fix it by mapping each color to a full class string, such as { red: "bg-red-500", green: "bg-green-500" }, and reading from that object.
Exercise
Add a danger variant to the Button component that has a red background, white text, and a darker red hover state. Which line do you change?
Show answer
Add an entry to the variants object: danger: "bg-red-600 text-white hover:bg-red-700",. The shared classes and the component body stay the same, and callers use <Button variant="danger">.