One of the developments that has began to change the way we style our applications and sites is the introduction of utility classes. Tailwind is an example of a popular framework that has adopted the approach.
To learn more about the approach, I am interviewing Chad Donohue about his library called Benefit. It's designed particularly React developers in mind and makes Tailwind a perfect fit with React.
My name is Chad Donohue. I enjoy creating user experiences and talking about design systems. I've written computer software as a Full Stack Engineer for a little over ten years. When I'm not in front of a computer screen, I spend time with my beautiful wife and three amazing kids.
benefit is a Tailwind-compatible utility CSS library powered by emotion. It is framework agnostic, has a file size of 5kB
, and it only ships the CSS styles that you use.
Here we have a Button
component:
import React from "react";
export default function Button() {
return <button>Click Me</button>;
}
We'll add a few lines to include benefit and add some Tailwind class names:
/** @jsx jsx */
import { jsx } from "benefit/react";
export default function Button() {
return (
<button
className="
px-8
py-2
bg-blue-600
text-white
font-bold
tracking-wide
uppercase
rounded-full
border-2
border-blue-700
shadow-lg"
>
Click Me
</button>
);
}
By adding two lines and some additional class names, we have accomplished two things:
~10,000
) at just a 5kB
inclusion cost~350 bytes
Check out this CodeSandbox to explore this example more.
At the point of inclusion within your project, benefit takes its default configuration (or your own if you need to customize it), then it generates CSS declarations in memory.
As you use these generated class names in your markup, the styles are looked up in benefit's cache, auto prefixed, and injected into the document.
On the client, benefit generates and injects styles for class names only when they are used. On the server, benefit pairs with emotion’s built-in SSR support and inlines CSS with the markup.
Since benefit is powered by emotion in both scenarios, you also can tap into the power that it provides, like nested declarations and deterministic style composition.
Also, being framework agnostic, benefit can be used alongside any JS framework. It can be introduced at the component level or at the root of an application. And, dead-style elimination is built-in.
I help build and ship 3rd party components. It is for sure an edge-case, but it brought on problems to solve for:
margin
, padding
, etc.) and sandbox the elements that made up our shipped components and not have to duplicate those normalized styles with every new component.benefit started as an internal idea to solve these issues and has been through a few iterations. As it matured a bit more, we began to see how this could be a solution for both isolated components and full-blown sites alike.
We are working to remove the runtime altogether for SSR. Soon, we'll have some examples put together for how this would work with something like Next.js.
We're also working on the way to generate custom documentation based on a configuration. So, it will be easy to share visually what different benefit configurations look and behave.
As digital experiences increase in complexity, we have more of a responsibility as makers to take a look at what we are shipping to the end user. In the future, I see this getting better through the use of code-splitting and rendering on the server before shipping to the browser.
The use of utility classes for styling will continue to gain popularity thanks to the great work over at Tailwind. Utility classes are a great pattern that DRYs up a lot of the view layer. I'm not saying that every page/application will only have utility classes, but the individual one-off styling needs will go down considerably.
CSS Utility Classes and "Separation of Concerns" by Adam Wathan is an excellent read that talks about some of the benefits to be gained from styling with utility classes.
Make it a goal to learn something new every day and share your knowledge with others. This industry moves fast, and it helps tremendously to be able to step out of your comfort zone often.
It is a gratifying profession that allows you to produce your best work while simultaneously learning something.
Thank you for your time and interest! I've enjoyed sharing my thoughts here and am always around on Twitter and GitHub. Ask me anything 😄!
Thanks for the interview, Chad! I can immediately see how adopting the utility class approach would help my in my daily development and I might have a project in mind that's a perfect fit for it!
To learn more about benefit, check out the homepage for more examples and star the project on GitHub.