Sub topic 2
Spotting real duplication
This chapter assumes you know basic HTML and CSS, common Tailwind utilities such as flex, px-4, and bg-blue-500, and the idea of a component or template partial. Its goal is to help you tell real duplication from repetition that is harmless.
Why long class lists are not a bug
Utility-first styling puts the styling in the markup. A button can easily carry six or eight classes. That length alone is not a problem. You can see the whole style in one place, and you can change it without hunting through a separate stylesheet.
The cost shows up when the same list appears many times. Here is the usual example:
<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 the design changes to rounded-lg, you must edit three lines. In a real project you might need to edit thirty, and missing one produces an inconsistent button.
The problem is in the template, not the CSS
The CSS Tailwind generates for these three buttons is not duplicated. Tailwind emits each utility once, no matter how many elements use it. The duplication lives in your templates: the same decision (what a primary button looks like) is written in many places.
This changes how you fix it. You do not need a new CSS rule to shrink the output. You need one place in your source where the decision is made. Components, loops, and @apply are different ways to create that place.
A test for real duplication
Not every repeated class list is a problem. Ask these questions before you extract anything:
- Do the copies represent the same idea? Three buttons that are all "the primary action button" share a purpose. A button and a badge that happen to share
rounded-md px-4do not. - Would you want them to change together? If a redesign should update every copy, the duplication is real. If each copy might drift on its own, leave them separate.
- Does the repetition happen in more than one file or place? Two copies in one small template are often fine. Copies across many pages are a maintenance risk.
If you answer yes to the first two, you have real duplication.
| Situation | Real duplication? | Why |
|---|---|---|
| Ten "Save" style buttons across forms | Yes | Same purpose, should change together |
| Cards in a list generated by a loop | No | The class list is written once in the source |
| A `px-4 py-2` pair on a button and a table cell | No | Shared by coincidence, not by design |
| Same form-input classes on every input in the app | Yes | One design decision repeated |
Repetition that is only in the output
The loop row in the table matters. If your template renders a list of items from an array, the class list appears once in the source even though it appears many times in the browser:
const items = ["Save", "Publish", "Send"];
export function Actions() {
return (
<div>
{items.map((label) => (
<button
key={label}
className="rounded-md bg-blue-600 px-4 py-2 font-semibold text-white hover:bg-blue-700"
>
{label}
</button>
))}
</div>
);
}Here you edit one line to change every button. Check whether a loop already solves your problem before reaching for something larger. The next chapter covers when loops and multi-cursor editing are enough.
Finding duplication in your project
Search for a distinctive class string. Searching for rounded-md bg-blue-600 px-4 py-2 across your source shows how many copies exist and where they live. Copies that differ by one class, such as px-4 against px-6, often point to a variant of one component. Note those, because variants are handled differently from exact copies.
What comes next
Once you have confirmed that duplication is real, you have several options. For markup that repeats, you extract a component. For styles that must live in plain CSS, you use @apply. The rest of this tutorial covers both and ends with a guide for choosing.
Exercise
A page has a product list rendered with products.map(...). Each card has the same 12 utility classes, and the browser shows 40 cards. A teammate says this is duplication and wants to move the classes into @apply. Is it real duplication? What would you check first?
Show answer
It is not real duplication in the source, because the class list is written once inside the loop. Changing it there updates all 40 cards. First check whether the same 12 classes also appear in other files or components for the same purpose. If they do, those copies may be real duplication worth extracting. If they do not, no change is needed.
Exercise
You find rounded-md px-4 py-2 on a primary button, a tag label, and a table header cell. List the two questions you would ask to decide whether to extract them together.
Show answer
Do the three elements represent the same design idea? Would you want a redesign to change all three together? If either answer is no, the shared classes are a coincidence, and extracting them would couple unrelated parts of the interface.