Skip to content

feat(angular): add onElementConnected hook for universal elements - #4143

Merged
josemontespg merged 4 commits into
mainfrom
angular-element-connected-hook
Nov 13, 2026
Merged

josemontespg merged 4 commits into
mainfrom
angular-element-connected-hook

Conversation

@josemontespg

@josemontespg josemontespg commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

This adds a hook so an app can decorate universal elements it does not own, for example to add analytics attributes, without subclassing them.

A catalog entry can set onElementConnected(element, context, injector). When a universal element first connects with a node or context, it dispatches a bubbling, composed a2ui-element-connected event once: A2uiLitElement does it itself, and renderA2uiNode does it for the elements it creates, whatever their base class. The nearest ComponentHostComponent catches it, stops it from reaching outer hosts, and calls the entry's callback with its own injector. That covers root elements and the ones universal containers create.

Changes

  • @a2ui/web_core: export A2UI_ELEMENT_CONNECTED_EVENT and the idempotent dispatchA2uiElementConnected helper from the universal entry point. A2uiLitElement and renderA2uiNode call it, and custom containers that insert children by hand can too. The jsdom test setup exposes jsdom's CustomEvent.
  • @a2ui/angular: add the optional onElementConnected field to AngularComponentImplementation, and forward the event to it from ComponentHostComponent, which also dispatches it for its own root element.
  • src/catalog/types.ts joins the boundary allowlist in eslint.boundary.mjs, because the callback receives a ComponentContext. A signature that takes the ComponentNode instead would avoid that; happy to switch if reviewers prefer it.

Verification

  • yarn --cwd typescript/core/web_core test and the Angular karma suite (ng test lib), with the new specs confirmed in the karma output.
  • yarn lint in both packages, the web_core and Angular builds, and ./scripts/fix_format.sh --check.

Part of #4148.

@github-project-automation github-project-automation Bot moved this to Todo in A2UI Nov 10, 2026
@josemontespg
josemontespg force-pushed the angular-element-connected-hook branch from 17729e7 to 1569ef5 Compare November 10, 2026 21:35

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the A2UI_ELEMENT_CONNECTED_EVENT dispatched by A2uiLitElement upon connecting with a context or node, and adds an onElementConnected hook to AngularComponentImplementation so that Angular hosts can decorate universal elements. The review feedback highlights a critical bug where the connected event is missed if the context is assigned asynchronously after the element connects, recommending dispatching it during updates as well. Additionally, a potential TypeError was identified in ComponentHostComponent due to a missing guard on componentModel, and a test update was suggested to cover the asynchronous context assignment scenario.

@josemontespg
josemontespg force-pushed the angular-element-connected-hook branch 2 times, most recently from 373fba4 to 5f01440 Compare November 11, 2026 21:57
@josemontespg
josemontespg force-pushed the angular-universal-google3-readiness branch from d71fe00 to abba9ea Compare November 11, 2026 22:02
@josemontespg
josemontespg force-pushed the angular-element-connected-hook branch from 5f01440 to e701747 Compare November 11, 2026 22:05
@josemontespg
josemontespg force-pushed the angular-universal-google3-readiness branch from abba9ea to dc6790f Compare November 13, 2026 18:32
@josemontespg
josemontespg force-pushed the angular-element-connected-hook branch from 63fed77 to 90c5968 Compare November 13, 2026 18:34
@josemontespg
josemontespg requested a review from ditman November 13, 2026 19:12
@josemontespg
josemontespg marked this pull request as ready for review November 13, 2026 19:12
@josemontespg
josemontespg changed the base branch from angular-universal-google3-readiness to main November 13, 2026 19:26
@josemontespg
josemontespg force-pushed the angular-element-connected-hook branch from 90c5968 to f44a72d Compare November 13, 2026 19:27

@ditman ditman left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Clean design across @a2ui/web_core/universal and @a2ui/angular! Good use of WeakSet!

…s handled

Use event.composedPath()[0] so elements inside shadow roots reach the hook,
and replace stopPropagation() with a WeakSet marker so ancestor hosts skip
handled events while other listeners still observe them.
Add dispatchA2uiElementConnected to web_core, an idempotent helper that
dispatches A2UI_ELEMENT_CONNECTED_EVENT once per element when it is
connected with a node or context. A2uiLitElement, renderA2uiNode and the
Angular ComponentHostComponent call it, so onElementConnected runs for
universal elements of any base class.
@josemontespg
josemontespg force-pushed the angular-element-connected-hook branch from f44a72d to 74681cd Compare November 13, 2026 23:32
@josemontespg
josemontespg merged commit 6293d18 into main Nov 13, 2026
49 checks passed
@josemontespg
josemontespg deleted the angular-element-connected-hook branch November 13, 2026 23:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants