How to Use IBM’s Carbon Design System (React) to Create a Quick UI

Will Jones
5 min readJun 10, 2021

--

By Will Jones, Engineer & Cofounder SocioProphet

Image: IBM — https://www.carbondesignsystem.com/

When you get to building your web application’s UI, it can be handy to work with a design system. This allows you to rather effortlessly create quick and responsive interfaces, that generally have properties (props) defined in them that allow you to also generate your UX. An extremely popular design framework is Google’s Material UI. However, another not as well known, but nonetheless full fledged design system is IBM’s design system, Carbon.

What is the Carbon Design System?

From the Carbon homepage,

Carbon is IBM’s open source design system for products and digital experiences. With the IBM Design Language as its foundation, the system consists of working code, design tools and resources, human interface guidelines, and a vibrant community of contributors.

The system is built for developers to use either React, Angular, or Vue. The site carbondesignsystem.com has all their components completely open source, design guidelines, color palette examples, and tutorials. The IBM color theme which primarily incorporates offset whites, multiple shades of blue, and of course carbon black, was the inspiration for SocioProphet’s design theme.

Image: IBM — https://www.carbondesignsystem.com/

While SocioProphet no longer uses any Carbon components, or any part of the Carbon Design System, it was an extremely useful starting point. So, I’ll explain how to get started using Carbon in you React project, and give an example of the simple interface we created to begin with!

Installing Carbon

I use yarn as my package manager, however, the following works with npm.

There are three things that need to be installed to get started, those are;

  1. Component styles — ‘carbon-components’
  2. React components — ‘carbon-components-react’
  3. React icons (for the components) — ‘@carbon/icons-react;

So, in your project (or globally if you prefer),

yarn add carbon-components carbon-components-react @carbon/icons-react carbon-icons

Carbon styles uses Sass, which is a CSS preprocessor that gives you the ability to ‘program’ CSS. This includes things like variables, inheritance, etc. Essentially Sass is a CSS superset. Anyway, we need to install that as well.

yarn add sass

In you React project, you will typically have the following structure;

Typical React app structure (not using CRA)

After installing Sass and the three Carbon packages, your package.json should look similar to this,

You will also need to change App.css extension to App.scss and make sure you change the import in App.js to include App.scss.

If you used ‘npx create-react-app’ to begin your application, you won’t need to worry about the following additions. However, if you are configuring Webpack from scratch, you’ll need to add a few lines and another package.

yarn add sass-loader

sass-loader allows Webpack to load and compile Sass (.scss) files.

For your Webpack configuration, add the sass-loader to the module rules,

module: {
rules: [

...
{
test: /\.(s*)css$/,
use: ['style-loader, 'css-loader', 'sass-loader'],
},
... ],
},

Now that everything is set up and installed, let’s move on to using Carbon!

Importing Components and Styles

Styles

First of all, in your index.scss add the following import,

@import 'carbon-components/scss/globals/scss/styles.scss';

This import alone will apply the Carbon font family ‘IBM Plex Sans’, font sizes, weight, colors, margins and padding (along with a few other CSS values).

The import goes in the index.scss file in order to reduce the amount of re-compiling Sass will do. We want to modify our app level styles, such as App.scss as this will lead to quicker compile times.

Components

A common component you might use is a button, and of course, there is a Carbon button.

At the top of your App.js file (or whatever component will need a button), add

import { Button } from 'carbon-components-react';

Then, inside the App component add,

<Button>This is a Carbon Button!</Button>

So far, App.js looks like,

import React from 'react';import { Button } from 'carbon-components-react';import './App.scss'const App = () => {    return (        <React.Fragment>

<Button>This is a Carbon Button!</Button>
</React.Fragment>
);
};export default App;

That’s it! That’s the basically all the important stuff.

It is good practice to only import what we need. To get a full understanding of how all the Carbon components are organised, you can visit the GitHub for Carbon Design System.

For example, the ‘Header’ component is under ‘carbon-components-react/lib/components/UIShell’.

So the import would be,

import { Header } from 'carbon-components-react/lib/components/UIShell'.

Building a UI From the Carbon Design System

I’ll give an example of how we (SocioProphet) used Carbon to build a quick UI as a solid starting point.

Initial ‘concept’ UI Shell using Carbon Design System

As you can see from this dashboard, we wanted a simple header that contained the site name ‘SocioProphet’, some navigation links, such as ‘News & Events’, an app switcher icon, avatar icon, and a left side panel (which expands on mouseover).

The import for this was all from ‘carbon-components-react/lib/components/UIShell’.

import { 
HeaderContainer,
Header,
SkipToContent,
HeaderName,
HeaderNavigation,
HeaderMenuItem,
HeaderGlobalBar,
HeaderGlobalAction,
SideNav,
SideNavItems,
SideNavLink,
SideNavMenu,
SideNavMenuItem,
} from "carbon-components-react/lib/components/UIShell";

The full dashboard, including link components that can’t be seen in the image above is shown below in the gist.

This is about as simple as it gets, since all these components have many props that can be used to give your site functionality. The above file shows a quick mock-up UI that met our initial requirements.

Carbon Resources

Carbon Design System has three primary themes (all open source) that I am aware of, I’ll share the React Storybooks for those here.

Open Source Storybooks

  1. Standard — Carbon Components React v7.36.0 + GitHub
  2. Carbon for IBM Security v1.41.0-prerelease.2 + GitHub
  3. carbon-addons-iot-react + GitHub

№2 and №3 provide extensions of the classic Carbon components, and №3 is used in Watson IoT components.

For SocioProphet’s UI (a few iterations in) we were using a combination of components from all three Storybooks.

Further Note

The Carbon Homepage provides lots of great learning resources to get started. This article touches briefly on a couple of ‘getting started’ ideas but if you’re eager to learn more, be sure to check out the free tutorial and earn one of those IBM badges! 😃

Click Here for the Carbon React Tutorial

I hope this article was potentially helpful to one of your future projects! Check out my previous post on how to use a Makefile and Bash scripts to automate terminal commands when running a React application…

--

--

Will Jones
Will Jones

Written by Will Jones

Writing about the things I learn as a software engineer, front-end, back-end, and devops.

No responses yet