Sub topic 7
Combining Several Classes on One Element
This chapter assumes you can write a React component with props and JSX, and that you have already imported a .module.css file and applied a single class through the styles object. Here you will learn how to put more than one class on the same element.
Why one element needs several classes
A button usually has a base look shared by every instance, plus a variant (primary or secondary) and sometimes a state (disabled). Keeping these in separate classes avoids repeating the shared rules. The element then gets all the classes it needs at once.
Start with a stylesheet that separates these concerns:
.button {
padding: 0.5rem 1rem;
border: 0;
border-radius: 4px;
cursor: pointer;
}
.primary {
background: #2563eb;
color: #fff;
}
.secondary {
background: #e5e7eb;
color: #111827;
}
.disabled {
opacity: 0.5;
cursor: not-allowed;
}The problem with a plain `className`
The className prop takes one string. Each value on styles is a generated class name, so you need a string that holds several of them separated by spaces. A template literal works:
<button className={`${styles.button} ${styles.primary}`}>Save</button>This is fine for two fixed classes. It gets awkward once some classes are optional. If disabled is false, a naive version produces the text undefined or false inside the class string. Those values are not valid class names and make the output hard to read.
Joining classes with clsx
The small clsx library builds the string for you. It accepts any number of arguments, joins the truthy ones with spaces, and skips false, null and undefined. Install it with npm install clsx.
import clsx from 'clsx';
import styles from './Button.module.css';
export function Button({ variant = 'primary', disabled = false, children }) {
return (
<button
className={clsx(styles.button, styles[variant], disabled && styles.disabled)}
disabled={disabled}
>
{children}
</button>
);
}Each argument does a different job:
styles.buttonis always present. It carries the shared base styles.styles[variant]uses bracket access to pickstyles.primaryorstyles.secondaryfrom the prop value.disabled && styles.disabledadds the state class only whendisabledis true. When it is false, the expression isfalse, andclsxignores it.
With variant="secondary" and disabled set, the rendered element has three generated class names, one for each of button, secondary and disabled.
Common errors
- A missing variant gives no styles. If
variantis a value with no matching class, such as"danger"here,styles[variant]isundefined.clsxdrops it silently, so the button renders with only the base look. Check the spelling of the prop values against your class names. - Writing
className={styles.button styles.primary}. This is a syntax error. Always wrap multiple values in one expression. - Using a string for a class from the file. Writing
'primary'instead ofstyles.primaryadds a plain, unscoped class that matches nothing in your module. - Relying on source order for conflicts. When two classes set the same property, the one that appears later in the CSS file wins, not the one listed later in
clsx. Keep variant classes from setting the same properties as each other on the same element, or place the overriding rule later in the stylesheet.
Exercise
A Button receives variant="secondary" and disabled={false}. Using the clsx call above, which of the stylesheet's classes end up on the element, and why is no undefined text added to the class string?
Show answer
The element gets the generated names for .button and .secondary. disabled && styles.disabled evaluates to false, and clsx skips falsy arguments, so nothing is added for it.