Skip to content

refactor(core): add context support for foreign components#69502

Closed
leonsenft wants to merge 3 commits into
angular:mainfrom
leonsenft:foreign-component-context
Closed

refactor(core): add context support for foreign components#69502
leonsenft wants to merge 3 commits into
angular:mainfrom
leonsenft:foreign-component-context

Conversation

@leonsenft

@leonsenft leonsenft commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Enable foreign components to receive and propagate contextual data across framework boundaries.

Previously, foreign render functions only accepted component properties, and foreign content projection instructions (ɵɵforeignContent / ɵɵforeignContentFn) did not provide any mechanism to expose foreign framework context to projected Angular embedded views.

With this change:

  • Update ForeignRenderFn and ForeignComponent interfaces to accept an optional context parameter and an optional GET_CONTEXT symbol method.

  • Introduce FOREIGN_CONTEXT injection token and provideForeignRootContext helper to configure root context in Angular's DI hierarchy.

  • Update ɵɵforeignComponent instruction to resolve FOREIGN_CONTEXT from the injection tree and pass it to the foreign component's render function.

  • Update ɵɵforeignContent and ɵɵforeignContentFn instructions to wrap embedded view creation with a ForeignContextInjector when GET_CONTEXT is present.

Furthermore, foreign render functions are no longer run inside an Angular injection context, since it's expected they use the foreign context support directly.

@leonsenft leonsenft added the area: core Issues related to the framework runtime label Jun 24, 2026
@ngbot ngbot Bot added this to the Backlog milestone Jun 24, 2026
@leonsenft leonsenft added area: compiler Issues related to `ngc`, Angular's template compiler target: patch This PR is targeted for the next patch release target: minor This PR is targeted for the next minor release and removed target: patch This PR is targeted for the next patch release labels Jun 24, 2026
@leonsenft leonsenft force-pushed the foreign-component-context branch 7 times, most recently from cd635e0 to b23131f Compare June 26, 2026 17:16
@leonsenft leonsenft marked this pull request as ready for review June 26, 2026 19:39
@leonsenft leonsenft marked this pull request as draft June 26, 2026 19:39
Enable foreign components to receive and propagate contextual data
across framework boundaries.

Previously, foreign render functions only accepted component properties,
and foreign content projection instructions (`ɵɵforeignContent` /
`ɵɵforeignContentFn`) did not provide any mechanism to expose foreign
framework context to projected Angular embedded views.

With this change:

- Update `ForeignRenderFn` and `ForeignComponent` interfaces to accept
  an optional context parameter and an optional `GET_CONTEXT` symbol
  method.

- Introduce `FOREIGN_CONTEXT` injection token and
  `provideForeignRootContext` helper to configure root context in
  Angular's DI hierarchy.

- Update `ɵɵforeignComponent` instruction to resolve `FOREIGN_CONTEXT`
  from the injection tree and pass it to the foreign component's render
  function.

- Update `ɵɵforeignContent` and `ɵɵforeignContentFn` instructions to
  wrap embedded view creation with a `ForeignContextInjector` when
  `GET_CONTEXT` is present.

Furthermore, foreign render functions are no longer run inside an
Angular injection context, since it's expected they use the foreign
context support directly.
`@content(name)` -> `@content (name)` to align with other block syntax.
@leonsenft leonsenft force-pushed the foreign-component-context branch from b23131f to 0f319a0 Compare June 29, 2026 21:31
@leonsenft leonsenft requested a review from alxhub June 29, 2026 21:34
@leonsenft leonsenft marked this pull request as ready for review June 29, 2026 21:34
* @param contextFactory The factory function that creates the root context object.
* @template TContext The foreign context type.
*/
export function provideForeignRootContext<TContext>(contextFactory: () => TContext): Provider {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This limits us to a singleton foreign context per application, right? So if we ended up in an app with multiple foreign frameworks, things would break down.

One way around this is to have a global Map<string, InjectionToken<unknown>> that maintains a token-per-stringly-typed-framework, and accept the framework name in provideForeignRootContext. This assumes we can identify the foreign framework at the usage site in order to look up this key.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's a great suggestion. I'll work on this in a follow-up PR.

Comment thread packages/core/src/render3/foreign_context.ts Outdated
@alxhub alxhub added the action: merge The PR is ready for merge by the caretaker label Jun 29, 2026
@angular-robot angular-robot Bot requested a review from alxhub June 29, 2026 23:07
@alxhub

alxhub commented Jun 30, 2026

Copy link
Copy Markdown
Member

This PR was merged into the repository. The changes were merged into the following branches:

@alxhub alxhub closed this in 555fc20 Jun 30, 2026
alxhub pushed a commit that referenced this pull request Jun 30, 2026
`@content(name)` -> `@content (name)` to align with other block syntax.

PR Close #69502
@leonsenft leonsenft deleted the foreign-component-context branch June 30, 2026 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: merge The PR is ready for merge by the caretaker area: compiler Issues related to `ngc`, Angular's template compiler area: core Issues related to the framework runtime target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants