DEV Community

Cover image for Creating a Tailwind CSS Fade Plugin: My Journey
Emmanuel Imolorhe | EIO
Emmanuel Imolorhe | EIO

Posted on • Updated on

Creating a Tailwind CSS Fade Plugin: My Journey

As a web developer, I've always been fascinated by the power of CSS to create stunning visual effects. I came across a nice trick for creating a fading effect in CSS, without using another element for overlay and I used it a lot in my regular CSS projects. Then, sometime in 2023, I had to implement this in a tailwind project. That's when I had an idea: why not create a Tailwind plugin to make this process easier and more efficient? Here's how I did it.

The Problem

I often needed to create fading effects on elements, particularly at the edges of containers or for text that needed to gracefully disappear. While it initially made sense to just write the styles in a global css file, implementing the effect across different components, with each component having its own fade ratio began to seem ridiculous. I wanted a solution that would leverage Tailwind's utility-first approach and make these effects as easy to apply and customize as using a utility class like padding (p-4).

The Solution: A Tailwind CSS Plugin 🎉

I decided to create a plugin that would generate utility classes for various fading effects. The goal was to make it as flexible and easy to use as possible, allowing developers to apply fading effects with simple classes like fade-x-4 or fade-t-8.

The Implementation

1. The Vanilla CSS

The plugin implements and supercharges this simple rule set:

.fade {
  -webkit-mask-image: linear-gradient(transparent, white);
  mask-image: linear-gradient(transparent, white);
}
Enter fullscreen mode Exit fullscreen mode

Yeah, it looks simple now 😅, but this is just the building block of the final product.

Since I was going for a supercharged version of this implementation which could easily adapt to customizations, I had to find a way for tailwind to control the fade amount or as I referred to it in the code, --padding
I used this value to generate the --start-padding and --stop-padding values.

If you remember, the effect uses a gradient
mask-image: linear-gradient(transparent, white);

That's where these padding values come in; to specify the gradient stops. You'll see it in use as we go on.

2. Creating Utility Functions

The core of the plugin is a set of utility functions that generate the necessary styles for different fading effects. Here's an example of one such function:

export const fadeX = (value: any) => ({
  "--padding": value,
  "--start-padding": "var(--padding, 0px)",
  "--stop-padding": "var(--padding, 0px)",
  "--mask-gradient": `linear-gradient(
    to right,
    transparent,
    white var(--start-padding),
    white calc(100% - var(--stop-padding)),
    transparent
  )`,
  "-webkit-mask-image": "var(--mask-gradient)",
  maskImage: "var(--mask-gradient)",
});
Enter fullscreen mode Exit fullscreen mode

I created similar functions for fading in different directions: fadeY, fadeTop, fadeBottom, fadeLeft, and fadeRight.

3. Integrating with Tailwind

The next step was to create the actual Tailwind plugin. I used Tailwind's plugin function and matchUtilities to create the utility classes:

import plugin from 'tailwindcss/plugin';

export default plugin(function ({ matchUtilities, theme }) {
  matchUtilities(
    {
      "fade-x": fadeX,
      "fade-y": fadeY,
      "fade-t": fadeTop,
      "fade-b": fadeBottom,
      "fade-l": fadeLeft,
      "fade-r": fadeRight,
    },
    {
      values: theme("spacing"),
    }
  );
});
Enter fullscreen mode Exit fullscreen mode

This setup allows users to use classes like fade-x-4, where the number corresponds to Tailwind's spacing scale.

The Result

The final product is a plugin that allows developers to easily apply fading effects to any element. For example:

<div class="fade-x-4 bg-blue-500 p-4 text-white">
  <p>This text fades on both sides</p>
</div>
Enter fullscreen mode Exit fullscreen mode

This creates a blue box with text that fades out on the left and right sides.

A blue box with text that fades out on the left and right sides, using @eioluseyi/tailwind-fade

Here's another example where this plugin has been used to fade out both the left and right sides of the container.
The @eioluseyi/tailwind-fade plugin has been used to fade out both the left and right sides of the container

Lessons Learned

  1. Flexibility is key: By using CSS custom properties, the plugin allows for dynamic adjustments and complex compositions.
  2. Integration matters: Ensuring the plugin works seamlessly with Tailwind's existing theme system (like the spacing scale) makes it more intuitive for users.
  3. Abstraction is the art of simplifying complexity: I realized I kept implementing this plugin, project after project, copying the code from codebase to codebase, until finally, I chose, today, to publish the plugin on npm, which is why I am just talking about it after a year 😅
npm install @eioluseyi/tailwind-fade
Enter fullscreen mode Exit fullscreen mode
yarn install @eioluseyi/tailwind-fade
Enter fullscreen mode Exit fullscreen mode

What's Next?

I'm looking to add more features to the plugin, such as:

  • Radial gradient options for circular fading effects
  • Preset combinations for common use cases
  • Custom property syntax declaration (@property) to allow transitions

I'm excited to see how other developers will use and potentially contribute to this plugin (It's open source! 🤘🏾). If you're interested in trying it out, you can find it on npm as @eioluseyi/tailwind-fade.

Creating this plugin was a fulfilling experience, and I hope it proves useful to the Tailwind community. Happy coding!

Top comments (0)