Search by Algolia
Introducing new developer-friendly pricing
algolia

Introducing new developer-friendly pricing

Hey there, developers! At Algolia, we believe everyone should have the opportunity to bring a best-in-class search experience ...

Nick Vlku

VP of Product Growth

What is online visual merchandising?
e-commerce

What is online visual merchandising?

Eye-catching mannequins. Bright, colorful signage. Soothing interior design. Exquisite product displays. In short, amazing store merchandising. For shoppers in ...

Catherine Dee

Search and Discovery writer

Introducing the new Algolia no-code data connector platform
engineering

Introducing the new Algolia no-code data connector platform

Ingesting data should be easy, but all too often, it can be anything but. Data can come in many different ...

Keshia Rose

Staff Product Manager, Data Connectivity

Customer-centric site search trends
e-commerce

Customer-centric site search trends

Everyday there are new messages in the market about what technology to buy, how to position your company against the ...

Piyush Patel

Chief Strategic Business Development Officer

What is online retail merchandising? An introduction
e-commerce

What is online retail merchandising? An introduction

Done any shopping on an ecommerce website lately? If so, you know a smooth online shopper experience is not optional ...

Vincent Caruana

Sr. SEO Web Digital Marketing Manager

5 considerations for Black Friday 2023 readiness
e-commerce

5 considerations for Black Friday 2023 readiness

It’s hard to imagine having to think about Black Friday less than 4 months out from the previous one ...

Piyush Patel

Chief Strategic Business Development Officer

How to increase your sales and ROI with optimized ecommerce merchandising
e-commerce

How to increase your sales and ROI with optimized ecommerce merchandising

What happens if an online shopper arrives on your ecommerce site and: Your navigation provides no obvious or helpful direction ...

Catherine Dee

Search and Discovery writer

Mobile search UX best practices, part 3: Optimizing display of search results
ux

Mobile search UX best practices, part 3: Optimizing display of search results

In part 1 of this blog-post series, we looked at app interface design obstacles in the mobile search experience ...

Vincent Caruana

Sr. SEO Web Digital Marketing Manager

Mobile search UX best practices, part 2: Streamlining search functionality
ux

Mobile search UX best practices, part 2: Streamlining search functionality

In part 1 of this series on mobile UX design, we talked about how designing a successful search user experience ...

Vincent Caruana

Sr. SEO Web Digital Marketing Manager

Mobile search UX best practices, part 1: Understanding the challenges
ux

Mobile search UX best practices, part 1: Understanding the challenges

Welcome to our three-part series on creating winning search UX design for your mobile app! This post identifies developer ...

Vincent Caruana

Sr. SEO Web Digital Marketing Manager

Teaching English with Zapier and Algolia
engineering

Teaching English with Zapier and Algolia

National No Code Day falls on March 11th in the United States to encourage more people to build things online ...

Alita Leite da Silva

How AI search enables ecommerce companies to boost revenue and cut costs
ai

How AI search enables ecommerce companies to boost revenue and cut costs

Consulting powerhouse McKinsey is bullish on AI. Their forecasting estimates that AI could add around 16 percent to global GDP ...

Michelle Adams

Chief Revenue Officer at Algolia

What is digital product merchandising?
e-commerce

What is digital product merchandising?

How do you sell a product when your customers can’t assess it in person: pick it up, feel what ...

Catherine Dee

Search and Discovery writer

Scaling marketplace search with AI
ai

Scaling marketplace search with AI

It is clear that for online businesses and especially for Marketplaces, content discovery can be especially challenging due to the ...

Bharat Guruprakash

Chief Product Officer

The changing face of digital merchandising
e-commerce

The changing face of digital merchandising

This 2-part feature dives into the transformational journey made by digital merchandising to drive positive ecommerce experiences. Part 1 ...

Reshma Iyer

Director of Product Marketing, Ecommerce

What’s a convolutional neural network and how is it used for image recognition in search?
ai

What’s a convolutional neural network and how is it used for image recognition in search?

A social media user is shown snapshots of people he may know based on face-recognition technology and asked if ...

Catherine Dee

Search and Discovery writer

What’s organizational knowledge and how can you make it accessible to the right people?
product

What’s organizational knowledge and how can you make it accessible to the right people?

How’s your company’s organizational knowledge holding up? In other words, if an employee were to leave, would they ...

Catherine Dee

Search and Discovery writer

Adding trending recommendations to your existing e-commerce store
engineering

Adding trending recommendations to your existing e-commerce store

Recommendations can make or break an online shopping experience. In a world full of endless choices and infinite scrolling, recommendations ...

Ashley Huynh

Looking for something?

Simplifying Tailwind CSS with ESLint tools, tagged template literals, and a lot more

Apr 15th 2022 engineering

Simplifying Tailwind CSS with ESLint tools, tagged template literals, and a lot more
facebookfacebooklinkedinlinkedintwittertwittermailmail

After years of development, we decided to standardize the user interface of one of our most important products – our multi-application dashboard. We did this for our customers and internal users (ease of use) as well as our product team (easier design process, decision-making, and coding). We also needed to align more consistently with our company’s branding. 

For this, we built an internal design system, called Satellite. In developing Satellite, we looked at different CSS solutions for the UI library, all with their pros and cons: Saas, css modules, css-in-js.

After considering frameworks similar to Bootstrap, we settled on the CSS framework Tailwind CSS. Why?

  • Pure CSS (no JS runtime) – good for performance
  • Tends to generate smaller CSS stylesheet files (after purging) – also good for performance
  • No switching between a CSS file and your javascript code while developing new components
  • No time wasted trying to find good names for utility classes
  • Helps promote UI consistency
  • Allows you to define a collection of spacings and colors that map well to design tokens (a “Restricted Palette”)

However… there was a downside to Tailwind: the readability of complex components. The soup of Tailwind can be hard to digest when you’re not used to its classnames. In our case, it was made worse because we had to use a prefixed versions of the CSS classes (stl-) to avoid conflicts with our legacy CSS, adding even more noise and length to our classname strings. 

This article shows how we mitigated the readability problem. For starters, we used several web development techniques, such as tagged literals and interpolation, to shorten the length of our strings. Then we simplified the usage of the classnames with the linter tool ESLint, providing a better DX with two tools:

  • An ESLint plugin, because there was no official ESLint-Tailwind plugin at the time. 
  • A Visual Studio Code extension to simplify the usage by providing intellisense of Tailwind’s many classes. The official ESLint VS extension didn’t work for us because it expected a configuration file (tailwind.config.js) to be present in the project, while we had used a prebuilt version at the time. Among other tasks, we needed VS to work with our config file.

That’s more or less the background. Now let’s get into the implementation.

Tailwind – classnames are good, but can get complex

Classnames are good

A utility-first CSS framework like Tailwind comes with a large set of pre-existing utility classes that you can use directly in your HTML and JavaScript. These classes enable consistency across the code. And they are entirely configurable: with the same classnames, we could easily brand our application with variants. Therefore, using Tailwind CSS classnames enabled us to create a consistent set of colors, spacing, fonts – essentially, all things CSS – and rolled out an easy-to-implement design system. 

But Tailwind classes can get complex

We wanted to simplify our usage of Tailwind’s classes. For this, we used techniques such as tagged template literals, interpolation, and conditions.

We started with a long string of CSS classes like the following:

const className = 'stl-inline-flex stl-items-center stl-justify-center stl-rounded-full stl-h-10 stl-w-10 stl-bg-blue-100';

But we soon realized that this was not easy to read. Additionally, it contained unnecessary noise, such as the prefix stl-, used to avoid clashes with other classes. So we enlisted the help of tagged template literals to remove the prefix from the string. We created an stl tag:

const className = stl 'inline-flex items-center justify-center rounded-full h-10 w-10 bq-blue-100';

Finally, we wanted more readability. So we added:

  • Separate lines for more readability and grouping of common elements
  • Interpolation with inline tagged template literals 
  • Conditions for more powerful and adaptive styling

The result is an elegant piece of (CSS) code:

const className = stl '
inline-flex items-center justify-center
h-10 w-10
${round && 'rounded-full'}
${iscool ? 'bg-blue-100' : 'bq-red-100'}
;

ESLint – effortlessly correcting human error

Elegance is one thing. Correct is another. It’s easy to misspell classes, especially when there are many classes to initially learn about in Tailwind. 

Here’s an example of what can go wrong: 

cost className = stl 'felx space-between text-gray-200’;

Can you spot the errors?

  • Switching letters (felx for flex)
  • Incomplete or non-existent classes (space-between)
  • American vs British spelling (gray)

ESLint to the rescue – creating an ESLint plugin

We needed to verify that the classes that people used were the correct ones. So we used the linter tool ESLint to parse the code, analyze it, and report errors. We created an ESLint plugin for code quality and to report errors on class names that didn’t exist.

eslint errors

Here’s the central ESLint code that does the validation:

eslint ast

ESLint uses an abstract syntax tree (AST) that gives access to individual lines of code. The AST essentially converts the strings of the code into nodes, which you can parse as collections and elements.

Here’s the breakdown of how ESLint parses the code. The whole expression is a node of type VariableDeclataion:

variable declaration

We want to parse the expression on the right side, the TaggedTemplateExpression. As you can see, there’s a callback that handles this kind of expression:

tagged template expression 

In the TaggedTemplateExpression callback, we collect all the strings in that template. For example:

  • The TemplateElement 

template element

  • The Literals

literals

Once the collecting is done, there’s another registered callback that loops through the collected class names and confirms whether they exist. It does this with the collection, validClassNames:  

tagged template expression eslint callback

That’s it. We knew right away that creating this validation plugin was the right thing to do, because we actually found a few misspellings in our system, as well as in the existing dashboard codebase!

Proposing suggestions with our ESLint VisualStudio extension

The last tool we created was an extension in Visual Studio Code. Using the same logic as in our plugin, ESLint suggests typescript classes as a developer types. This intellisense keeps the developers from guessing or having to go to the Tailwind website to search for and find the correct classes. 

As you can see in the GIF, it not only proposes class names, it also displays the colors or useful icons for each suggestion.

With Tailwind CSS and ESLint, we’ve been able to enforce our standards (accessible internally on Github) by improving the DX in implementing them. 

About the author
Peter Villani

Sr. Tech & Business Writer

linkedinmediumtwitter

Recommended Articles

Powered byAlgolia Algolia Recommend

Redesigning Our Docs – Part 4 – Building a Scalable CSS Architecture
engineering

Sarah Dayan

Staff Software Engineer

Building a Store Locator in React using Algolia, Mapbox, and Twilio - Part 1
engineering

Clément Sauvage

Software Engineer, Freelance

Goodbye JavaScript: Introducing our CSS API Client
product

Tim Carry