Notes on Atomic Design

Our components employ the concept of atomic design, meaning that we break them down into atoms, molecules, and organisms, each successive level being more complex than the previous. (We do not currently use the template or page concepts as described in Brad Frost's seminal article introducing atomic design).

Our components are composed (on the front-end) of HTML, Less, and JavaScript. If a component doesn’t have user interactions or require styling, then it won’t have an associated JS and/or Less file. Components that are available for adding to a Wagtail page also require some Python programming—see the creating and editing components page for details.

We compose our atomic components as follows:

Atoms

The smallest kind of component. May not contain any other components. Prefixed with a- in class names.

HTML

<div class="a-tag">Tag label {{ svg_icon('error') }}</div>

Less

.a-tag {
    cursor: default;
    display: inline-block;
    padding: 5px 10px;
    …
}

JavaScript

None of our atoms require any JavaScript at this time.

Molecules

The medium-sized component. May contain atoms. Prefixed with m- in class names.

HTML

<div
  class="m-notification
         m-notification__visible
         m-notification__error"
>
  {{ svg_icon('error') }}
  <div class="m-notification_content" role="alert">
    <div class="h4 m-notification_message">Page not found.</div>
  </div>
</div>

Less

.m-notification {
    display: none;
    position: relative;
    padding: @notification-padding__px;
    …
}

JavaScript

const BASE_CLASS = 'm-notification';
function Notification( element ) {
  // Constants for the state of this Notification.
  const SUCCESS = 'success';
  const WARNING = 'warning';
  const ERROR = 'error';

  // Constants for the Notification modifiers.
  const MODIFIER_VISIBLE = BASE_CLASS + '__visible';
  const _dom = atomicHelpers.checkDom( element, BASE_CLASS );
  const _contentDom = _dom.querySelector( '.' + BASE_CLASS + '_content' );
  …
}

The Notification molecule can be instantiated by adding the following to your project's JavaScript code:

const notification = new Notification(_dom);
notification.init();

Organisms

The largest component. May contain atoms, molecules, or (if no other solution is viable) other organisms. Prefixed with o- in class names.

HTML

<div
  class="o-expandable
         o-expandable__padded
         o-expandable__midtone"
>
  <button class="o-expandable_header">
    <div class="o-expandable_label">…</div>
  </button>
</div>

Less

.o-expandable {
    position: relative;

    &_header {
        padding: 0;
        border: 0;
        …
    }
    …
}

JavaScript

const BASE_CLASS = 'o-expandable';
function Expandable( element ) {
  // Bitwise flags for the state of this Expandable.
  const COLLAPSED = 0;
  const COLLAPSING = 1;
  const EXPANDING = 2;
  const EXPANDED = 3;

  // The Expandable element will directly be the Expandable
  // when used in an ExpandableGroup, otherwise it can be the parent container.
  const _dom = atomicHelpers.checkDom( element, BASE_CLASS );
  const _target = _dom.querySelector( '.' + BASE_CLASS + '_header' );
  const _content = _dom.querySelector( '.' + BASE_CLASS + '_content' );
  …
}

The Expandable organism can be instantiated by adding the following to your project's JavaScript code:

const expandable = new Expandable(_dom.querySelector('.o-expandable'));
expandable.init(_expandable.EXPANDED);

or

import { instantiateAll } from '@cfpb/cfpb-atomic-component';
import { Expandable } from '@cfpb/cfpb-expandables';
instantiateAll('.o-expandable', Expandable);

Folder structure

Our atomic components are separated and named based on asset type. HTML, Less, and JavaScript for each component are in separate directories.

HTML

consumerfinance.gov/cfgov/v1/jinja2/v1/includes/atoms/
consumerfinance.gov/cfgov/v1/jinja2/v1/includes/molecules/
consumerfinance.gov/cfgov/v1/jinja2/v1/includes/organisms/

Note

Some of our foundational components get their Less and JavaScript from the Design System, but the HTML for their Wagtail block templates is stored in the above folders.

CSS

consumerfinance.gov/cfgov/unprocessed/css/atoms/
consumerfinance.gov/cfgov/unprocessed/css/molecules/
consumerfinance.gov/cfgov/unprocessed/css/organisms/

JavaScript

consumerfinance.gov/cfgov/unprocessed/js/molecules/
consumerfinance.gov/cfgov/unprocessed/js/organisms/

Tests

consumerfinance.gov/test/unit_tests/js/molecules/
consumerfinance.gov/test/unit_tests/js/organisms/

JavaScript architecture

JavaScript components are built to be rendered on the server and then enhanced via JavaScript on the client. The basic interface for the components is as follows:

function AtomicComponent(domElement) {
  // Ensure the passed in Element is in the DOM.
  // Query and store references to sub-elements.
  // Instantiate child atomic components.
  // Bind necessary events for referenced DOM elements.
  // Perform other initialization related tasks.
  this.init = function init() {};

  // General teardown function
  // We don't remove the element from the DOM so
  // we need to unbind the events.
  this.destroy = function destroy() {};
}

We generally favor composition over inheritance. You can get more information by reading the following:

Component build pipeline

Routes

Routes are used to serve JavaScript bundles to the browser based on the requested URL or Wagtail page's Media definition. This happens via code contained in v1/layouts/base.html. This file serves as the base HTML template for serving Wagtail pages.

Wagtail page Media class

Each atomic component has a Media class that lists the JavaScript files that should be loaded via base.html. When a page is requested via the browser, code contained in base.html will loop all atomic components for the requested page and load the appropriate atomic JavaScript bundles.

Here is an example of the Media class on a component, the EmailSignUp organism:

class Media:
    js = ['email-signup.js']

This will load the email-signup.js script on any page that includes the EmailSignUp organism in one of its StreamFields.