Sub topic 9
Reusing styles with composes
This chapter assumes you can import a .module.css file and read styles.button in a component. It also assumes you know how the cascade orders rules. Here you will learn how composes shares declarations between classes.
What composes does
A CSS Modules file often has several classes that share a base. You could repeat the declarations, or apply two classes in JSX. The composes declaration moves that sharing into the stylesheet.
.base {
padding: 0.5rem 1rem;
border-radius: 4px;
}
.danger {
composes: base;
background: #b91c1c;
color: #fff;
}composes does not copy declarations. The build step changes what the styles object exports. After processing, styles.danger is a string holding two generated class names, one for danger and one for base. The browser receives both on the element.
import styles from './Alert.module.css';
export function DeleteButton() {
return <button className={styles.danger}>Delete</button>;
}The component uses one key. The rendered element looks like class="Alert_danger__x1 Alert_base__y2". The exact names vary by configuration.
Composing from another file
You can compose a class from a different CSS Module. Add from and a path.
.danger {
composes: base from './shared.module.css';
background: #b91c1c;
color: #fff;
}This is useful for shared building blocks such as a card shape or a visually hidden utility. The other file keeps its own scope, and the component only imports Alert.module.css.
Rules and limits
composesmust be in a rule with a single class selector. Selectors like.a, .bor.a:hoverare not allowed.- You can list several classes:
composes: base rounded;. - Composed classes must be local. You cannot compose a
:globalclass this way, except with thefrom globalform, which you should avoid at this stage. - Put
composesbefore your own declarations by convention. It reads as "start from this."
Common errors
Override order. Both classes target the same property, and the cascade picks the winner by source order. Within one file, define the base class first. Across files, the order depends on how your bundler concatenates CSS, which can change. If .danger must override a property from a file you compose from, do not rely on order. Keep the base to properties the variants do not change.
Expecting composes to run in JavaScript. It runs at build time. You cannot compose conditionally. For conditional state, use clsx in the component, as in the earlier chapter on variants.
Chains that hide the output. Composing a class that itself composes others works, but a long chain makes it hard to see what lands on the element. Inspect the rendered class attribute in your browser's developer tools when styles look wrong.
Choosing between approaches
| Need | Use |
|---|---|
| Variant always includes the base | `composes` |
| Class depends on props or state | `clsx` in JSX |
| Share styles across components | `composes ... from` another module |
Exercise
A stylesheet has .danger { composes: base; padding: 2rem; } and .base { padding: 0.5rem; } is defined after .danger in the same file. Which padding does the button get, and how do you fix it?
Show answer
The button gets 0.5rem. Both classes are on the element, and .base comes later in the file, so it wins. Move .base above .danger, or remove padding from .base if variants set their own.