npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tailwind-class-merge

v1.0.0

Published

A utility function that combines `clsx` and `tailwind-merge` to ensure that a string of Tailwind CSS classnames are free of duplications and conflicts.

Downloads

3

Readme

Tailwind Class Merge

Tailwind Class Merge is a utility function that combines clsx and tailwind-merge to ensure that a string of Tailwind CSS class names are free of duplications and conflicts.

Installation

You can install Tailwind Class Merge using npm:

npm install tailwind-class-merge

Usage

The classMerge function accepts any number of arguments, which can be strings, objects, or arrays. It combines the class names using clsx and then merges the result with tailwind-merge to optimize the Tailwind CSS class utilities.

Import the classMerge function from the package:

import { classMerge } from "tailwind-class-merge";

Use the classMerge function to merge and optimize Tailwind CSS class names:

const className = classMerge("text-lg", "font-bold", "text-red-500");
// Output: "font-bold text-lg text-red-500"

Examples

Merging class names with conflicting Tailwind utilities:

const className = classMerge(
  "text-lg",
  "text-xl",
  "text-red-500",
  "text-blue-500"
);
// Output: "text-xl text-blue-500"

Merging class names with conditional objects:

const className = classMerge("text-lg", {
  "font-bold": true,
  "text-red-500": false,
});
// Output: "text-lg font-bold"

It pairs very well with the cva package, and sets you up for the "bulletproof" components described in their Handling Style Conflicts docs.

import { cva, type VariantProps } from "class-variance-authority";
import { classMerge } from "tailwind-class-merge";

const baseClassNames =
  "rounded-md font-semibold text-gray-700 bg-gray-300";

const button = cva(["button", baseClassNames], {
  variants: {
    status: {
      success: "text-green-700 bg-green-300",
      warning: "text-yellow-700 bg-yellow-300",
      danger: "text-red-700 bg-red-300",
      info: "text-blue-700 bg-blue-300",
    },
    size: {
      small: "text-xs px-2 py-1",
      medium: "text-sm leading-5 py-2 px-4",
    },
  },
  defaultVariants: {
    status: "info",
    size: "medium",
  },
});

export interface ButtonProps
  extends HTMLAttributes<HTMLButtonElement>, VariantProps<typeof button> {}

export const Button = ({ className, status, size, ...restProps}: ButtonProps) => {
  return (
    <button
      className={classMerge(button({ className, status, size }))} {...restProps}
    />
  );
}

// In use:
<Button size="small" className="shadow">
    A Small Shadowy Button
</Button>

// Output:
<button class="button rounded-md font-semibold text-blue-700 bg-blue-300 text-xs px-2 py-1 shadow">
    A Small Shadowy Button
</button>

Testing

Tailwind Class Merge includes unit tests to ensure the correctness of the classMerge function. To run the tests, use the following command:

npm test

The tests are written using the Jest testing framework and cover various scenarios to validate the behavior of the classMerge function.

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvement, please open an issue or submit a pull request on the GitHub repository.

License

This package is open source and available under the MIT License.