Repository navigation
Description
Which @angular/* package(s) are relevant/related to the feature request?
core
Description
Utility CSS frameworks make two different class names conflict: px-4 from a component's host and px-2 from the template that uses it both set horizontal padding. Angular applies both, and the winner is decided by the order of the rules in the stylesheet, not by where the classes come from. So a consumer can't reliably override a component's classes.
This is now a mainstream setup: ng new offers Tailwind CSS as a stylesheet system and ng add tailwindcss configures it. Component libraries built on it (spartan/ui, shadcn-style libraries) all need to resolve these conflicts, usually with tailwind-merge.
In other frameworks this is simple because a component receives className / class as a prop and composes it itself (cn(own, props.className)). In Angular the classes of a host element come from sources the component can't see: the consumer's class and [class] / [class.x] bindings, host directives, other directives and Renderer2. There is no single place to merge them, so libraries work around it after Angular has written the classes. spartan/ui, for example, does it with effects, a MutationObserver per element and suppressing CSS transitions during hydration, because the classes land in two steps and cause flicker. That's ~950 lines (with tests) and a measurable runtime cost.
Angular already knows every class source of an element and their precedence, so it's the natural place to resolve conflicts once, before writing to the DOM.
Proposed solution
A small, generic, opt-in hook. It isn't Tailwind specific: any function that turns a list of classes into a class string.
import {provideClassMerge} from '@angular/core';
import {twMerge} from 'tailwind-merge';
bootstrapApplication(App, {
providers: [provideClassMerge((classes) => twMerge(classes))],
});Semantics:
- Angular passes the classes of an element in its precedence order, lowest first: host directives → component host → other directives (matching order) → template →
Renderer2.addClass. Within a source: staticclass→[class]→[class.x](in template order). - The merge function is called once per change and the result is written to
classwith a single write. - SSR and hydration (including incremental hydration) produce the same classes on the server and the client. Server-rendered classes Angular doesn't own are kept until the app is stable, so classes such as
RouterLinkActive's don't flicker. - Classes added with
Renderer2.addClass(RouterLinkActive,NgClass) take part in the merge, andremoveClassrestores the class they replaced. Classes added outside Angular (classList,animate.enter) are kept as they are. - Tree-shakable: without the provider only an injection token lands in the bundle.
[style]bindings are untouched. - The function must be pure and deterministic (results are cached per template location).
Prototype and numbers
I have a working implementation with tests (acceptance, browser, platform-server hydration, forms), API docs and a guide section: diff against v21.2.16 (branch prototype/class-merge-hook on my fork; I'll rebase it on main for a PR). The existing suites pass unchanged. With a global identity merge, the only differences are class order in 8 specs that assert className strings.
To validate it, spartan/ui (300 components) was migrated from its userland solution to the hook. Measured on the spartan docs app (Chromium, 4x CPU throttling, median of 7 runs):
| Userland merging (today) | provideClassMerge |
|
|---|---|---|
| Elements with different classes (~46k elements, SSR + hydrated) | – | 0 |
Script time, /components/date-picker |
1637 ms | 531 ms |
Total blocking time, /components/date-picker |
2982 ms | 1844 ms |
| Transition flicker on load without suppression (98 routes) | 152 transitions | 0 caused by classes |
| SSR time per route | 171 ms | 172 ms |
Compared to stock Angular without any merging, on a list of helm-like components: rendering is faster (one class write per element instead of one classList.add per class), and updates are on par (1k rows: render 57.3 → 49.2 ms, update 13.2 → 12.4 ms).
Known cost: each changed class binding re-merges the element, so an element with N changed bindings is merged N times in one change detection.
Alternatives considered
- Userland, after Angular writes the classes (what libraries do today): needs effects and DOM observation on every element, writes classes twice, and causes transitions during hydration that have to be suppressed.
- A single
[class]binding owned by the component (hostClass = computed(() => cn(base, consumerClass()))with aclassinput): it misses classes from other directives, host directives andRenderer2, and takesclassaway from consumers as a normal attribute. - A compiler-level solution: the sources are only known at runtime (applied directives,
createComponent({directives}),Renderer2, hydration), so a runtime hook is needed anyway.
I'd be happy to open a PR if the direction looks good to the team.
cc spartan/ui maintainers: @goetzrobin @ashley-hunter @MerlinMoos
Activity
Flarette commentedon Nov 19, 2026
This feature was recently closed as not planned. You can see the full implementation here: #71966 and #67519
trs-dev-1 commentedon Nov 19, 2026
Thanks for the links, I read #67519, #71966 and the team's reply before opening this. I think this is a different, much smaller proposal, and the reasons the team gave for closing those don't fully apply here.
The component does not change. There is no new template syntax, no
(class)or(attr.x)binding and no resolver inhostmetadata. Onlyclassis affected, through one function of type(classes: string[]) => string.The provider is opt-in. Without it, the current behavior stays exactly as it is today, and only an injection token remains in the bundle, so it is tree-shakable. With it, the provider is a clear signal in
bootstrapApplicationthat the app changes how conflicting classes are resolved. This is the same model as other opt-in providers in Angular, such asprovideZonelessChangeDetection.I agree this means two behaviors, and that a global provider can hide things. But today the same question, which class wins, is answered by an
effectand aMutationObserverinside every component of a library. That is harder to find, not easier.The main reason for the hook is not convenience. The userland workaround writes classes twice and causes transitions to fire during hydration. On the spartan/ui docs app, script time on
/components/date-pickerwent from 1637 ms to 531 ms, and class-caused transition flicker on load went from 152 to 0. Measurements and the branch are in the description.Flarette commentedon Nov 19, 2026
I agree that current userland workaround sucks, the point was that proposal is a special case for the same feature. The
provideAttributeResolvers({ class: twMerge }, withAutoResolution())to be specific. In fact my first version was classes-only too before adding other attributes. The extra complexity and resolver syntax grew up from addressing edge cases, giving user an opt-in/opt-out api, Ability to apply surgically. Reduce cost. Etc. you should run AI agent to compare both prototypes and see where they differ in capability. The point team made that its too implicit so the final feature design would likely need a visible contract in the template. Maybe something like a template reference like<comp #cRef dir #dRef="dir" [class]="cn(cRef.classes(), dRef.classes(), 'px-2)"/>but idkFlarette commentedon Nov 19, 2026
Looks ugly af but AI doesnt care about that i guess
Flarette commentedon Nov 19, 2026
There is still extea issue with the example above is tha
[class.foo]tokens are managed separately from the static and [class] binding so they would still not participate. Everyone would need to be shoved inside thecn()function and surrounded by the ternaries syntax which React already doestrs-dev-1 commentedon Nov 19, 2026
@Flarette in my fork, the [class.value] binding works as expected.
Flarette commentedon Nov 19, 2026
I meant the template reference example in the previous comment
trs-dev-1 commentedon Nov 19, 2026
I read the team's reasoning on #67519: configuration that isn't visible at the component is a concern, and a global provider has the same problem.
The underlying problem remains for Tailwind-based libraries, so a question for the team: is there a direction you would consider?
effectand aMutationObserverper element.I'm happy to prototype whichever direction fits.