Sharing React Components: From Atoms and Molecules to Pages

Sharing components of all scales to optimize frontend collaboration.

Image by Elchinator from Pixabay

Atomic Design

The terms “atoms”, “molecules” and “pages” come from Brad Frost's seminal work “Atomic Design”. In his book, he encourages us to think of our user interface as a hierarchical structure made of independent modules, arranged by role and structure — from the simplest component, the atom, to an entire page.

Atomic components are components that cannot be broken down into smaller pieces (and if they do, they become completely useless). Atoms can be button elements, inputs, labels, etc. Molecules are relatively simple groups of UI elements functioning together as a single unit. For example, a form label, search input, and a button that forms a search form molecule. As mentioned earlier, this hierarchy continues all the way to full pages.

Complexity and Reusability

The “Atomic Design” hierarchy correlates with levels of abstraction and reusability. A page, for instance, is a very concrete entity. It is not reusable and will not appear more than once in an app. Simpler components, atoms, molecules, and even organisms, may very well appear numerous times in a single app.

This continuum of reusability is tremendously important for organizations looking to build a design system or a component library.

Sharing Components as a Means for Collaboration between Independent Teams

We traditionally share simple components, atoms and molecules, to maintain a consistent UI but there’s no good reason why we shouldn’t use similar methodologies to enable collaboration between independent teams. We can collaborate in a way that resembles micro frontends, to enable teams to independently deliver large component compositions — organisms, templates, and even pages.

Enough talking, let’s code!

For this demonstration, I will be using Bit (Github). Bit is a tool that enables you to isolate components and compositions from any codebase and share them to a component collection in bit.dev.

As a short POC, I will isolate and share components of different scales. I will then install an organism, the toDoList in a new React project.

To follow this POC, clone this repo:



$ cd basic-todo-app

$ yarn $ git clone https://github.com/giteden/basic-todo-app $ cd basic-todo-app$ yarn

Export Components

Install Bit CLI:

$ yarn global add bit-bin

Log in:

$ bit login

Initialize a workspace:

$ bit init --package-manager yarn

Track all the app’s components (located in the ‘components’ library):

$ bit add src/components/*

My repo is no longer versioned solely as meaningless code. It now has an additional semantic layer where code is versioned as components.

Let’s check if there are any dependency graph issues that need to be resolved:

$ bit status new components

(use "bit tag --all [version]" to lock a version with all your changes) > button ... ok

> h1 ... ok

> list ... ok

> removable-list-item ... ok

> text-input ... ok

> to-do-list ... ok

All good :)

Configure a compiler for the shared components so that they could be used in other environments (with different build configurations).

$ bit import bit.envs/compilers/react --compiler the following component environments were installed

- bit.envs/compilers/react@1.0.5

Tag the components to build them in an isolated environment and lock changes to version.

$ bit tag --all 1.0.0 6 component(s) tagged

(use "bit export [collection]" to push these components to a remote")

(use "bit untag" to unstage versions) new components

(first version for components)

> button@1.0.0

> h1@1.0.0

> list@1.0.0

> removable-list-item@1.0.0

> text-input@1.0.0

> to-do-list@1.0.0

Export the tracked components:

$ bit export <username>.basic-todo-app

For example:

$ bit export eden.basic-todo-app

The components are now available in my (or yours) basic-demo-app collection in bit.dev.

Install a Component in a New Project

Let’s install the entire to-do-list in a new project:

$ npx create-react-app new-project

$ cd new-project

$ yarn add @bit/eden.basic-todo-app.to-do-list

Let’s add the package to our app:

import React from 'react';

import './App.css';

import ToDoList from '@bit/eden.basic-todo-app.to-do-list'; function App() {

return ( <div className="App">

<h1>New Project</h1>

<ToDoList />

</div> )

} export default App;

A ToDoList organism installed in a new project

Conclusion

That was a very basic POC but you can easily imagine how using the same methodology will enable multiple teams to deliver features independently, with a single team composing it all into one complete app (using $ bit list --outdated the composing team is able to identify components that have newer versions)

Learn More