Frontend System Design · Lesson 70 of 86 · 5 min
Atomic Design for Component Libraries
This isn't just a folder structure; it's a mental model for composition that interviewers use to test your ability to build scalable UIs.
After this, you'll be able to break down any UI into a logical component hierarchy and explain the trade-offs of this approach in a system design interview. This is how you demonstrate you can think about frontend architecture, not just write components.
The Five Levels of Composition
Atomic Design gives you a five-level hierarchy for your components. The core idea is simple: start with the smallest possible pieces and compose them into larger, more complex structures.
-
Atoms: The indivisible building blocks. These are often just styled HTML elements. Think
Button,Input,Label,Icon. They have no business logic and are globally reusable. -
Molecules: Simple, functional groups of atoms. A
SearchFormmolecule might combine anInputatom and aButtonatom. It has one, and only one, job. It encapsulates a small piece of functionality. -
Organisms: More complex sections of an interface, composed of molecules and atoms. A
Headerorganism could contain aLogoatom, aNavigationmolecule, and aSearchFormmolecule. Organisms start to have more context and complexity. -
Templates: The layout or wireframe of a page. Templates arrange organisms into a specific structure. They are placeholders for content, defining the skeleton of a view but without any real data.
-
Pages: Specific instances of templates, filled with real content. This is where you see the final UI. It's the
ProductListingPagewith actual product data, not just placeholder grids.
How to Classify Components Without Overthinking It
The trap: Agonizing over whether a component is a "big molecule" or a "small organism." This is a common sign of inexperience. The labels are less important than the principle of composition and clear responsibility.
Use this heuristic: can it be broken down further and still be useful?
- If no, it's an Atom. A
Buttonis just a button. - If yes, and the combination performs a single, discrete function, it's a Molecule. A
SearchFormcombines an input and a button to perform a search. - If it's a major, distinct section of the page composed of multiple smaller components, it's an Organism. The
SiteHeaderis a good example.
The goal is a shared vocabulary for the team, not a perfect, dogmatic classification.
In an interview, when asked to break down a UI, start by identifying the smallest reusable pieces (atoms) and then show how you'd compose them. The process of demonstrating composition is more important than getting every label right. Explain why you're grouping certain elements—that's what the interviewer wants to hear.
Building a Composable Search Form
Let's build a SearchForm molecule from Input and Button atoms. This is a classic example that shows the power of composition. The atoms are "dumb" and only care about their own state and props. The molecule is "smart" and manages the state and logic that connect the atoms.
import React, { useState } from 'react';
// --- ATOMS ---
// A generic, reusable button atom
interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
variant?: 'primary' | 'secondary';
}
const Button: React.FC<ButtonProps> = ({ children, variant = 'primary', ...props }) => {
const baseClasses = "px-4 py-2 rounded font-bold";
const variantClasses = variant === 'primary' ? "bg-blue-500 text-white" : "bg-gray-300 text-black";
return (
<button className={`${baseClasses} ${variantClasses}`} {...props}>
{children}
</button>
);
};
// A generic, reusable input atom
type InputProps = React.InputHTMLAttributes<HTMLInputElement>;
const Input: React.FC<InputProps> = (props) => {
return <input className="px-4 py-2 border border-gray-300 rounded" {...props} />;
};
// --- MOLECULE ---
// A functional unit combining the atoms
interface SearchFormProps {
onSearch: (query: string) => void;
initialQuery?: string;
placeholder?: string;
}
export const SearchForm: React.FC<SearchFormProps> = ({ onSearch, initialQuery = '', placeholder = 'Search...' }) => {
const [query, setQuery] = useState(initialQuery);
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
onSearch(query);
};
return (
<form onSubmit={handleSubmit} className="flex items-center gap-2">
<Input
type="search"
value={query}
onChange={(e) => setQuery(e.target.value)}
placeholder={placeholder}
aria-label="Search query"
/>
<Button type="submit" variant="primary">
Search
</Button>
</form>
);
};
Notice the clear separation of concerns. The Button doesn't know it's part of a search form. The Input doesn't know what will happen to its value. The SearchForm molecule orchestrates them to perform a specific task. This is the reusability you're aiming for.
Where the Model Gets Blurry
Atomic Design is a mental model, not a strict law. In practice, the lines blur, especially in modern frontend development.
The distinction between Templates and Pages is often less relevant in frameworks like React, Vue, or Svelte. A page component (e.g., in a Next.js app/ directory) often handles both the layout (template) and the data fetching (page content) in one file. It's more useful to think of page-level components as containers that fetch data and pass it down to organisms.
The line between Molecules and Organisms is also subjective. Is a ProductCard with an image, title, price, and button a molecule or an organism? It depends on its complexity and context. If it's a key, self-contained part of your business logic, you might call it an organism. If it's just a simple grouping, a molecule might fit. Don't waste time on this. Pick one and be consistent.
The trap: Presenting this hierarchy as the only way to build UIs. Acknowledge its limitations. This shows you're a pragmatic engineer who adapts models to reality, not the other way around.
This diagram shows the ideal flow. Your job is to know when reality requires a shortcut.
How to Answer "Design a Component Library"
When asked to design a component library or a design system, use Atomic Design as your framework.
-
State the Goal: "My goal is to create a consistent, reusable, and maintainable set of components. I'd use a methodology like Atomic Design to structure them."
-
Start with the Atoms: "First, I'd define the atoms. These are the design tokens and basic elements: colors, typography, spacing, and components like
Button,Input,Icon. These would be highly configurable through props." -
Compose Molecules: "Next, I'd build molecules by combining atoms to handle simple, recurring patterns. For example, a
FormFieldmolecule would combine aLabel,Input, andErrorMessageatom. This encapsulates the logic for a single form field." -
Build Organisms for Context: "Then, I'd compose organisms for larger sections of the UI. A
LoginFormorganism would use severalFormFieldmolecules and aButtonatom, and it would manage the form's overall state and submission logic." -
Connect to Tooling: "I'd structure this in a monorepo (as we discussed in Monorepo Tooling: Nx and Lerna), with each component in its own package to encourage independent versioning and reuse."
This answer shows you have a structured approach. It moves from the abstract to the concrete and connects to other parts of the system design.
Try it
Take the header of a site you use daily (e.g., GitHub, dev.to). Sketch out its component tree using the five atomic levels. Identify the atoms (icons, links, avatar), molecules (search bar, notification indicator), and the main header organism. Don't write code; just map the hierarchy. This takes 15 minutes and builds the right mental muscle.
Next: We'll see how to make these components adaptable across different products or brands with a system for Theming and Customization in Design Systems.
Lock it in — apply this lesson under interview conditions.