Smoothly Inclusive Component Library Documentation

A presentation at React Summit in October 2020 in by Kathleen McMahon

Slide 1

Slide 1

Hi, my name is Kathleen McMahon and I’m here today to tell you how Gatsby and MDX makes component library documentation… smoothly inclusive Before we begin, let’s get some details out of the way

Slide 2

Slide 2

My slide deck will be posted on Notist, including links to resources I briefly touch upon. The full URL will be available later today on Twitter.

Slide 3

Slide 3

You can follow me at. Resource11 on Twitter, Instagram, and GitHub.
Before we dig into Gatsby and MDX, let’s back up so I can introduce myself better…

Slide 4

Slide 4

I’m a Principal Engineer at CarGurus

Slide 5

Slide 5

And I race bikes… very badly.

Slide 6

Slide 6

Mostly you’ll see me in costume, racing two laps to your six, at the back of the pack, on a singlespeed. Mostly. Unless something happens.

Slide 7

Slide 7

Like… a pandemic kicking in our doors. Then your racing season is postponed.

Slide 8

Slide 8

So while I’m an engineer, and a super slow bike racer…

Slide 9

Slide 9

…I’m a super fan of design systems — especially the component library parts!

Slide 10

Slide 10

Before working for CarGurus, I was the Tech Lead for O’Reilly Media’s design system.

While I was there, I learned a lot about streamlining component libraries, and optimizing documentation.

Slide 11

Slide 11

If you’ve never worked on a design system, let’s just say… There are a lot of moving parts.

Slide 12

Slide 12

And if your core team is small — and you’re rebooting your component library — you really have to choose your focus. And… rely on your contributors.

Slide 13

Slide 13

In our case, our focus was business logic out, accessibility in. We fixed our colors, our components, our patterns, and rebooted our docs.

Slide 14

Slide 14

Once that was done, we realized that a part of our system was becoming a hindrance for our team, and a barrier to entry for our contributors: our documentation.

Our process was spread across two projects: the design-system repo, and the design-system-docs repo.

Slide 15

Slide 15

In the design-system repo, the documentation content was stored…

Slide 16

Slide 16

…in two locations.

Slide 17

Slide 17

While the docs repo held the site scaffolding…

Slide 18

Slide 18

…and docs layout components.

Slide 19

Slide 19

To get up and running, you had to follow a detailed series of steps to sync, run scripts, grab files, parse information, generate data, and render the docs to a React app. Phew!

Slide 20

Slide 20

Just getting starting working with these repos was overwhelming for new contributors to say the least…

Slide 21

Slide 21

But wait there’s more!

The scripts were set up in such a way that you had to write your Markdown in a very specific order for your content to show up in the docs.

Slide 22

Slide 22

Your Button heading could be paired with an intro paragraph, but only one paragraph.

Slide 23

Slide 23

Same with your variants. Only one paragraph, and then a code block.

Slide 24

Slide 24

Best practices had to be written as an unordered list. Always.

Slide 25

Slide 25

Same with the Related components section. Always an unordered list.

Slide 26

Slide 26

So while our docs scripts were great for of generating color swatches and details about which props were available in components…

Slide 27

Slide 27

The process was not great for creating our component documentation.

Slide 28

Slide 28

Which frustrated everyone.

Slide 29

Slide 29

One mistake, and parts if not whole chunks of documentation would be missing on the component pages. We had the freedom of using markdown, yet weren’t taking advantage of it.

Slide 30

Slide 30

This was a serious cognitive lift, even for someone who is very familiar with the codebase

Slide 31

Slide 31

Now imagine if you have a new contributor to your project…? Not exactly a welcoming experience.

Slide 32

Slide 32

We decided to review our process and see what wasn’t working.

Slide 33

Slide 33

One of our biggest issues: we chose to show one component variant at a time, rather than all at once.

This forced users to access a select menu over and over to compare variants, which increased the time they needed to look up information. It didn’t make sense.

Slide 34

Slide 34

In retrospect, that decision was about as questionable as a recipe that combines Jello with shellfish

Slide 35

Slide 35

We decided that it was time to make our docs contribution process more user friendly, in terms of the structure, and how we authored.

Slide 36

Slide 36

Especially the component example pages. We wanted the users to have confidence that whatever content they wrote in a docs page

Slide 37

Slide 37

…would make it into that docs page.

Slide 38

Slide 38

If we made our docs more user friendly, we’d have more contributors rather than fewer.

Slide 39

Slide 39

We considered our options for making our docs better, and decided upon Gatsby for a few reasons.

Slide 40

Slide 40

Gatsby gave us a way to migrate all our main documentation pages right into the site without much fuss.

Gatsby provides a Webpack and Babel config similar to create-react-app, so we could start with a scaffolded project, and extend as needed. And…

Slide 41

Slide 41

Gatsby supports MDX, so we could write component documentation using Markdown, import React components right in the file, and it just works. Even better, MDX compiles down into…

Slide 42

Slide 42

Semantic HTML. Which in turn, gives the site…

Slide 43

Slide 43

Improved accessibility. Better support for users that rely on assistive technology to access the docs.

If a design system is promoting inclusivity in general — and it really should — it follows that inclusive docs makes sense.

Slide 44

Slide 44

As a bonus, with Gatsby we streamlined our setup from two repos to one, and…

Slide 45

Slide 45

Reduced the amount of CLI commands

Slide 46

Slide 46

Dramatically.

Slide 47

Slide 47

The default setup for Gatsby is great, however, we needed to make some adjustments so Gatsby worked for our needs.

Slide 48

Slide 48

In the gatsby-config file…

Slide 49

Slide 49

we added gatsby-plugin-postcss to support PostCSS…

Slide 50

Slide 50

gatsby-plugin-compile-es6-packages to support our design system package…

Slide 51

Slide 51

gatsby-plugin-mdx, so the site would recognize MDX files.

Slide 52

Slide 52

gatsby-remark-autolink-headers, to generate auto links for page headings for better screen reader and keyboard support…

Slide 53

Slide 53

gatsby-plugin-page-creator to create pages from the src/pages folder…

Slide 54

Slide 54

And gatsby-plugin-filesystem so we could pull in our data file, and…

Slide 55

Slide 55

…point Gatsby to the folder where all our component MDX files lived.

Slide 56

Slide 56

In the gatsby-node file…

Slide 57

Slide 57

we added a GraphQL query to find the component MDX content

Slide 58

Slide 58

And a createPage action to generate routes and pages for the component docs.

Slide 59

Slide 59

In the gatsby-browser file…

Slide 60

Slide 60

…we added things we wanted to apply throughout the site, like our global CSS styles and the wrap-root-element component.

Slide 61

Slide 61

The wrap-root-element component acts as a wrapper around the site’s root element…

Slide 62

Slide 62

…And imports MDXProvider. This way, the entire site would recognize MDX content.

Slide 63

Slide 63

We had to do one extra thing for our component docs page template, though.

Slide 64

Slide 64

Since our component MDX files were located outside of the src/pages folder, MDXRenderer needed to wrap the contents of our component page template. Otherwise, the MDXs file would render…

Slide 65

Slide 65

…like this in the browser.

Slide 66

Slide 66

Now that our layout template used MDXRenderer, our component MDX pages…

Slide 67

Slide 67

…would render as we want in the browser.

Slide 68

Slide 68

Now we could pop in React components next to the code blocks…

Slide 69

Slide 69

…And they would render, no problem.

Slide 70

Slide 70

We could even wrap a Button component with a DisplayBox component

Slide 71

Slide 71

And add a more cohesive look to the component/code block pairing

Slide 72

Slide 72

If we wanted to add a props table to show which props can be used with the component…

Slide 73

Slide 73

…and created a component that pulled in data to render as a table component…

Slide 74

Slide 74

…Popped that component into the docs MDX file, passed in the name of the component to render…

Slide 75

Slide 75

Voila! The docs would show a props table.

Slide 76

Slide 76

For the component Dos and Don’ts…

Slide 77

Slide 77

We used to have to write out an unordered list in the Markdown file.

Slide 78

Slide 78

And the list rendered like this.

Slide 79

Slide 79

With MDX support, we instead created a custom component that renders unordered lists.

Slide 80

Slide 80

We could pass in a dosList and a dontsList array into the component…

Slide 81

Slide 81

…and BOOM. A nicely-styled dos/don’ts list.

Slide 82

Slide 82

Code blocks. Markdown has great code block support. MDX makes those even more powerful, so we were psyched to add this feature.

You can override the styling of a code block in an MDX file with a custom code block, or even display different types of code blocks.

Slide 83

Slide 83

You can use prism-react-renderer in a custom CodeBlock component and import a theme with good color contrast…

Slide 84

Slide 84

…wrap your code block and theme it…

Slide 85

Slide 85

…Import that into your wrap-root-element along with the preToCodeBlock util…

Slide 86

Slide 86

…And override the default pre styling at the app level by passing it in as a component to the wrapRootElement.

Slide 87

Slide 87

So now when you write a code block in MDX…

Slide 88

Slide 88

Instead of the baseline styling…

Slide 89

Slide 89

…the custom code block will render with syntax highlighting.

Slide 90

Slide 90

Fun fact: you can pass in a prop to that code block and do really cool things if you use custom code blocks!

Slide 91

Slide 91

For example, if you abstract this MDX into a DRYer pattern into our custom CodeBlock component…

Slide 92

Slide 92

… and pass the displaybox prop into the CodeBlock…

Slide 93

Slide 93

If you add the displaybox prop to the code block in MDX…

Slide 94

Slide 94

You’ll get a display box paired with a code block.

What about if you want your code block to be an editable code block?

Slide 95

Slide 95

You can add a react-live option in the component.

Slide 96

Slide 96

…and show component inside a displayBox pairing with an editable code block…

Slide 97

Slide 97

… so if you add the react-live prop to your MDX code block…

Slide 98

Slide 98

…You’ll get an editable block, and see your changes in real time.

Slide 99

Slide 99

… So if we write something like this… this is what you see in the app.

[open live demo]

Here’s a side-by-side example of the three options, the live editable code block is the top block here.

Slide 100

Slide 100

So to wrap up…

Slide 101

Slide 101

If your documentation setup is brittle, your contributors will flee…

Slide 102

Slide 102

But Gatsby…

Slide 103

Slide 103

And MDX…

Slide 104

Slide 104

…Helps make your docs inclusively smooth in both setup and execution.

Slide 105

Slide 105

Then your contributors will return…

Slide 106

Slide 106

…and will be happy to be part of your team.

Slide 107

Slide 107

Thank you.

Slide 108

Slide 108

https://noti.st/resource11

Slide deck posted after the talk @resource11 Twitter | Instagram | GitHub