At GumGum, my team builds and works on many React JS web applications. As the number of apps grew, we found ourselves writing the same code and components for each app. This is not ideal because we had to spend time writing similar code, and it was not centralized, so each variation might have slight differences.

Michele
Michele
Aug 3, 2017 · 4 min read

A few things came together at once for our team:

  1. Our UI team created a design system that allowed us to use the same UI styles across all apps.
  2. We liked the concept of our BEM style library, but found it verbose to add so many class names on everything.
  3. We kept getting more projects quickly, so we needed to streamline our development process for fast prototyping.
  4. My team discovered React Storybook, a development environment for React components.

It all made sense — let’s create a shared component library!

  • We can import these components into each project.
  • Code is only developed one time in one place.
  • We can abstract the lengthy class names away.
  • We can use Storybook to self document our library and provide examples for use.
  • As a team, we can all contribute to this project and develop best practices. And the response has been great! We’re even working across teams to build this together.

Our component library

The first thing we need here is the library. In a new project, we made a components folder and started with our most basic components.

Here is a stripped down component example. Our badge takes in text to display in the badge and context, which is the color applied to the badge via our design system. We also apply some standard props to every component: className, style, and …otherProps, which allows you to pass in any other classes, styles, or props that you want — making your component very flexible.

Storybook

Now that we have our library, let’s set up Storybook so we can document and display our components. Storybook has great documentation and is very customizable with a variety of Addons. Out of the box, you can use Storybook as a way to show and interact with your components. We also wanted to use our Storybook to provide complete documentation for using our library, and allow users to see all of the options available via props. Therefore, we customized our Storybook with the following Addons:

  • Options — allows you to customize a few Storybook options. We changed the default name to “GumGum Storybook”, added the url, and put the addons on the right side instead of the default bottom.
  • Readme — allows you to write documentation in markdown format as a readme file. We used to have all of our documentation in the storybook files, but were limited because not all markdown was supported. Our files also started getting long and disorganized. Keeping all documentation in its own markdown file makes it really easy to write the documentation. We now organize our storybook with a folder for each component, containing a README.md and index.js:

/_stories

/Badge

index.js

README.md

/Button

index.js

README.md

  • Knobs — makes your props into actionable items. For example, in our Badge component, we have a “context” prop. There are a number of options, including [“default”, “primary”, “secondary”, “success”, “info”, “warning”, “danger”]. By using knobs, we can pass all of these options to our Storybook and the user sees a select that allows them to dynamically toggle any of the options. This is really powerful because someone can play with the component and see all of the options without needing to build anything.
  • Info — displays your story source and displays the component’s propTypes and defaultProps. In conjunction with “Knobs”, this gives the user the ability to change the props to what they need and then copy the story source into their project.
  • Actions — Some of your components may be more complex and track actions. This Addon shows the user when an action is taken, so you know the component is properly hooked up.

To start, run this command in your project and then follow the steps on Storybook docs:

yarn i — save-dev @storybook/react

Storybook also provides a command to export your Storybook as a static app. This is helpful if you’d like to host it without a server.

And finally, we needed to add a link to our custom css file for our design system. This goes in .storybook/preview-head.html

Exporting your library

For our purposes, since we’re using this library internally, we only needed to be concerned about exporting ES6 components.

First, we have a single index.js file that exports each component.

You’ll need a few packages and settings depending on your set up on the projects that will import it.

Our .babelrc:

To handle the actual build tasks, we added a few scripts in our package.json:

Postinstall lets us automatically build our library as es6 modules when the library is added to a project. This way we don’t have to build the files and keep them as part of our library. To deal with dependencies on our other projects, we use postinstall-build’s — only-as-dependency to grab any dependencies we need to build our library and remove them after they are built.

Importing your library into other apps

Finally, we need to import our library into our other React apps to use the components.

We aren’t publishing this on NPM, so when adding the library to a project, we can use this command:

yarn add git+ssh://git@bitbucket.org/YOUR_PROJECT.git#0.1.0

(We do releases and use versioning for this project. Highly recommended!)

And then in a component file, you can import components from your library:

import { Badge } from ‘YOUR-LIBRARY’’;

Enjoy your new library! Special thanks to the GumGum Web Engineering team for their continuing support and contributing to this project. Check out our library at storybook.gumgum.com.


We’re always looking for new talent! View jobs.

Follow us: Facebook | Twitter | | Linkedin | Instagram

gumgum-tech

Thoughts from the GumGum tech team

Michele

Written by

Michele

Hello, I’m a JavaScript Engineering Manager in Santa Monica, CA && https://michelelarson.com

gumgum-tech

Thoughts from the GumGum tech team

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade