Sub topic 3
When Loops and Multi-Cursor Editing Are Enough
This chapter assumes you know Tailwind utility classes such as flex, px-4, and bg-blue-500. It also assumes you have used a loop or a partial in a template language or a component framework. Not every repeated class list needs a new abstraction. Sometimes the repetition exists only in the file you are looking at, and a simpler tool fixes it.
The test: is the duplication in your source or only in the output?
Repetition in the rendered HTML is normal. Repetition in your source is the real cost. If one element sits inside a loop, the browser receives many copies, but you maintain only one.
Ask two questions before you extract anything:
- Does the class list appear once in the source, inside something that repeats?
- Do all copies live in one file, so you can see and change them together?
If both answers are yes, you probably don't need a component or @apply.
Loops: one class list, many items
A list of cards, nav links, or table rows is the common case. The markup is written once and the data drives the repetition.
const plans = [
{ name: "Free", price: "$0" },
{ name: "Pro", price: "$12" },
{ name: "Team", price: "$40" },
];
export function Pricing() {
return (
<ul className="grid gap-4 md:grid-cols-3">
{plans.map((plan) => (
<li
key={plan.name}
className="rounded-lg border border-gray-200 p-6 shadow-sm"
>
<h3 className="text-lg font-semibold">{plan.name}</h3>
<p className="text-gray-600">{plan.price}</p>
</li>
))}
</ul>
);
}The <li> class list exists once. Changing p-6 to p-8 changes all three cards. Nothing here is duplicated in the source.
The same idea works outside React. In a template language you use its own loop, such as {% for %} in Jinja or Nunjucks, v-for in Vue, or {#each} in Svelte.
<ul class="space-y-2">
<li v-for="item in items" :key="item.id" class="rounded bg-gray-100 px-3 py-2">
{{ item.label }}
</li>
</ul>Multi-cursor editing: repetition in one file
Sometimes you have several similar elements that differ in content, such as three hand-written buttons on a static page. A loop would need data and indirection that cost more than they save. Your editor can handle the edit instead.
With multi-cursor editing you select every occurrence of a string and change them in one step. In VS Code, select the text and press Ctrl+D (or Cmd+D on macOS) to add the next match, or Ctrl+Shift+L (Cmd+Shift+L) to select all matches. Then type the replacement.
Suppose a page has three identical buttons and you want a darker blue on hover. Select hover:bg-blue-700, select all matches, and type hover:bg-blue-800. All three change at once.
Tailwind's class names are literal strings, so search and replace is reliable. That is an advantage over custom class names, which can be hard to find if they are built dynamically.
Where this approach stops working
Loops and multi-cursor editing have limits. Watch for these signs:
| Sign | What it means |
|---|---|
| The same class list appears in several files | Multi-cursor editing in one file no longer reaches every copy |
| The element has behavior, such as state or accessibility attributes | A loop repeats markup but gives you no place to share logic |
| Copies differ in small ways, such as a variant or a size | Edits must be made by hand and copies drift apart |
| Other developers copy and paste the element | The pattern is spreading and needs a single name |
Multi-cursor editing also depends on discipline. It fixes the copies that exist today. It does nothing to stop a new copy from being pasted next month with the old classes.
A quick decision flow
Common mistakes
- Building class names inside a loop from fragments. Writing `
bg-${color}-500` hides the full class from Tailwind, so it never generates the CSS. Use complete class names, for example from a lookup object. - Looping when the items are genuinely different. Forcing different elements through one loop with many
ifbranches is harder to read than separate markup. - Ignoring other files. Run a project-wide search for the class list before deciding that the duplication is local.
Exercise
A static page has four identical <a> links in a footer, each with the same twelve utility classes. They appear nowhere else in the project. Name the simplest fix for changing their color later, and give one condition that would make you extract a component instead.
Show answer
Loop over an array of link data (or use multi-cursor editing to change all four at once), because all copies are in one file and the class list is written once or edited together. Extract a component if the same link style starts appearing in other files, or if the links need shared behavior or variants such as an active state.