Coding Your Design System with Tailwind CSS
Stefan Kudla | | Tailwind
Save yourself some time
As I'm primarily using Tailwind in my React projects, I couldn't tell you a better recipe for building a UI than using swanky Tailwind CSS styled JSX gumbo.
But let's be honest with ourselves here, we don't have to write as much Tailwind as we might be writing. I've been using Tailwind and React for no more than five months, so I'm just learning as I go.
I've found that configuring your project initially will save you a lot of time and stress down the line. Initializing things like Prettier, ESLint, and Postcss before building any part of your UI is the way to go.
Following a style guide
Recently, I was assigned a coding assessment (how exciting). The coding assessment was to look at a mockup image of a simple landing page and build it. A style-guide was also provided to be followed.
Here's the guide:
This guide was simple enough, and I realized at that moment the importance of implementing this style guide into my code.
Coding the style guide
Create a globals.css file in your directory. I often have mine set up in a 'src' file under styles (/src/styles/globals.css). Let's start coding.
First, we see the desired accent and text colors that are to be used. We will set those using CSS custom variables. I have these defined in the ':root' so that they can be accessed globally as well as in the globals.css file itself.
We can use these root variables in our JSX as well as our coded design system.
Before we create any custom classes for things like buttons and horizontals, let's define the styles for all of the native HTML elements.
We're setting our @layer directive to define our base styles.With Tailwind CSS, there are three main "buckets" we can add a layer too.
@layer components - used for things styling entire components like "Nav" or "cta-btn".
@layer utilities - these are universal classes that are present everywhere throughout your code like "flex-col" "opac-half".
@layer base - the base layer of our styles, "h1", "h2", "p".
When writing the actual styles in our globals.css style sheet, we simply use '@apply' to start writing styles with Tailwind.
Now let's finish up coding this style guide by defining the component and utility classes.
Finding a good balance
Depending on how big your project is and how scalable it needs to be, finding the happy medium for coding your design system is crucial. While you want to set up your project with scalability in mind, you don't want to over engineer and take away from the on-demand benefits of Tailwind.
It is also important to consider how deep your design system (reference) goes and the complexity of your project. Maybe you have a fairly straight forward blog and you only need one or two layouts to achieve the desired design. You wouldn't really have an issue with setting up component and utility classes in your coded design system.
Then again, you probably wouldn't have a problem writing a few strings in Tailwind for your class names, though our goal is to reduce redundancy at the end of the day, and don't forget how painful it is having to sift through several components and repeatedly change the same styles.
As I'm on this journey to becoming a better programmer, designer, and writer each and every day, I'm constantly finding ways to be more efficient. Let's keep the conversation going over on LinkedIn and Twitter.
Happy coding and creating!