Sub topic 9
Reusing styles with composes
This chapter covers the composes declaration in CSS Modules. It lets one class build on another without copying declarations, and it keeps the component code simple. It assumes you can apply classes through the styles object, you know how generated class names keep CSS locally scoped, and you have seen clsx used for variants. You should also know the cascade and specificity basics.
The problem composes solves
Say you have a base button style and a danger variant that needs the same padding and radius. You could copy the declarations:
.base {
padding: 0.5rem 1rem;
border-radius: 4px;
}
.danger {
padding: 0.5rem 1rem;
border-radius: 4px;
background: #b91c1c;
color: #fff;
}This works, but the shared values now live in two places. If the padding changes, you have to remember to edit both rules.
The clsx approach from the previous chapter avoids the copy. The component applies styles.base and styles.danger together. That moves the knowledge "danger needs base" into JavaScript, and every caller has to repeat it. composes moves that relationship back into the stylesheet, where it belongs.
Basic composes
Use composes inside a rule, and name the class to build on:
.base {
padding: 0.5rem 1rem;
border-radius: 4px;
}
.danger {
composes: base;
background: #b91c1c;
color: #fff;
}composes is not a CSS property. The CSS Modules loader reads it at build time and removes it from the output. The browser never sees it.
What it changes is the exported object. Without composes, styles.danger is one generated name. With it, styles.danger is a string holding two names, the generated danger name and the generated base name.
import styles from './Button.module.css';
export function DangerButton({ children }) {
return <button className={styles.danger}>{children}</button>;
}At runtime, the rendered element looks roughly like this (the exact names depend on your bundler configuration):
<button class="Button_danger__a1b2c Button_base__d3e4f">Delete</button>The component uses a single key, and the element gets both classes. The .base rule is emitted once, in the stylesheet, and is not duplicated into .danger.
Composing several classes
You can list more than one class in a single declaration, separated by spaces:
.rounded {
border-radius: 4px;
}
.padded {
padding: 0.5rem 1rem;
}
.danger {
composes: rounded padded;
background: #b91c1c;
color: #fff;
}You can also write several composes declarations in one rule. The result is the same set of names. By convention, put composes first in the rule so a reader sees where the class comes from before its own declarations. Some tooling requires this ordering, so it is a safe habit.
Composing from another file
The more useful form shares styles across files. Put the shared classes in a module of their own and compose them by path:
/* shared.module.css */
.card {
padding: 1rem;
border: 1px solid #e5e7eb;
border-radius: 8px;
background: #fff;
}/* Panel.module.css */
.panel {
composes: card from './shared.module.css';
box-shadow: 0 1px 3px rgb(0 0 0 / 0.1);
}
.panelWarning {
composes: card from './shared.module.css';
border-color: #f59e0b;
}The component imports only Panel.module.css:
import styles from './Panel.module.css';
export function Panel({ warning = false, children }) {
return (
<section className={warning ? styles.panelWarning : styles.panel}>
{children}
</section>
);
}The generated name for card comes from shared.module.css, so it is the same name wherever it is composed. The from path is resolved relative to the current CSS file, like an import.
You can also compose a class that is not scoped, using from global:
.title {
composes: heading from global;
color: #111827;
}This adds the literal class name heading to the output, with no generated suffix. It is the composes counterpart of :global, which the next chapter covers.
Where composes is allowed
composes has strict rules, and breaking them gives a build error.
- The rule's selector must be a single class name.
.danger { composes: base; }works..danger:hover { composes: base; },.card .title { composes: base; },button.danger { ... }and#id { ... }do not. - The composed class must be a plain class defined in the same file or in a file you name with
from. - You can compose from a local class only if it is a simple class selector. A rule such as
.base:hoverdoes not count as a source of.base. - Support inside
@mediablocks and nested rules from preprocessors is inconsistent between toolchains. Do not rely on it.
The ordering gotcha
composes adds class names to an element. It does not change which declaration wins. The cascade still follows specificity first, then source order in the final CSS. The order of the names in the class string has no effect.
When .danger composes .base from the same file, both rules have the same specificity. If both set padding, the one that appears later in the file wins. Define the base class before the classes that compose it, so the more specific class can override it:
.base {
padding: 0.5rem 1rem;
}
.danger {
composes: base;
padding: 1rem;
}Here .danger wins because it comes later. If you reversed the order in the file, .base would win and the override would silently stop working.
Across files, the risk is larger. The order of rules from shared.module.css and Panel.module.css in the final bundle depends on how the bundler orders CSS, which can differ between development and production builds. Avoid depending on that order. If a class composes from another file and needs to override one of its properties, use a more specific selector or avoid declaring the same property in both places. A safer design is to have the shared class set defaults that the composing class does not redeclare.
Composes or clsx
Both approaches combine classes, and they suit different jobs.
| Situation | Better choice | Reason |
|---|---|---|
| A class always includes another, such as danger always includes base | `composes` | The relationship is fixed and belongs in the stylesheet |
| Several variants share one base style | `composes` | Each variant exports a single key |
| A class depends on props or state, such as `disabled` | `clsx` | The decision happens at render time |
| Picking a variant by name, as in `styles[variant]` | `clsx` or direct lookup | The key is dynamic |
| Sharing a style across many components | `composes ... from` | One source file, one generated name |
The two combine well. A common pattern is to compose the fixed parts in CSS and use clsx only for the conditional parts:
import clsx from 'clsx';
import styles from './Button.module.css';
export function Button({ danger = false, disabled = false, children }) {
return (
<button
className={clsx(danger ? styles.danger : styles.button, disabled && styles.disabled)}
disabled={disabled}
>
{children}
</button>
);
}.button {
padding: 0.5rem 1rem;
border: 0;
border-radius: 4px;
cursor: pointer;
}
.danger {
composes: button;
background: #b91c1c;
color: #fff;
}
.disabled {
opacity: 0.5;
cursor: not-allowed;
}The component no longer needs to pass styles.button alongside styles.danger. The stylesheet guarantees that danger always carries the base look.
Common mistakes
- Putting
composesin a rule with a compound selector, which fails the build. - Composing a class that is defined later in the same file and then expecting the later rule to lose. Source order still decides.
- Forgetting the quotes or the
fromkeyword. The correct form iscomposes: card from './shared.module.css';. - Composing a class from a file that is not a CSS Module. Plain
.cssfiles are global, so usefrom globalfor those names instead. - Composing very deep chains. A class that composes a class that composes another is hard to trace. Keep chains to one or two levels.
Exercise
A file has .base { padding: 0.5rem; } followed by .card { composes: base; padding: 1rem; }. Then you move .base below .card in the same file. What happens to the padding on an element that uses styles.card, and why?
Show answer
Before the move, the padding is 1rem, because .card appears later and both rules have equal specificity. After the move, .base appears later and its 0.5rem padding wins. composes only adds the generated class name of base to the element. It does not change the cascade, so the override depends on source order.
Exercise
You want .link:hover to also include the styles from a class named linkBase. Can you write .link:hover { composes: linkBase; }? If not, what should you do instead?
Show answer
No. composes is only allowed in a rule whose selector is a single class name, and .link:hover includes a pseudo-class. Put composes: linkBase; in the plain .link rule, and write the hover declarations directly in .link:hover.