Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 6

Advanced Component Design with Atomic Principles

Recommended resources

Welcome back. In our last lesson, we established a robust framework for analyzing and articulating architectural trade-offs, a cornerstone of senior-level engineering. We applied this framework to high-level decisions like rendering strategies and codebase architecture. Now, we'll apply that same critical thinking to the very atoms of our user interfaces: the components.

This lesson marks the beginning of our deep dive into "Scalable UI & Design System Architecture." Our goal is to move beyond simply building components that work and start designing component APIs that are scalable, maintainable, and a pleasure for other developers to consume. We will explore the "why" and "how" of component composition, starting with the Atomic Design philosophy and then diving into the powerful implementation patterns of Compound and Headless Components. Mastering these patterns is essential for building UI libraries that can serve dozens of teams and evolve gracefully over time.

The Problem: The Monolithic Component and "Prop Drilling" Hell

Before we jump into solutions, let's diagnose a common disease in large frontend codebases. As a component's requirements grow, it's tempting to add another boolean prop, another conditional render, another optional callback. This leads to monolithic components that are bloated, difficult to understand, and terrifying to modify.

The following video brilliantly illustrates this anti-pattern by dissecting a familiar, complex UI: the Slack composer.

Composition Is All You Need | Fernando Rojo at React Universe Conf 2025

Watch this segment from Fernando Rojo's talk "Composition Is All You Need." He walks through the painful evolution of a component controlled by an ever-growing list of props. This sets the stage perfectly for why we need a different approach.

Pay close attention to the section from the beginning up to the point where he shows the "nightmare" API of the composer, packed with boolean props like isThread, isEditingMessage, and so on. This is the problem we're about to solve.

The component you just saw violates the Open/Closed Principle: it's not open for extension but requires modification for new features. This is the path to technical debt and slow development velocity. A senior engineer's role is not just to avoid creating such components, but to have a clear strategy for refactoring them into a scalable architecture.


A Guiding Philosophy: Atomic Design

To build a scalable architecture, we first need a mental model for deconstructing UIs. Atomic Design, a methodology created by Brad Frost, provides an excellent one. It uses a chemistry analogy to describe building UIs from the smallest parts up to complete pages.

Atomic Design provides a clear methodology for crafting design systems. It visualizes the process of building from foundational "sub-atomic" parts like color and typography, up through atoms (buttons, inputs), molecules (a search field with a button), organisms (a header), templates, and finally, full pages.

The levels are:

  1. Atoms: The fundamental building blocks of our UI. Examples: a button, an input field, a label, a color variable.
  2. Molecules: Simple groups of atoms functioning together as a unit. Example: a search form molecule composed of a label atom, an input atom, and a button atom.
  3. Organisms: More complex UI components composed of molecules and/or atoms. Example: a site header organism that might contain a logo, a navigation molecule, and a search molecule.
  4. Templates: Page-level objects that place organisms into a layout. They are content-agnostic and focus on the page's structure.
  5. Pages: Specific instances of templates, with real representative content.

While you won't rigidly follow this in every project, internalizing this hierarchy is key. It trains you to think in terms of composition and reusability, which is the foundation for the patterns we'll discuss next.


Solution Part 1: Composition over Configuration

The core antidote to the "boolean prop hell" is to favor composition over configuration. Instead of configuring a monolithic component with props, we compose smaller, focused components together using the natural structure of JSX.

The video you watched earlier provided the perfect transition to this idea. Let's see how the Slack composer is rebuilt using this philosophy.

Composition Is All You Need | Fernando Rojo at React Universe Conf 2025

This next segment demonstrates the solution: breaking the monolithic composer into smaller, composable parts that are assembled declaratively.

Watch the section where he refactors the composer, starting from the ComposerProvider and building up the UI by rendering components directly instead of conditionally.

This powerful technique is often formalized as the Compound Component pattern.

The Compound Component Pattern

A compound component is a set of components that work together to manage a shared state and perform a collective task. The classic HTML example is the <select> and <option> element pair. The <select> doesn't know or care about what options it contains; you, the developer, compose them together.

This pattern gives the consumer maximum control over the structure while the components handle the complex internal logic. Let's dive into a fantastic article that breaks this down.

Advanced React component composition - Frontend Mastery

This guide by Rem is one of the best resources on this topic. It walks through the design principles and implementation trade-offs for building a Tabs component using the compound pattern.

First, read the introduction to composition-based APIs, which reinforces the <select> analogy. Next, focus on the section on Designing composable components. The "stable dependency principle" is a critical concept for senior engineers: encapsulate what is volatile (implementation details) and expose what is stable (the component API). Then, read about the two main challenges you'll face: Underlying problems to solve. Finally, and most importantly for our trade-off analysis skills, study the section that compares different approaches for solving the orchestration problem. Pay attention to the limitations of React.cloneElement and why React Context is often the superior choice.

As the article highlights, the key challenge is orchestration. How do the child components (Tab) communicate with the parent (TabsList)? While cloning elements is simple, it's brittle because it makes assumptions about the child tree. Render props offer more flexibility but can lead to more verbose APIs.

React Context provides the most elegant solution. The parent component acts as a Provider for a shared context, and the child components are Consumers of that context. This decouples the components; they don't need to know about each other directly, only about the shared context "contract".

Let's revisit the "Composition Is All You Need" video to see exactly how this state management piece works.

Composition Is All You Need | Fernando Rojo at React Universe Conf 2025

This final segment from the talk connects the compositional UI to the state management layer, demonstrating the power of React Context and state lifting.

Watch the part explaining how the Context provider defines the interface for state and actions. Then, see how he lifts the state by wrapping the entire feature in the provider, allowing even sibling components to access the composer's state.

This is a profound architectural insight. The Provider establishes a boundary for a feature's state, and any component within that boundary can interact with the state without messy prop drilling. It also allows you to swap out the entire state management logic (e.g., from local useState to a global library like Zustand or a server-state library like React Query) in one place—the provider—without touching any of the consumer components.


Solution Part 2: Headless Components

Headless components take composition a step further. If compound components give the consumer control over the structure, headless components give the consumer control over everything visual.

A headless component provides all the logic, state management, and accessibility hooks, but renders no UI by itself. It's the "brain" without the "face." This pattern has become incredibly popular for design systems because it achieves maximum reusability. The logic is written, tested, and maintained once, and different teams can apply any styling or markup they need.

Headless Component: a pattern for composing React UIs

This Martin Fowler article provides a clear definition and practical implementation of the headless pattern.

First, read the introductory section to understand the core concept. Next, see the most common modern implementation: encapsulating logic in a custom hook (useDropdown). This is a pattern you'll see everywhere. Then, skip ahead and see the power of this separation by reading how easy it is to apply a completely new UI (using Tailwind CSS) to the existing logic. Finally, review the section on real-world libraries that use this pattern, such as Headless UI, Radix, and React ARIA.

The rise of headless components has been a game-changer for frontend development, solving a major dilemma.

Why Unstyled Components are so popular - Explained!

This video explains the "why" behind the popularity of headless/unstyled libraries. It clarifies the trade-offs between using a fully-styled library like Material UI and building from scratch.

Watch the first section explaining the dilemma of component libraries. Then, see how unstyled libraries like Radix UI solve this by providing all the complex logic and accessibility work for you.

By providing battle-tested logic for state, interactions, and accessibility, libraries like Radix UI and Headless UI let you focus on what's unique to your application: the look and feel. This is a massive productivity boost and a huge win for creating high-quality, accessible user experiences without reinventing the wheel.


Conclusion

In this lesson, we've moved from the philosophical (Atomic Design) to the practical (Compound and Headless Components). You've learned to identify the "monolithic component" anti-pattern and now have a powerful set of compositional patterns to combat it.

Key Takeaways:

  • Embrace Composition over Configuration: Instead of controlling a component's rendering with boolean props, compose smaller components together using JSX.
  • Use Compound Components for Expressive APIs: When a group of components needs to work together (like Tabs or an Accordion), use the compound pattern with React Context to manage shared state implicitly.
  • Use Headless Components for Maximum Flexibility: When you need to reuse complex logic across different visual presentations, extract it into a custom hook or headless component. This separates the "brain" (logic) from the "face" (UI).
  • Leverage the Ecosystem: For common, complex components like dropdowns, dialogs, and menus, lean on production-ready headless libraries like Radix UI or Headless UI. They've already solved the hard problems of accessibility and edge-case behavior for you.

In our next lesson, we will zoom out again. Now that we know how to design scalable components, we'll discuss how to architect the codebase that contains them. We'll dive into the highly-debated topic of Monorepos vs. Polyrepos, analyzing the trade-offs and exploring modern tooling like Nx and Turborepo.