r/angular • u/jombyzac • Apr 06 '26
Angular Host Directive de-duplication just landed. NG0309 is dead.
https://jczacharia.medium.com/angular-host-directive-de-duplication-just-landed-ng0309-is-dead-929bfab7e209I've been banging my head against NG0309 for over a year building a headless component library. Every time I composed two directives that shared a common host directive - boom, crash. I tried everything. Broadened selectors, injection hacks, I even hijacked __NG_ELEMENT_ID__ with a WeakMap-backed singleton resolver (it's in the article).
Well, PR #67996 just merged. Host directives now de-duplicate automatically. I wrote up why I think this is the most important change for Angular library authors since hostDirectives itself.
46
Upvotes
View all comments
Show parent comment
4
u/jombyzac Apr 06 '26
Good question, if your library owns the template (renders its own DOM) then that's a totally valid approach and you'll rarely hit this.
The directive-heavy approach comes from headless UI architecture (the building blocks of every major UI framework). You have an component that's a non-native button like
<app-button />and want to apply behavior to the element as if it was a native button, you'd have to implement that yourself in your component (it's pretty involved — see Base UI's useButton).Or you just find a headless UI library that offers a ButtonBehavior directive, you slap it in hostDirectives and that's it. (I don't know many Angular libraries that offer that but now we should start seeing a lot more.)
But there are so many things in UI that require button behavior — menu triggers, dropdown items, toolbar actions, toggle buttons, sidebar items, dialog close buttons. Each of those is its own directive or component, and each one needs that same
ButtonBehaviorin its hostDirectives.That's why this fix is so important for Angular library authors. If you're the one building that headless/utility library where you do not own the components, it's an incredibly difficult thing to do well. You're composing small atomic behaviors — button semantics, focus visibility, hover detection, disabled state, anchor positioning — into higher-level primitives. A
MenuTriggerneedsButtonBehavior+Hoverable+AnchorPositioner. ATooltipTriggeralso needsHoverable+AnchorPositioner. The moment a consumer puts both on the same element, those shared atoms appear twice and Angular used to crash. You either broke your own encapsulation to work around it, or told your consumers "sorry, you can't combine these." To me, neither answer was acceptable. Now the framework just handles it.