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

class-filter

v0.0.6

Published

## Tailwind CSS Class Merger for React Components

Downloads

81

Readme

Class Filter

Tailwind CSS Class Merger for React Components

Class Filter is a lightweight utility that merges Tailwind CSS classes, ensuring no duplicates or conflicts when combining default and additional classes in React components. Perfect for maintaining a clean and efficient class structure in your projects.

Features

  • Class Conflict Resolution: Automatically replaces conflicting Tailwind classes, retaining the latest ones.
  • Simplified Integration: Easily merge default classes with additional ones passed as props.
  • React Optimized: Ideal for use in React components where default styling needs to be extended or overridden.

Installation

Install via npm:

npm install class-filter

Or via Yarn:

yarn add class-filter

Usage

Basic Example

The cf function (cf stands for "class filter") helps you merge two sets of classes, ensuring that only one version of each class type is present.

import React from 'react';
import { cf } from 'class-filter';

type ButtonProps = {
  className?: string;
  children: React.ReactNode;
};

const Button: React.FC<ButtonProps> = ({ className = "", children }) => {
  const defaultClasses = "px-4 py-2 rounded bg-blue-500 text-white";
  const finalClassName = cf(defaultClasses, className);

  return <button className={finalClassName}>{children}</button>;
};

const ButtonExample: React.FC = () => (
  <div className="p-6">
    <h1 className="text-2xl font-bold mb-4">Button Example</h1>
    
    <Button>Default Button</Button>
    <Button className="bg-red-500 mt-4">Red Button</Button>
    <Button className="mt-4 p-4">Padded Button</Button>
    <Button className="mt-4 text-black">Black Text Button</Button>
  </div>
);

export default ButtonExample;

Explanation

  • Default Classes: The Button component starts with a base set of classes.
  • Extra Classes: Additional classes are passed via the className prop.
  • Conflict Resolution: The cf function merges these, resolving conflicts by using the classes from className over those in defaultClasses.

Why Use Class Filter?

When building React components with Tailwind CSS, you often have a set of default styles but want to allow users to override or extend them. The cf function makes it easy to merge these classes without worrying about duplicates or conflicts.

Real-World Use Cases

  • Reusable Components: Create components with sensible defaults that can easily be customized by passing additional Tailwind classes.
  • Consistent Styling: Maintain consistent styling across your application by ensuring that only one version of each class type is applied.
  • Improved Maintainability: By using the cf function, you reduce the risk of conflicting styles, making your code easier to maintain.

Advanced Usage

In scenarios where more complex class merging is required, such as conditional styling, you can use cf to dynamically generate the final class string:

const isActive = true;

<Button className={cf("bg-gray-200", isActive ? "bg-green-500" : "bg-red-500")}>
  Conditional Button
</Button>

This flexibility allows you to build more dynamic and responsive UIs with ease.

License

This project is licensed under the ISC License. See the LICENSE file for more details.

Contributing

Contributions are welcome! If you have ideas for improvements or run into any issues, please open an issue or submit a pull request.