in CSS it’s easy to override a component class, usually we’re using BEM naming convention to build a reusable components, and if you want a special case style for a component then we simply add a specificity to that particular component.
For example, if you have a Navbar component, and let’s pretend you applied this style to Navbar:
.Navbar {
background-color: red;
}And this components is being used in every Page, but in a contact page you want a special case, you want to set the background to green. Well, it’s simple right, just add a specificity like this:
.ContactPage .Navbar {
background-color: green;
}But in CSS Modules this can became problematic. So, first you need to know how CSS Modules is work, it’s transform every classname into a generated string so that it become unique.
So our Navbar style would became something like this:
.Navbar__12GH89 {
background-color: red;
}CSS Modules will add a unique hash in the end of every classname, so everything will have no specificity and become one level only. The problem arise when you want to use this specificity.
Let’s say you setup a React project, and then having a structure like this:
src/
└── components/
├── Navbar.js
├── Navbar.module.scss
├── ContactNavbar.js
└── ContactNavbar.module.scssWhere ContactNavbar is simply an extended Navbar component with special use case of style.
in Navbar.js the code like this:
// Navbar.js
import React from "react"
import styles from "Navbar.module.scss"
const Navbar = ({ className }) => (
<div className={`${styles.root} ${className}`}/>
)
export default Navbarand Navbar.module.scss:
// Navbar.module.scss
.root { background: red; }and here the ContactNavbar.js
// ContactNavbar.js
import React from "react"
import styles from "ContactNavbar.module.scss"
import Navbar from "./Navbar"
const ContactNavbar = () => <Navbar className={styles.contactNavbar}/>
export default ContactNavbarand then we want the style to look like this
// ContactNavbar.module.scss
.contactNavbar { background: green; }it looks like a silly example, but the use case for this ability exist, sure they’re more complex than something like this, but that is the idea.
So, the ended up having red background for Contactnavbar eventhough we want it to be green, because we don’t have the specifity, all of the classname become the same level, and even worse we don’t know which one being applied at the first and which one is applied for the last rule. Which bring us into a Race condition.
This is somehow to be expected, because it basically how CSS Modules works.
The Solution
The only solution I know is to make double class name declaration so that you have more specificity, so in our ContactNavbar.module.scss we wrote like this instead:
// ContactNavbar.module.scss
.contactNavbar.contactNavbar { background: green; }It may seems weird to you, but it’s still a valid way to add a specificity to CSS nonetheless. And in the compiled output we will have something like this:
.contactNavbar__123AD.contactNavbar__123AD {
background: green;
}
.navbar__767AS {
background: red;
}this is better than adding !important to every extended styles, because it make everything harder to track, and the weirdest thing is you can duplicate the class name more than twice to add more specificity into it, but I encourage you to only add 2, because other than that probably you make a different concept of styling from the extended parent itself, and consider creating an abstraction or make a new styles definition instead.