How to Seamlessly Integrate TailwindCSS and Radix Colors

Yannis Panagis

Yannis Panagis / August 26, 2023 (1y ago)

Introduction

As frontend developers, we are constantly striving to create interfaces that not only look great but also meet accessibility standards. That's why Radix UI's Color Palette by Work OS has been gaining such popularity. The Radix color system offers 1512 colors that automatically invert between light and dark themes and fit together that make designs that not only look great but feel intuitive to work with and work well for accessibility. Adam Wathan and the team have done an incredible job with TailwindCSS. TailwindCSS holds an important place in the frontend ecosystem and many developers love it because it provides an efficient and customizable way to build user interfaces. However, integrating Radix colors with TailwindCSS, isn't as easy as it looks - until now. For this reason, I've built a little npm package that might be able to help.

The Problem

I recently started working on a project with a designer who first introduced me to Radix and was adamant that it was the way to go. However, there were a few hurdles to jump in combining Radix with Tailwind. First, the automatic inversions in Radix don't "really" align with TailwindCSS's explicit specificity model. Typically if you're working with Tailwind you're quite used to seeing markup like this:

<div className="bg-red-100 dark:bg-red-900">{...}</div>

Second, the Radix color system has a scale which ranges from 1 to 12 and when I shared this with our internal team one of the first bits of feedback was that it wasn't easy to spot in a long string of classnames in the markup. When lots of teams decided that it was easier to work with styles as utility classes in the markup, they made the choice to bring it all together and so we're used to scanning long strings of classnames to find specific utilities, but most tailwind utilities are pretty easy to spot.

The Motivation

After lots of fiddling around I saw on some GitHub threads that lots of people were having similar issues and there wasn't really a plugin that integrated the two tools together and worked like this out there so, like any developer with an empty weekend, I set out to build one.

tailwindcss-radix-colors-plugin is a simple npm package that seamlessly integrates the Radix color system with TailwindCSS. This plugin is designed to address the challenges faced by developers when combining these two powerful tools.

With tailwindcss-radix-colors-plugin, you can finally harness the full potential of both TailwindCSS and the Radix color system without sacrificing bundle size or manually specifying inversions for 1440 colors. The plugin offers several key features that enhance your development workflow:

  1. Auto inversions for minimal markup: Trust Radix to handle color inversions automatically, avoiding the need to add excessive utility classes in your JSX markup.
  2. Fixed colors across themes: Easily use a specific color consistently across themes or from either the light or dark palette whenever the default radix inversions aren't working for you.
  3. Enhanced palette visibility: The color numbering system remains visible in the markup, making it easier to scan your codebase for colors.
  4. Dark mode overrides: In classic Tailwind fashion, whenever you want to override the dark variant of a colour on a component, you can use the dark prefix as before to take control.
  5. Specify inclusions and exclusions: You don't have to pull in an entire system if most of it doesn't matter for your app.tailwindcss-radix-colors-plugin let's you specify inclusions and exclusions in your tailwind config so only the colors you want get added, keeping your bundle size as small as possible and preventing unnecessary CSS from sitting in your stylesheets.
  6. Theme pinning: Sometimes applications aren't all light or dark. Sometimes they are sort of “patchy” and have subsets of the page that follow a specific “theme” in the realm of how they are styled. tailwindcss-radix-colors-plugin let's you pin the theme on a specific subset of the page to give you full control over your designs.

How to use tailwindcss-radix-colors-plugin

tailwindcss-radix-colors-plugin is a Tailwind CSS plugin that simplifies color management by integrating the ~Radix UI Color Palette~ into your Tailwind CSS configuration. This guide will walk you through the essential steps to get started.

Installation

Before you begin, ensure that your project meets the following prerequisites:

  • Node.js: Make sure you have Node.js version 12 or newer installed.
  • Tailwind CSS: Your project should be using Tailwind CSS version 2.0 or a more recent version.
  • PostCSS: Verify that your project processes CSS with PostCSS, commonly used in tools like Create React App, Next.js, or Gatsby.

Once your project meets these requirements, install the tailwindcss-radix-colors-plugin using npm:

npm install tailwindcss-radix-colors-plugin

Basic Usage

To use the plugin, import it in your Tailwind CSS configuration file (tailwind.config.js) and add it to your configuration:

const { tailwindRadixPlugin } = require('tailwindcss-radix-colors-plugin')

/** @type {import('tailwindcss').Config} \*/
module.exports = {
  content: ['./src/**/*.{js,ts,jsx,tsx,mdx}'],
  darkMode: 'class',
  theme: {
    // your theme configuration
  },
  variants: {
    // your variants configuration
  },
  plugins: [
    tailwindRadixPlugin(),
    // your other plugins
  ],
}

Your Tailwind CSS utility classes now have access to the Radix color palette. You can use these classes just like any other utility classes provided by Tailwind CSS:

<div className="text-crimson-500 bg-indigo-1000">
  <!-- your content -->
</div>

thats-all-folks

In just a few steps, we integrated Radix's entire color system Tailwind and now we can take full advantage of radix overrides, without compromising on classname visibility, overrides, and hybrid themes. If you're interested in any of the more advanced usage of the package such as theme pinning, inclusions/exclusions and more, head on over to https://tailwind-radix-plugin-docs.vercel.app/ to check it out!

Conclusion

Given I never shared the project publicly, I was surprised to find that as of the time I wrote this article a few 100 people had downloaded the package and the initial feedback has been positive. Of course I'm biased as the developer of this package to say it, but it really is easy to configure and add to your project in just a few lines.

What's next? The package doesn't have a lot of dependencies and is pretty standalone but there are a few extensions I have in mind. First and foremost, it would be great to extend the plugin to handle typography as well in the context of the radix palette as this still requires quite a bit of manual configuration.

If you're interested in contributing or expanding the capabilities of the plugin, please reach out Together, let's make tailwindcss-radix-colors-plugin the de facto solution for seamlessly combining TailwindCSS and Radix colors.

About

I'm Yannis Panagis and I'm the Tech Lead at Behaviour Lab in London. I spend my time building products, visualizing data, exploring London, and writing music.

Not Playing

© 2023 Yannis Panagis. All Rights Reserved.

Powered by Coffee. ☕️