feat(modal): add IonModalToken for injecting modal elements in Angular components (#30474)

Issue number: resolves #internal

---------

<!-- Please do not submit updates to dependencies unless it fixes an
issue. -->

<!-- Please try to limit your pull request to one type (bugfix, feature,
etc). Submit multiple pull requests if needed. -->

## What is the current behavior?
<!-- Please describe the current behavior that you are modifying. -->

Currently, we provide no injection access to angular modals, which makes
it difficult to connect to their events normally.

## What is the new behavior?
<!-- Please describe the behavior or changes that are being added by
this PR. -->

This is an attempt to allow easier programmatic access to the internals
of injected modals.

## Does this introduce a breaking change?

- [ ] Yes
- [X] No

<!--
  If this introduces a breaking change:
1. Describe the impact and migration path for existing applications
below.
  2. Update the BREAKING.md file with the breaking change.
3. Add "BREAKING CHANGE: [...]" to the commit description when merging.
See
https://github.com/ionic-team/ionic-framework/blob/main/docs/CONTRIBUTING.md#footer
for more information.
-->


## Other information

<!-- Any other information that is important to this PR such as
screenshots of how the component looks before and after the change. -->

Dev build: `8.6.2-dev.11749830167.1460aa73`
This commit is contained in:
Shane
2025-06-23 13:15:48 -07:00
committed by Brandy Smith
parent b154f4ed09
commit 30d1910d6e
18 changed files with 153 additions and 17 deletions

View File

@ -1,13 +1,13 @@
import {
ApplicationRef,
NgZone,
Injectable,
Injector,
ComponentRef,
createComponent,
EnvironmentInjector,
inject,
createComponent,
Injectable,
InjectionToken,
ComponentRef,
Injector,
NgZone,
} from '@angular/core';
import {
FrameworkDelegate,
@ -22,6 +22,9 @@ import { NavParams } from '../directives/navigation/nav-params';
import { ConfigToken } from './config';
// Token for injecting the modal element
export const IonModalToken = new InjectionToken<HTMLIonModalElement>('IonModalToken');
// TODO(FW-2827): types
@Injectable()
@ -142,8 +145,19 @@ export const attachView = (
* The modern approach is to access the data directly
* from the component's class instance.
*/
const providers = getProviders(params);
// If this is an ion-modal, provide the modal element as an injectable
// so components inside the modal can inject it directly
if (container.tagName.toLowerCase() === 'ion-modal') {
providers.push({
provide: IonModalToken,
useValue: container,
});
}
const childInjector = Injector.create({
providers: getProviders(params),
providers,
parent: injector,
});