# Extracting Components

> Extracting React components keeps utilities composable, avoiding specificity traps, duplication, and maintainability pitfalls of the CSS @apply directive.

Subject: Tailwind CSS | Lesson 14 of 16 | 7 min read
Interactive lesson: https://codewithanimation.in/tailwind/extracting-components
Previous: [Data Attributes](https://codewithanimation.in/tailwind/data-attributes.md) | Next: [Dynamic Classes & tailwind-merge](https://codewithanimation.in/tailwind/dynamic-classes-and-merge.md)

## What Component Extraction in Tailwind Is

In modern component-based frameworks like React, component extraction is the primary pattern for style reuse in Tailwind CSS.

Instead of writing custom CSS classes with `@apply` inside external stylesheets, you encapsulate HTML markup and utility classes inside reusable React components such as `<Button>`, `<Card>`, or `<Badge>`.

React components receive props to compute utility class strings dynamically, maintaining atomic utility co-location directly inside JSX markup.

## Why React Components Excel Over the CSS @apply Directive

While the CSS `@apply` directive allows inline selectors to inherit utility declarations, relying on `@apply` introduces serious architectural drawbacks:

1. Specificity and Cascade Traps: Utilities applied inside custom selectors inherit the specificity of that custom selector. Overriding properties inline on an element with standard utilities can fail unexpectedly due to CSS specificity conflicts.

2. CSS Bundle Size Inflation: Utility-first CSS generates each utility declaration once in the output stylesheet. Using `@apply` across multiple custom class rules causes the CSS engine to duplicate CSS declarations inside every target rule, inflating bundle size.

3. Loss of Variant Flexibility: Using state modifiers like `hover:`, `focus:`, or responsive prefixes inside `@apply` requires complex nested CSS rules, destroying utility composability.

4. Class Naming Fatigue: Restores BEM class naming fatigue (`.btn-primary-large-v2`) and forces developers to switch constantly between JSX files and external `.css` stylesheets.

## How to Extract Reusable React UI Components

Follow these standard steps to build composable React components with Tailwind utilities:

1. Define Component Prop Interfaces: Create TypeScript interfaces extending standard HTML attributes (`interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement>`). Include variant and size prop options.

2. Compose Base Utilities: Define base layout, typography, and focus styles directly on the element markup (`inline-flex items-center justify-center font-medium rounded-xl transition-colors focus-visible:ring-2`).

3. Merge Dynamic Class Overrides: Use the `cn()` utility helper to combine base utilities, variant conditions, and external `className` overrides seamlessly without utility collisions.

## Common mistake: The Overuse of @apply as a Traditional CSS Crutch

A common mistake when transitioning to Tailwind is creating external `.btn` or `.card` classes in CSS filled with `@apply` directives for every UI element.

This anti-pattern reintroduces named CSS classes and stylesheet fragmentation without providing component abstraction benefits.

Reserve `@apply` only for third-party CSS library overrides or small HTML resets. Use React components for UI component abstraction.

## Tip: Accepting Custom className Props in Reusable Components

Always accept an optional `className` prop in reusable components and pass it into `cn()` as the final argument.

This allows component consumers to customize spacing (`className='mt-4'`) or layout alignment without breaking the component's internal design system defaults.

## Try it: Component Extraction vs @apply Comparison

Compare how React component extraction keeps utility styles composable and lightweight versus external CSS @apply directives.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/tailwind/extracting-components

## Check yourself

### Question 1

Why does extracting styles via React components preserve CSS bundle size better than using @apply across multiple custom CSS classes?

- A. React components use atomic utilities that appear once in CSS, whereas @apply duplicates CSS properties inside each custom selector
- B. CSS @apply disables browser caching for external stylesheets
- C. React automatically strips unused @apply directives at runtime

Correct answer: A

Utility classes generate each CSS rule once in the compiled stylesheet. Using @apply copies raw property declarations into every custom CSS class selector, inflating output bundle size.

- Why not B: Browser caching is unaffected by @apply; bundle size inflation occurs due to CSS property duplication.
- Why not C: React does not modify or compile CSS stylesheets at runtime.

### Question 2

What happens when you attempt to override a property inline on an element styled with a custom @apply class (e.g. <button className='my-btn bg-red-500'>)?

- A. The override may fail because @apply utilities take on the specificity of the custom .my-btn CSS selector
- B. Tailwind throws a build error whenever custom classes are mixed with utilities
- C. Inline utilities always override @apply classes regardless of CSS selector specificity

Correct answer: A

Utilities applied inside custom CSS selectors inherit the specificity of that custom selector, breaking utility-first specificity predictability.

- Why not B: Tailwind allows mixing classes, but specificity conflicts cause silent visual bugs.
- Why not C: Custom CSS selectors can have higher specificity than single utility classes, causing inline overrides to fail.

### Question 3

What is the best practice for accepting styling overrides in a reusable React component?

- A. Accept a className prop and merge it using the cn() helper as the last argument
- B. Pass style objects via the inline style attribute
- C. Hardcode all utility classes without accepting a className prop

Correct answer: A

Accepting a className prop and merging it with cn() allows callers to override layout or spacing while maintaining component defaults.

- Why not B: Inline styles bypass Tailwind utilities and break design system constraints.
- Why not C: Hardcoding prevents consumers from positioning or tweaking the component in context.

## Remember this

- Prefer React component boundaries (<Button>, <Card>) over CSS @apply directives for style reuse.
- @apply duplicates CSS declarations in output stylesheets and breaks utility specificity predictability.
- React components preserve full utility variant power (hover:, focus:, md:) directly in JSX markup.
- Always accept an optional className prop in reusable components and merge it via the cn() helper.
