Sub topic 6
Naming classes: camelCase and bracket access
This chapter assumes you can import a .module.css file and apply a class with styles.name. It covers how your class names in CSS become property names on the styles object, and which naming style makes that object easiest to use.
Class names become object keys
The styles object that a CSS Module exports is a plain JavaScript object. Each local class name in the file is a key, and each value is the generated unique class name. Reading a class from the object is ordinary property access, so JavaScript's rules apply.
Dot access (styles.button) works only when the key is a valid identifier. A name with a hyphen is not one, because styles.primary-button is parsed as styles.primary minus button. That expression produces NaN or throws, depending on what button is.
.primary-button {
background: #2563eb;
color: #fff;
}For that class, you need bracket access:
import styles from './Button.module.css';
export function Button({ children }) {
return <button className={styles['primary-button']}>{children}</button>;
}Bracket access works for any key, but it is noisier and easy to get wrong. Most projects avoid it by naming classes in camelCase.
Prefer camelCase in the CSS file
If you write .primaryButton in the stylesheet, you can use styles.primaryButton in the component. This also keeps the name identical in both files, so a search for it finds every use.
| Class in CSS | Access in JavaScript | Notes |
|---|---|---|
| `.button` | `styles.button` | A single word works either way. |
| `.primaryButton` | `styles.primaryButton` | Recommended. |
| `.primary-button` | `styles['primary-button']` | Valid, but needs brackets. |
| `.primary_button` | `styles.primary_button` | Works, but is less common. |
Bracket access for computed names
Brackets are also the right tool when the class name is not known until runtime. The variant pattern from earlier chapters depends on this:
import styles from './Button.module.css';
export function Button({ variant = 'primary', children }) {
return <button className={`${styles.button} ${styles[variant]}`}>{children}</button>;
}Here styles[variant] looks up styles.primary or styles.secondary. The names of the variants must match the class names in the CSS file exactly, including case.
Missing keys fail silently
If the key does not exist, you get undefined. React omits the className attribute for undefined, so nothing breaks. The element is simply unstyled. In a template string, as in the example above, the result is worse: the literal text undefined ends up in the class list.
// If .secondry is a typo, styles.secondry is undefined.
<button className={styles.secondry}>Save</button>Three habits catch these mistakes early:
- Log or inspect
stylesonce in the browser console to see the real keys. - Use TypeScript declarations that list the class names, if your tooling can generate them.
- Use
clsxinstead of template strings, because it drops falsy values rather than printing them.
Letting the bundler convert names
You can write kebab-case in CSS and still use dot access, if the bundler converts the exported keys. In Vite, set localsConvention:
// vite.config.js
import { defineConfig } from 'vite';
export default defineConfig({
css: {
modules: {
localsConvention: 'camelCase',
},
},
});With camelCase, the class .primary-button is exported as both styles['primary-button'] and styles.primaryButton. The camelCaseOnly setting exports only the camelCase key. In css-loader (v6 and later) the equivalent option is modules.exportLocalsConvention with the values camel-case or camel-case-only.
Be aware of the cost. The conversion is configuration that every contributor and every test setup must share. Writing camelCase in the CSS needs no configuration at all.
Summary of the rule
- Use camelCase for multi-word class names.
- Use dot access when the name is fixed.
- Use bracket access when the name is computed or contains a hyphen.
Exercise
A stylesheet defines .error-message. A teammate writes className={styles.error-message} and the element is unstyled. Why does it fail, and what are two ways to fix it?
Show answer
The hyphen is not valid in a dot-access identifier, so JavaScript reads styles.error minus message. Fix it by renaming the class to .errorMessage and using styles.errorMessage, or by keeping the CSS and writing styles['error-message']. A third option is to set the bundler's locals convention to camelCase.