Frontend Style Guide Miniseries: Part Three: Pattern Lab
Pattern Lab: Atomic Design + Living Styleguide
Background
Automated (or “living”) style guides are a popular topic in frontend development these days. And it’s no surprise, as they benefit the integrity of the project as well as ease design and development (as you can see in Part 1 of this miniseries!). Four Kitchens has a long history of using static style guides within projects, but as the frontend team re-evaluated our process this year, we wanted to standardize around an automated styleguide for future projects. By automating this part of our process, we ensure the style guide always reflects the latest design and code during the entire life of the project.
We began by evaluating existing automated style guide tools and narrowed the selection down to a couple that made sense alongside our existing toolset: KSS and Pattern Lab. We then committed to testing these in real projects to expose strengths/weaknesses so that we could eventually choose the best tool for our needs. Randy discussed KSS in a previous article, and in this article we will explore Pattern Lab.
Pattern Lab & Atomic Design
Pattern Lab is one of the more established style guide tools out there and is the brainchild of Brad Frost, best known for his “atomic design” principles. When evaluating Pattern Lab, it’s best to start by understanding atomic design.
Atomic Design
Put simply, atomic design just asserts that like organic matter, design systems can be broken down into their smallest pieces and built up from there. In web development, this means we shift from the mentality of building “pages” to breaking down designs into patterns, organized smallest to largest, and use these building-block patterns to develop the project. Here are the categories commonly used in this approach:
- Atoms: simple HTML tags (e.g.,
<button>
,<input type="text" />
,</button><br><h1>
,<a>
,</a></h1>
) - Molecules: small combinations of atoms (search form, menu, lists)
- Organisms: groups of molecules forming a distinct design section (header, masthead, footer)
- Templates: organisms combined to form contextualized designs
- Pages: fully actualized templates often with real content
There is a video from the Pattern Lab website that demonstrates this best. Some folks get distracted by the lingo (atoms, molecules, etc.), but you should see these naming conventions as only one way to break down components into a logical structure. Pattern Lab actually allows you to use any category names you want. Pattern Lab does, however, expect you to use atomic design in that it wants you to organize patterns smallest to largest regardless of the category names.
Pattern Lab
On the technical side, Pattern Lab is a static site generator powered by either PHP or Node that supports Mustache and Twig templating languages. The Node version has Grunt and gulp support as well. Importantly, Pattern Lab is open-source and actively maintained.
In terms of built-in perks, Pattern Lab not only ships with a nice stock UI, it allows you to populate pattern data using JSON or YAML and then annotate the patterns using Markdown. It also provides a way to easily create pattern variations as well as pattern states (e.g., in progress, needs review, approved). It also ships with a pattern searching tool and a viewport resizer in the toolbar to easily test/demo your components across any screen size.
Building Patterns in Pattern Lab
Patterns are small chunks of HTML that may also contain CSS and/or JavaScript. In other words, there are no technical hurdles for a current Frontend developer to build these components—only the mental shift in breaking a design down to its logical atomic design parts.
Let’s take a look at building a simple button component. We’ll be using Twig as our templating language.
The button component is comprised of a file with the button’s markup (button.twig):
<a href="{{ url }}" class="button{% if variation %}--{{ variation }}{% endif %}">{{ text }}</a>
and a stylesheet containing the component styles (button.scss)
a.button { background-color:#35AA4E; border:none; color:#fff; cursor:pointer; font-size:100%; padding:1em 2em; text-transform:uppercase; &:hover { background-color:#eee; color:#35AA4E; text-decoration:underline; } &--alt { @extend .button; font-size: 80%; padding: .5em 1em; }}
To take full advantage of Pattern Lab, let’s also create some default data (button text and URL) and some annotations to help describe the component. For the data, let’s create a simple button.yml file:
url: "/"text: "Default Button"
This is what will populate the Twig variables in our markup above. And now let’s create an informative annotation that will display in the style guide. For this, we’ll create a Markdown file (button.md):
---el: ".button"title: "Default button"---Here we can see the default button styling in action.
This all shows up in Pattern Lab like this:
As you can see, we have our component name, followed by our annotations with code snippets in both Twig and HTML versions (another Pattern Lab perk) and then we have the design element itself, our button.
Let’s now add an alternative button option. It’s as simple as adding an alternative YML file (button~alternative.yml). The tilde character tells Pattern Lab this is a variation, and Pattern Lab uses the name after the tilde as the variation name. Let’s add this content to the file:
url: "/"text: "Alternate Button"variation: "alt"
You may have noticed that button.twig contained a check for a variation
variable that added the variation as a modifier class (class="button{% if variation %}--{{ variation }}{% endif %}"
). This alternate YML file supplies that variable, which means our template will change the class accordingly. This is what Pattern lab looks like now:
As you can see, Pattern Lab makes it quick and painless to create components with variations and metadata. From here, Pattern Lab also makes it easy to build nested patterns and to link patterns to one another to form a full, working prototype.
Final Thoughts
Adopting any new technology has its pain points, and it is of course no different with Pattern Lab. The latest version of Pattern Lab (v2) overcame our frontend team’s strongest critiques, namely that Twig is now natively supported and data can be specified in YAML format. I personally also like that annotations can now be written in Markdown, as it is a great format for this type of notation. My only remaining critique is that while writing components comes easily, debugging or tweaking core Pattern Lab does take some effort as the codebase is fairly large. This critique, for me, is far outweighed by all the built-in perks I mentioned above but I thought it worth mentioning.
Overall, I would argue Pattern Lab is one of the strongest contenders on the market for creating an automated styleguide. If you would like to learn more, consider reading through the documentation on their website or jumping into the codebase. Mostly, I would recommend downloading and installing Pattern Lab, as it’s the most rewarding way to learn atomic design while building an automated styleguide.
Stay tuned next week for the thrilling conclusion of our Frontend Style Guide Miniseries!
The post Frontend Style Guide Miniseries: Part Three: Pattern Lab appeared first on Four Kitchens.