Title: [Feature Request] Resolve conflicting classes from a component, its directives, and its template · Issue #71888 · angular/angular · GitHub
Open Graph Title: [Feature Request] Resolve conflicting classes from a component, its directives, and its template · Issue #71888 · angular/angular
X Title: [Feature Request] Resolve conflicting classes from a component, its directives, and its template · Issue #71888 · angular/angular
Description: Which @angular/* package(s) are relevant/related to the feature request? core, compiler, compiler-cli Description Angular applies the union of an element's classes from the component's host, its other directives, and the template. With u...
Open Graph Description: Which @angular/* package(s) are relevant/related to the feature request? core, compiler, compiler-cli Description Angular applies the union of an element's classes from the component's host, its ot...
X Description: Which @angular/* package(s) are relevant/related to the feature request? core, compiler, compiler-cli Description Angular applies the union of an element's classes from the component's host...
Opengraph URL: https://github.com/angular/angular/issues/71888
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Feature Request] Resolve conflicting classes from a component, its directives, and its template","articleBody":"### Which @angular/\\* package(s) are relevant/related to the feature request?\n\ncore, compiler, compiler-cli\n\n### Description\n\nAngular applies the union of an element's classes from the component's host, its other directives, and the template. With utility CSS such as Tailwind, conflicting classes both stay, and the stylesheet picks the winner:\n\n```ts\n@Component({\n selector: 'ui-button',\n host: {class: 'px-4 py-2'},\n template: '\u003cng-content />',\n})\nexport class UiButton {}\n```\n\n```html\n\u003cui-button class=\"px-2\">Save\u003c/ui-button>\n\u003c!-- Today: class=\"px-4 py-2 px-2\". px-2 may or may not win. -->\n\u003c!-- Wanted: class=\"py-2 px-2\". -->\n```\n\nLibraries work around it with an input aliased to `class` and a computed host `[class]`, as spartan-ng does. That can't see `[class.x]` bindings or other directives' classes, and each library reimplements it.\n\n### Proposed solution\n\nAn app provides a resolver, and a `(class)` binding resolves an element's classes with it. A prototype is implemented and tested: https://github.com/Flarette/angular/pull/5. Reference: https://github.com/Flarette/angular/blob/feat/class-resolver/docs/class-resolver.md\n\n#### Provide a resolver\n\n```ts\nbootstrapApplication(App, {providers: [provideClassResolver(twMerge)]});\n\n// Angular calls it with one string per source, lowest priority first:\ntwMerge('px-4 py-2' /* component */, '' /* directives */, 'px-2' /* template */); // 'py-2 px-2'\n```\n\nAny `(...classes: string[]) => string` fits. Without a provider, classes merge as they do today. A library can scope its own resolver with `viewProviders`.\n\n#### Resolve a component's host\n\n```ts\n@Component({\n selector: 'ui-button',\n host: {class: 'px-4 py-2 bg-primary', '(class)': 'classes'},\n template: '\u003cng-content />',\n})\nexport class UiButton {\n readonly disabled = input(false);\n protected readonly classes: ClassCallback = (twMerge) => twMerge(this.disabled() && 'bg-muted');\n}\n```\n\n```html\n\u003cui-button class=\"px-2\" />\n\u003c!-- class=\"py-2 bg-primary px-2\" -->\n\n\u003cui-button class=\"px-2 bg-danger\" [disabled]=\"true\" />\n\u003c!-- class=\"py-2 px-2 bg-danger\": the template still wins over the component's extra bg-muted -->\n```\n\n#### Resolve in a template\n\n```html\n\u003cui-button class=\"px-2\" (class)=\"(twMerge) => twMerge('shadow-lg', busy() && 'opacity-50')\" />\n```\n\n`(class)=\"F\"` is shorthand for `(attr.class)=\"$event.resolve((F)($event.resolver))\"`. The full form gives a handler the classes of each source:\n\n```ts\n@Component({\n selector: 'app-toolbar',\n imports: [UiButton],\n template: `\u003cui-button class=\"px-2\" (attr.class)=\"resolveButton($event)\" />`,\n})\nexport class Toolbar {\n readonly busy = signal(false);\n readonly compact = signal(false);\n\n protected resolveButton(event: ClassResolveEvent) {\n // event.sources, after UiButton's own host resolver ran:\n // [['py-2', 'bg-primary'], [], ['px-2']]\n if (this.compact()) {\n event.resolve('px-1 py-1'); // exact classes, no resolver\n } else if (this.busy()) {\n event.resolve(event.resolver('opacity-50')); // twMerge with an extra class\n }\n // No resolve() call: the element keeps the classes as collected.\n }\n}\n```\n\nThe shorthand must return what its resolver returns. Both of these are compile errors:\n\n```html\n\u003cdiv (class)=\"(twMerge) => 'px-2'\">\u003c/div>\n\u003cdiv (class)=\"onClick\">\u003c/div>\n```\n\n#### Resolve every element\n\n```ts\nbootstrapApplication(App, {providers: [provideClassResolver(twMerge, withAutoResolution())]});\n\n@Component({\n // Off for this component's template.\n viewProviders: [provideClassResolver(twMerge)],\n})\nexport class LegacyWidget {}\n```\n\nEvery element that a component or directive matches resolves without a binding.\n\n#### Several resolvers on one element\n\nPriority: template > directives > component. Every resolver runs, lowest priority first, and each gets the previous result as its sources.\n\n```ts\n@Component({\n selector: 'ui-button',\n host: {class: 'px-4 bg-primary', '(class)': 'resolve'},\n template: '\u003cng-content />',\n})\nexport class UiButton {\n protected readonly resolve: ClassCallback = (twMerge) => twMerge('rounded');\n}\n\n@Directive({\n selector: '[tone]',\n host: {class: 'bg-gray-100', '(class)': 'resolve'},\n})\nexport class Tone {\n protected readonly resolve: ClassCallback = (twMerge) => twMerge('text-gray-900');\n}\n```\n\n```html\n\u003cui-button tone class=\"px-2 bg-white\" (class)=\"(twMerge) => twMerge('shadow')\" />\n```\n\n```text\nsources component 'px-4 bg-primary' directives 'bg-gray-100' template 'px-2 bg-white'\n1. UiButton component 'rounded' directives '' template 'px-2 bg-white'\n2. Tone component 'rounded' directives 'text-gray-900' template 'px-2 bg-white'\n3. template class=\"rounded text-gray-900 px-2 bg-white shadow\"\n```\n\nEach resolver's extra classes join its own source, so a component's extras are defaults that directives and the template override.\n\n#### Cost\n\nAbout 2 µs or less per resolved element in dev mode, not counting the resolver. Apps that don't use it don't bundle it.\n\n#### Breaking changes\n\n```ts\n@Directive({selector: '[dir]'})\nexport class Dir {\n // `\u003cdiv dir (class)=\"...\">` no longer listens to this output. NG8120 warns.\n readonly class = output\u003cstring>();\n}\n```\n\n```ts\n@HostListener('class') onClass() {} // Now a compile error.\n```\n\nOutputs named `class` are rare. Until #71661, such an output also dropped the element's `[class]` binding, so apps are unlikely to depend on one.\n\n### Alternatives considered\n\nToday's alternative is the userland workaround above. The open design choices, with why the prototype chose what it did:\n\n- **All attributes, not only `class`.** `aria-describedby` and other token lists have the same problem:\n\n ```ts\n provideAttributeResolvers({class: twMerge, 'aria-describedby': joinIds});\n ```\n\n The prototype resolves only `class`, because each attribute needs its own merge rule. Other `(attr.*)` names stay ordinary event bindings, so the namespace is still free for this.\n\n- **Styles.** A `(style)` counterpart that resolves conflicting style declarations. Not designed yet.\n- **The shorthand's shape.** Today the callback gets only the bound resolver. It could also get the sources:\n\n ```html\n \u003c!-- Today -->\n \u003cdiv (class)=\"(twMerge) => twMerge('px-2')\">\u003c/div>\n \u003c!-- With the sources as a second argument -->\n \u003cdiv (class)=\"(twMerge, sources) => twMerge(...sources[0], 'px-2')\">\u003c/div>\n \u003c!-- The first prototype: the callback got the sources and returned the classes,\n which a component method merged -->\n \u003cdiv (class)=\"(sources) => merge(sources, 'px-2')\">\u003c/div>\n ```\n\n Why the prototype chose today's shape:\n - **The callback returns what the resolver returns.** An earlier version used a handler's return value as the classes. No other Angular event does that, the value wasn't type-checked, and a later statement in a handler could replace the classes silently. The result is now an opaque type that only the resolver creates, so the compiler rejects `(twMerge) => 'px-2'`, and a cast fails in dev mode.\n - **The sources stay in the full form.** A handler that needs them uses `(attr.class)` and `$event.sources`. The shorthand stays one call for the common case.\n - **Extras join the caller's source.** Appending them after every source let a component's extras beat the consumer's classes.\n - **The resolver takes and returns strings:** `(...classes: string[]) => string`. A string is what a DOM attribute takes natively, for any attribute, so the same contract can serve a resolver for any attribute. `class` adds one step: Angular first reads extras and `resolve()` values the way a `[class]` binding does, so arrays, Sets, and object maps work, and `false` adds nothing. `twMerge` and a shadcn `cn` fit unchanged. A contract that takes `[class]` values fails `twMerge`'s types, and declaration merging for the extras' type breaks when two libraries augment it differently.\n\n- **Several resolvers.** Every resolver runs, and each gets the previous result back in its sources. Letting one resolver replace the others depended on `imports` order and dropped a library's logic silently. Letting a lower resolver delegate upward let its appended classes beat the consumer's.\n- **Naming.** `(class)` and `(attr.class)` collide with outputs named `class`. A name such as `(class.resolve)` avoids the breaking change, but it loses the short form, and `(class.x)` reads like a modifier, as in `(keydown.enter)`.\n- **Automatic resolution's reach.** It could resolve every element. The prototype chose not to: every element would pay the extra cost, and an element that no component or directive matches has only the template's classes, so it has no conflict to resolve. A template that needs a merge there can import `twMerge` and bind `[class]` to its result. Resolving every element would trade explicitness and performance for consistency and less boilerplate.\n\n\n### RFC\n\nThe class resolution is a special case of the #67519, Angular has no API for resolving host conflicts and relies on defaults (classes join, Id's override). Other frameworks avoid it by not having the selectors, in angular it is a friction point. The full feature could resolve any attribute, with shorthands such as `(class)` for the common cases or cases with special defaults ex: (class) accepting same types as [class]. Before proceeding with the implementation i want to hear the opinion of the maintainers and users if this API fits the angular vision\n","author":{"url":"https://github.com/Flarette","@type":"Person","name":"Flarette"},"datePublished":"2026-11-08T21:33:58Z","url":"https://github.com/angular/angular/issues/71888"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:2d0eb349-4bed-7354-5471-bc3ec95892b4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A6A4:23CF06:F3ED90:13A0D5D:6B00FDF9 |
| html-safe-nonce | 9f711485b91fb9d98ebb26bc69ab7344b8cb08e4ba5ef30d6d7cc035503cecb5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNkE0OjIzQ0YwNjpGM0VEOTA6MTNBMEQ1RDo2QjAwRkRGOSIsInZpc2l0b3JfaWQiOiI0MzIyMDk0MTk5NTY3NjEzNDMzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | e523d0d43e79a7c10a2be703c04c2a047ae7a508e3240401afbd9ffc6912f9df |
| hovercard-subject-tag | issue:6275877673 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/71888/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c9b38fc50640427c8ec8ca9f96f83c0cb36ec1a4c93e271d5ebc4e675f109133/angular/angular/issues/71888 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c9b38fc50640427c8ec8ca9f96f83c0cb36ec1a4c93e271d5ebc4e675f109133/angular/angular/issues/71888 |
| og:image:alt | Which @angular/* package(s) are relevant/related to the feature request? core, compiler, compiler-cli Description Angular applies the union of an element's classes from the component's host, its ot... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Flarette |
| hostname | github.com |
| expected-hostname | github.com |
| None | e208d86428ec00a57ade75058166297440e8128a01e5287c31a53dc042bbf187 |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7dafd017fb7809a839c443dba08622af38e63c09 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width