Repository navigation
Conversation
An element's classes come from three sources: the component's host, the other directives on the element, and the template. Angular applies their union, so conflicting utility classes such as `px-4` from the template and `px-2` from the component both stay on the element, and the stylesheet decides which one wins. Class resolver bindings, which the compiler emits in the next change, let an app resolve the classes of an element with a resolver such as tailwind-merge. This commit adds their runtime and public API. Nothing calls the new `ɵɵclassResolver` instruction yet, so no app changes behavior. When an element registers a resolver, its class bindings record their values instead of writing to the DOM. Once the view's host bindings have run, the element's resolvers run from the lowest to the highest priority: the component's host bindings, the other directives in match order, then the template. Each receives the classes grouped by source in `ClassSources`, and each later one receives the previous result folded back into the same groups. The runtime then applies the difference between the classes it applied last and the resolved ones, so classes that other code added, such as through `Renderer2.addClass`, stay on the element. - `ClassResolveEvent` carries `sources`, a `resolver(...extras)` that merges the sources with extra classes, and `resolve(classes)` that sets the element's classes. Without a call to `resolve()`, the classes pass through unchanged. - `ResolvedClasses` is an opaque value that only a bound resolver creates, so a `(class)` shorthand can't return plain strings. - `provideClassResolver(resolver)` sets the function that merges the classes. Each registration looks it up from its element's injector, so a component can use another one for its own template through `viewProviders`. Without a provider, the merge flattens the sources and drops repeated names. - A hydrated element starts from the classes that the server rendered, so the first resolution removes the ones that only the server resolved. `ComponentFactory` sets whether hydration claimed a root component's host before it creates the host's directives, and keeps a component created in a constructor from changing what the code that creates the outer node reads. - Dev mode reports a misuse of `resolve()` and a foreign shorthand result (NG0320), a resolver on `<ng-container>` or `<ng-template>` (NG0321), and a second resolver that a directive inherits (NG0323). Change detection and the styling instructions reach the runtime through hooks that the first registration sets, so an app without a class resolver doesn't bundle it.
Compile the two forms of a class resolver binding, which resolve the classes of an element through the class resolver runtime: - `(attr.class)="handler($event)"` receives a `ClassResolveEvent` with the classes of each source and sets the element's classes through it. - `(class)="F"` is a shorthand for `(attr.class)="$event.resolve((F)($event.resolver))"`, where `F` is a function, or a field that holds one, that receives the resolver. Both compile to `ɵɵclassResolver` instead of a listener, in templates and in host bindings, with flags that tell the runtime which form it is. On `<ng-container>` and `<ng-template>` they compile to the same instruction, so the runtime can report them. Other `attr.*` event names stay ordinary event bindings. The template parser reports a second class resolver on one element, and a `(class)` shorthand that is a chain of statements, as template parse errors (NG5002). A styling spec listened to a directive output named `class` through `(class)`. It now subscribes to the output in code, and still checks that the output emits and that the `[class]` binding applies. BREAKING CHANGE: `(class)` and `(attr.class)` bindings, in templates and host bindings, no longer listen to events or directive outputs with those names. They resolve the classes of their element instead. To keep listening, rename the output or event together with its listener, or subscribe to the output in code.
A directive can declare at most one class resolver, and only in its
`host` metadata. Host listeners are keyed by event name, so without a
check a resolver from `@HostListener('class')` would silently replace
one from `host`, or another one from the decorator.
- `@HostListener('class')` and `@HostListener('attr.class')` are
errors that point to `host: {'(class)': ...}` instead.
- `(class)` and `(attr.class)` in one `host` object are an error.
- A host `(class)` shorthand that is a chain of statements is an
error, as it is in templates; `(attr.class)` still runs statements.
AOT reports these as host binding parse errors (NG5001), and JIT
compilation throws with the same messages.
A linker that predates class resolvers would compile one as a plain
listener, so the partial declaration of a component or directive with
a class resolver in its host or its template now requires 22.3.0.
BREAKING CHANGE: `@HostListener('class')` and
`@HostListener('attr.class')` are compile errors.
To keep an existing event handler, rename the event and the listener
together, for example `@HostListener('classChange', ['$event'])` with
the code that dispatches the event renamed too, or listen in code
with teardown.
To resolve the host's classes, bind `(class)` or `(attr.class)` in the
`host` metadata instead, for example `host: {'(class)': 'resolve'}`.
Type checking now checks class resolver bindings the way the runtime runs them. - `$event` of an `(attr.class)` binding is a `ClassResolveEvent` when the types of output events are checked. - A `(class)="F"` shorthand is checked as its expansion, `$event.resolve((F)($event.resolver))`, with `F` checked against `ClassCallback` and the result of the call against `ResolvedClasses`. A plain call would accept a callback such as `(cn) => 'x'`. The check also infers the parameters of an arrow function and checks the `this` of a method that `F` calls. - A directive output named `class` or `attr.class` doesn't claim the binding, which is checked once, as a resolver. - A class resolver on `<ng-container>` is an error (NG8121), because the container has no classes to resolve. Its handler is still checked, with `$event` as `any`. - The `uninvokedFunctionInEventBinding` check (NG8111) no longer reports a `(class)` shorthand, which calls the function it names, nor a class resolver on a container, which NG8121 reports. - Symbol lookup, which the language service uses, finds the symbols in a shorthand. Also adds the ngtsc specs of the template rules for class resolvers (NG5002), whose templates only type-check once the bindings are checked as resolvers. The ngp backend checks class resolver bindings differently, so the specs that it fails are listed as its known failures.
`(class)` and `(attr.class)` always resolve the classes of their element, even when a directive on the element has an output of the same name, so the output's events never reach the binding. Code that used `(class)` to listen to such an output would silently stop receiving its events. The new extended diagnostic NG8120 (`outputShadowedByClassResolver`) warns when a class resolver binding on a template element shadows a directive output named `class` or `attr.class`, including one that a host directive exposes under that name. `<ng-container>` and `<ng-template>` resolve no classes, and type checking already reports a resolver on them (NG8121). Two type-checking specs that place a resolver next to an output of the same name now suppress the new warning. The ngp backend doesn't run this check, so the specs that it fails are listed as its known failures.
With class resolver bindings alone, an app that wants every element
resolved has to add `(class)` to each one. `withAutoResolution()`, a
feature of `provideClassResolver`, resolves every element that a
component or directive matches without a binding, including the host
of a component created in code and the component's host directives:
provideClassResolver(twMerge, withAutoResolution())
- Only the root providers can turn it on. Below the root, a component
turns it off for its template by providing the resolver without the
feature, and back on by providing it with the feature; nested
templates take the setting of the component that declares them. In
dev mode, a provider below a root without the feature throws NG0322;
in production it's ignored.
- An element with a `(class)` or `(attr.class)` binding is resolved
only through its bindings.
- The setting is looked up once per component view and root view. Each
element remembers the classes of each source and calls the resolver
again only when they change.
- Where the feature is off, class bindings apply as they do without
it.
- A component whose template was first created before
`withAutoResolution()` ran keeps rendering without automatic
resolution, because the template's setup is cached when it's first
created. Apps declare the feature in the root providers, which run
before any template renders; a test suite that turns it on partway
can hit this.
Element creation and `ComponentFactory` call a third hook after they
create an element's directives. It stays unset until
`withAutoResolution()` runs, so an app that doesn't use the feature
doesn't bundle it. The feature turns on for the whole process, so its
specs run in their own targets.
This was referenced Nov 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposal: angular/angular#71888 | Feature reference | Design record | Rounds | Deferred
Why the change
Angular applies the union of the classes that an element gets from its component, its other directives, and its template, so conflicting utility classes such as
px-4andpx-2both stay; this branch is the reference implementation of the(class)binding andprovideClassResolver()proposed in angular#71888, and this description records how the design got there.Special things to note
mainat5ac4cf7b54. An upstream PR waits until the team responds to the issue.(class)and(attr.class)become reserved: a template no longer listens to an output namedclassthrough them (NG8120warns; rename the output),@HostListener('class')is a compile error (bind(class)inhostinstead), and a partial declaration that uses them needs a22.3.0linker.ComponentFactory.createnow reads a root host's hydration information before its directives run, and restores thewasLastNodeCreatedflag around nested component creation, on every hydrated app. Every app also pays a null check per view refresh, per element that hosts a directive at creation, and per class binding in a template's first pass.Change outline
The issue shows the API. This outline shows how it's built, how the design got there, and what's still open.
What runs per element
On each refresh of a view, for each element with a resolver:
The types
All new public API in
@angular/coreis@experimental 22.3.Where the code lives
Commits and their tests
How the design got there
The design went through two design rounds and an upstream-preparation round.
docs/rounds.mdrecords the design rounds, andproposal.mdhas every measurement.Round 1, 2026-10-17 to 10-26: from one winner to a fold
In the first prototype, a handler returned the classes,
(class)="cn($event)", and$eventwas theClassSourcestuple. Each registration replaced the previous one:The sources are ordered by source, not by kind (static before bound). The case that decided it is a component with computed variants and a consumer with a static override. The component's host binds
[class]="variants()", which is'inline-flex h-9 px-4 bg-primary', and the consumer writes<ui-button class="px-2 w-full">.Ordered by kind, the consumer's static classes rank below the component's bound ones:
Ordered by source, the template ranks above the component:
Ordering by source also matches what Angular already does for one class name (
style_binding_list.ts), so(class)doesn't change which plain classes render.The round also found the following:
(class)as a plain listener, so the library's classes would silently stop resolving. Partial declarations that use the binding now require22.3.0.(class)next to aclassoutput as a listener, and classified<ng-container>inside<svg>as an element. Both are fixed, so the compiler classifies(class)the way the runtime runs it.classdropped the element's[class]binding, fixed in fix(core): do not treat outputs named class or style as styling inputs angular/angular#71661. Bindings on containers that do nothing raise no error, tracked in Report bindings that have no effect on ng-container, ng-template, and ng-content angular/angular#71881.Round 2, 2026-10-26 to 10-31:
resolve()instead of a return valueReturning the classes broke an event binding's void contract. The value wasn't type-checked, and a later statement in the handler could replace it silently.
NG8111had to exempt every(class), every bundle carried about 26 resolver symbols, and people and coding agents would forget a per-element(class).A component's extras rank at the end of its own source, not after every source. This was measured with
clsx2.1.1 andtailwind-merge3.7.0, simulating the fold, withhost: {class: 'inline-flex px-4'}and a consumer'sclass="bg-red-600" (class)="(cn) => cn('shadow-lg')":bg-muted[$event, 'bg-muted']inline-flex px-4 bg-muted shadow-lg: the consumer's color is lost['bg-muted', $event]inline-flex px-4 bg-red-600 shadow-lgcn('bg-muted')inline-flex px-4 bg-red-600 shadow-lgThe round decided the following, with the reason for each:
TS2717when a library augments the type differently. A resolver that receives[class]values failstwMerge's types, andclsx(new Set(['a']))returns''(measured). Angular normalizes extras the way it reads[class], sofalseadds nothing instead of rendering a class namedfalse.(class)shadows an output namedclass, andNG8120warns@HostListenercan't declare a resolverhost.NG0323twMergeresolves by position, and two resolvers in one directive would run in an order nobody writes down. A library's.d.tscarries no host bindings, so the check runs at runtime.NG8121andNG0321for(class)on containerswithAutoResolution()is declared in the root providers only(class)="active ? first : classes"dropped a method'sthis, compiled, and crashed. Exact checking can report up to three errors on one wrong callback.Designs that lost to the shorthand and
resolve():(class)="$event('x')"$eventbecomes a function. The callback names the resolver as a parameter, and a component can bind it by reference.(class)="[$event, 'x']"$eventwould mean the sources in the shorthand and the event in the full form. Extras rank by position, so a host that appends drops the consumer's class.(class)="'x'"replaces every class without a warning.resolve()applies the app's resolver itselfResolvedClasses$anypasses without a warning.(class)would need a provider before an app could adopt it. The plain merge matches what Angular renders today.[class]next to(class)class,[class.x], and host bindings stay sources anyway, and adopting(class)would mean rewriting existing[class]bindings.So both stay allowed, and they differ:
[class]="v"andcn()cn(v)false[class.x]bindingsclassinputClassBindingValueRound 3, 2026-11-06 to 11-08: preparing for upstream
An external review (Codex) checked each step: the code cleanup (R1), the test audit (R2), and the rebuilt history (R3).
NG8120guard, the merge, and the class diff before and after, and found no behavior change. The commented-out reservation of other(attr.*)names was removed.main. The compiler-first order wasn't bisectable, because it emittedɵɵclassResolverbefore core exported it, so the runtime now comes first.Constraints that shaped it
<ui-card>first rendered in the app shell would stay unresolved inside a lazy/dashboardthat turns the feature on. SowithAutoResolution()is root-only (NG0322), and a template compiled before the first call never gets it..d.tsrecords no host bindingsNG0323).minVersion: "22.3.0"for declarations that use the binding.[class]value is stored as a list sorted by class name[class]value resolve alphabetically. Extras keep their order, socn(value)resolves in the order written.classtakes the template's[class], as in spartan-ngclassstill is.@angular/compiler, so(class)compiles there, but it doesn't run the host rules orNG8120.(class)="(cn) => () => cn()"compiles until angular#71734 lands.Properties of the prototype
Renderer2.addClass()or an animation, stay on the element. The resolver doesn't see them.(class)runs on every refresh of the view that declares it, because its callback can read other state. Automatic resolution skips the resolver when no source's string changed.classListas today.checkNoChangespass doesn't run the handlers.Angular's own cost per element, measured in JIT dev mode with Domino and the resolver stubbed out, N=1000:
(class)twMergeadds 0.03–0.04 µs per call when its cache has the input, and 0.7–2.4 µs when it doesn't. Production mode, AOT, and a real browser aren't measured.Deferred and known gaps
Options for the team to decide, from the design record:
withAutoResolution(): warn once in dev mode, drop the cached template data, or set the data up lazily. Lazy setup would also let any provider turn the feature on. (Relevant on text fixtures that cache templates, not breaking but can introduce a footgun)(attr.*)namespace isn't reserved. Other names stay ordinary event bindings.[class]asserts on a top-levelfalsein dev mode, while extras accept it.Known gaps, listed in
docs/DEFERRED.md:createEmbeddedView()overwrites the hydration flag, so its element can keep classes that only the server resolved.(class)="choice?.classes"calls the method withoutthis.<ng-template>(upstream), so only the runtimeNG0321reports one.ngpcan widen an error span inside a shorthand, and its test driver can't detect a lost mapping.Left out on purpose:
(style)counterpart: each attribute needs its own source shape and fold rule.cnand bind[class]instead.[static, bound]split per source, and a switch that makes one resolver replace the lower ones: no case needs them yet.