Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class name prop not overriding component styles #386

Open
Konglomneshued opened this issue Feb 12, 2023 · 4 comments
Open

Class name prop not overriding component styles #386

Konglomneshued opened this issue Feb 12, 2023 · 4 comments

Comments

@Konglomneshued
Copy link

Konglomneshued commented Feb 12, 2023

Edit: I've pasted the code for the right component now.

My question is how do I do this:

// Example case; my actual case is a button
// Just easier to show what I want to do in one place
// Since I have nested components
...
<ul className={`
   ${styles.listDefaults}
   ${customStyles}
`}>
...

and have customStyles override styles.listDefaults?

An Icon Button component

import type { CSS_Class } from 'types/cssTypes';

import { Button } from 'components/button';

interface Props {
   name: string;
   iconPath: string;
   buttonStyles?: CSS_Class;
   iconStyles: CSS_Class;
}

export const IconButton = ({ name, iconPath, buttonStyles = '', iconStyles }: Props) => {
   return (
      <Button
         customStyles={buttonStyles}
      >
         <img
            className={iconStyles}
            src={iconPath}
            alt={name}
         />
      </Button>
   );
};

New Tab Button (an Icon Button) component styles

.newTabButton {
   padding: 0.4em;
   border-radius: 0.25em;
}

.newTabButton:focus,
.newTabButton:hover {
   background: var(--highlight);
}

.newTabButton:active {
   background: var(--selected);
}

.plusIcon {
   filter: var(--iconColor);

   width: 1em;
}

The Button component

import type { ReactNode } from 'react';

import type { CSS_Class } from 'types/cssTypes';

import styles from './Button.module.scss';

interface Props {
   customStyles?: CSS_Class;
   children?: ReactNode;
}

export const Button = ({ customStyles = '', children }: Props) => {
   return (
      <button className={`
         ${styles.buttonDefaults}
         ${customStyles}
      `}>
         {children}
      </button>
   );
};

Button component styles

.buttonDefaults {
   color: inherit;

   background: 0;
   padding: 0;
   border: 0;
}

My Design in HTML & CSS

New Tab Design
New Tab Design Styles (Highlighted)

My result when I make the component in React

New Tab Development
New Tab Development Styles (Highlighted)

@frontend-sensei
Copy link

Hi @Konglomneshued !
Did you figured out your issue?

@Konglomneshued
Copy link
Author

Hi @Konglomneshued ! Did you figured out your issue?

I did not. It's a known issue with CSS modules and there is only a workaround, unfortunately, which is to increase the specificity of the class you wish to have priority by repeating it:

.newTabButton.newTabButton

@frontend-sensei
Copy link

Ohh, I see. Unfortunately, I took the same solution.
Thanks for reply.

@receter
Copy link

receter commented Apr 17, 2024

Hi I just wrote an article about this exact topic, maybe it helps you understand the issue in more depth: https://dev.to/receter/avoiding-css-order-of-appearance-problems-with-css-modules-3j7p

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants