How to Conditionally Join CSS Class Names with Class-Names

Apr 15, 2024 | Programming

Welcome aboard, fellow developers! Today, we’ll dive into the world of React and explore how to conditionally join CSS class names using the class-names package by Sindre Sorhus. This method is especially useful when you’re looking to make your component styling more dynamic and efficient. Let’s get started!

Installation

First things first, we need to install the class-names package. You can do this by executing the following command in your terminal:

$ npm install @sindresorhus/class-names

Usage

Once installed, using class-names is as easy as pie! Let’s explore some examples to understand its potential.

Basic Example

Imagine you have two classes, unicorn and rainbow. To join them together, you can simply use:

import classNames from '@sindresorhus/class-names';

classNames('unicorn', 'rainbow'); 
// = 'unicorn rainbow'

Conditional Class Names

Let’s say you want to conditionally apply some class names based on the truthiness of certain values. Here’s how:

classNames({
  awesome: true,
  foo: false,
  unicorn: true,
  rainbow: false
}); 
// = 'awesome unicorn'

Ignore Falsy Values

What if you have a mix of values, including null or undefined? Don’t worry, class-names will neatly ignore those:

classNames('unicorn', null, undefined, 0, 1, {foo: null});
// = 'unicorn'

Dynamically Constructing Class Names

Let’s say you want to dynamically construct a class name based on the button type. You can do it like this:

const buttonType = 'main';
classNames([`button-${buttonType}`]: true); 
// = 'button-main'

Creating a Button Component

To see class-names in action, here’s a simple button component:

import classNames from '@sindresorhus/class-names';

const Button = (props) => {
  console.log(props);
  // type: success, small: true
  const buttonClass = classNames('button', 
    {[`button-${props.type}`]: props.type, 
    'button-block': props.block, 
    'button-small': props.small});
    
  console.log(buttonClass); 
  // = 'button button-success button-small'
 
  return ;
};

Understanding the API

The core of this package lies in the classNames(…input) function. It allows for a seamless combination of string and object inputs, where only truthy values will be included, and everything else is ignored. This makes managing your CSS classes clean and efficient.

FAQ

What sets it apart from classnames?

  • Dedupes by default.
  • Does not coerce numbers to strings.
  • Does not support array input, but you can use the spread operator instead.

Related Resources

For more utilities, check out react-extras for useful components tailored for React.

Troubleshooting Ideas

If you encounter any issues or have questions while using the class-names package, here are some troubleshooting steps:

  • Ensure the class-names package is properly installed in your project.
  • Double-check the syntax, particularly with how you’re passing objects and strings.
  • If classes are not being applied as expected, inspect the resulting class string to confirm what is being returned.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox