What is the functionality of "classnames" in cases where there are multiple classes sharing the same name?

Currently, I am reviewing some code that utilizes the npm utility classnames found at https://www.npmjs.com/package/classnames.

Within the scss file, there are various classes with identical names (small and large), as shown below:

.buttonA {
  &.small {
    height: 50px;
  }
  &.large {
    height: 80px;
  }
}

.buttonB {
  &.small {
    height: 20px;
  }
  &.large {
    height: 40px;
  }
}

This portion of the code is from the tsx file:

<IconButton
  ....
  className={
    classNames('buttonA', 'small')
  }
/>

I find this setup quite perplexing as it's unclear which 'small' class will be applied, given that we have two in the scss file. My assumption is that it would prioritize the 'small' class from buttonA since it's specified as the first parameter. However, I'm left wondering if these parameters are interdependent. What happens if I use classNames('buttonB', 'small'), or even just classNames('small')?

Answer №1

It appears that the issue at hand stems from an overestimation of the capabilities of the classnames utility and a potential misunderstanding of CSS and Sass functionality.

classnames serves as a tool for dynamically concatenating strings based on conditions. It does not possess the ability to comprehend or analyze the CSS properties associated with the outputted string values of the className. In fact, it can be utilized beyond styling purposes, such as in conditional text generation like:

const errors = ['this is required', 'that is also required'];
const numErrs = errors.length;
const errMsg = `You have ${numErrs} ${classnames('error', { s: numErrs > 1 })}`;

// errMsg is "You have 1 error" for a single error and "You have 2 errors" for two errors

The use of the parent selector (&) in your Sass file allows for the grouping of inner rules under a parent container, resulting in the following CSS structure:

.buttonA.small {
  /* properties here will applied to an element with both `buttonA` and `small` class */
  height: 50px;
}

.buttonA.large {
  /* properties here will applied to an element with both `buttonA` and `large` class */
  height: 80px;
}

.buttonB.small {
  /* properties here will applied to an element with both `buttonB` and `small` class */
  height: 20px;
}

.buttonB.large {
  /* properties here will applied to an element with both `buttonB` and `large` class */
  height: 40px;
}

Therefore, keeping these key points in mind:

  1. classnames('buttonA', 'small') will result in the string "buttonA small", assigning the classes .buttonA and .small to the element's className
  2. Understanding how the Sass-generated CSS is interpreted by browsers, the element will exhibit a height of 50px

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Discover more in React about using ellipses (...) with dangerouslySetInnerHTML

What is the best method for limiting the number of HTML lines retrieved using dangerouslySetInnerHTML in a React application and creating a '... Read More' expander for it? I attempted to use react-lines-ellipsis, but unfortunately the 'tex ...

Utilize ngx-filter-pipe to Streamline Filtering of Multiple Values

Need assistance with filtering an array using ngx-filter-pipe. I have managed to filter based on a single value condition, but I am unsure how to filter based on multiple values in an array. Any guidance would be appreciated. Angular <input type="text ...

What method can I use to incorporate a custom CSS code in Formfacade to conceal the back button on a Google Form?

Looking for a way to hide the back button on my custom questionnaire created with Google Forms? While there is no built-in option to do this directly on Google Forms, I decided to embed my form into Formfacade. However, I am struggling to find the CSS co ...

Clicking on the button link causes it to jump to a

Currently, I am utilizing Mixitup JQuery to create a portfolio page that organizes my content based on different filters. However, I have encountered a slight issue where when clicking on an item, the Jquery function works and shuffles the content, but the ...

Disabling the 'fixed navigation bar' feature for mobile devices

Here's a code snippet that I'm working with. It has the ability to make the navigation stick to the top of the page when scrolled to. HTML <script> $(document).ready(function() { var nav = $("#nav"); var position = nav.position(); ...

Exploring the parent of an HTML element / Finding a child element with unique content

Struggling with a challenge from frontendmentor that involves JavaScript. I need help on making the script detect if a P tag contains 0 as its content and then applying a different style to the containing div. <section class="pledge-box sub-section ...

Issue encountered when attempting to retrieve elements within an HTA's IFrame

We are currently working on automating an Intranet site using HTA and JavaScript. To bypass the browser security settings, we have implemented an Iframe within the HTA itself instead of opening the site in a browser. Despite being able to successfully logi ...

Tips for triggering an error during compilation for parameters lacking "as const"

Is there a way to generate a compile-time error if an argument is provided without being 'casted' as `const` in TypeScript? In the example code below, the array is converted into a `readonly` array: function myFunc<T extends string[]>(arr: ...

implementation of bug decrementation with a fadeout

For my current project, I need to implement a feature where individual elements containing a photo and their corresponding title fade out when clicked. However, the issue is that clicking on any picture fades out everything. How can I resolve this so only ...

What could be the reason my vue.js button is not generating a fresh textarea?

I am currently developing my first Web App with vue.js and I'm trying to implement a feature where clicking a button will generate a new textarea. It seemed to be functioning correctly when tested on jsfiddle, but once I tried running it in visual stu ...

How to Implement Horizontal Sorting with jQuery Using Various HTML Elements

Here is the current setup I have: http://www.bootply.com/sfLr2AJ7EU The issue I am facing is with moving the image (id) boxes horizontally. While I have managed to make it work vertically, attempting to do so horizontally has been unsuccessful. The image ...

Encountering a problem with Bootstrap 4 when trying to set medium breakpoints for columns, causing them to wrap to

I've been searching for a solution to this layout issue, but haven't found one yet. Here is the basic markup: <div class="container-fluid"> <div class="row border-bottom"> <div class="col d-none d-md- ...

An element from an array of typescript items

My current challenge involves working with arrays. Here is an example of the array I am dealing with: Array[{id:0,name:"a"},{id:1,name:"b"}...] In addition to this array, I have another array called Array2. My goal is to extract items from Array where th ...

Maintaining the aspect ratio of images in Bootstrap Carousel: A complete guide

Using the default carousel from Bootstrap template has been working well for me. However, I've encountered an issue where resizing the browser window distorts the image aspect ratio by squishing it horizontally. I've tried various solutions to m ...

Using the jquery plugin Sidr in Rails: A complete guide

I've been attempting to integrate the jQuery sidr plugin into my Rails project on Codecademy, but unfortunately, it's not functioning as expected. To view the page, simply click here: If you notice, the menu icon at the top left takes some time ...

JavaScript file encountering a problem with its global variables

What is causing the alert("2") to pop up first instead of it being second? Why am I encountering difficulty creating global variables c and ctx? Is there a way for me to successfully create these two global variables in order to utilize ...

Using the viewport meta tag in HTML and CSS within Laravel's Blade template while also

Greetings everyone! I have a question regarding Blade Laravel: Can we add a condition to the meta name="viewport" content="width=device-width, initial-scale=0.4"? I want the scale to start at 0.4 for smartphones and at 0.6 for tablets. ...

Leverage CSS to generate a visually appealing table of contents incorporating images and text blocks, perfectly aligned for an enhanced

I am in the process of designing a unique clip showcase on my website. The concept involves creating a table-like structure with a thumbnail image on the left and various information on the right, including a title, publication source, date, and descriptio ...

The contents of my div extend beyond the border-radius

Here is the issue I'm facing: My challenge involves having an <h1> nested inside a <div>. However, when I apply a border-radius to the <div>, the <h1> appears outside of the div. Below is the code snippet: <div class="test ...

How can you use jQuery to select and manipulate multiple form inputs with a common class?

I am currently working on a program that requires at least one of various form inputs to have data in order for the user to submit the form. I was able to successfully code for a scenario with only one input field, but I am facing a challenge when it comes ...