Skip to content

Commit deb860e

Browse files
authored
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`
1 parent 70dbcc9 commit deb860e

File tree

18 files changed

+153
-17
lines changed

18 files changed

+153
-17
lines changed

packages/angular/common/src/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
export { MenuController } from './providers/menu-controller';
21
export { DomController } from './providers/dom-controller';
2+
export { MenuController } from './providers/menu-controller';
33
export { NavController } from './providers/nav-controller';
44

55
export { Config, ConfigToken } from './providers/config';
66
export { Platform } from './providers/platform';
77

8-
export { bindLifecycleEvents, AngularDelegate } from './providers/angular-delegate';
8+
export { AngularDelegate, bindLifecycleEvents, IonModalToken } from './providers/angular-delegate';
99

1010
export type { IonicWindow } from './types/interfaces';
11-
export type { ViewWillEnter, ViewWillLeave, ViewDidEnter, ViewDidLeave } from './types/ionic-lifecycle-hooks';
11+
export type { ViewDidEnter, ViewDidLeave, ViewWillEnter, ViewWillLeave } from './types/ionic-lifecycle-hooks';
1212

1313
export { NavParams } from './directives/navigation/nav-params';
1414

15-
export { IonPopover } from './overlays/popover';
1615
export { IonModal } from './overlays/modal';
16+
export { IonPopover } from './overlays/popover';
1717

1818
export { IonRouterOutlet, provideComponentInputBinding } from './directives/navigation/router-outlet';
1919

20+
export * from './directives/control-value-accessors';
2021
export { IonBackButton } from './directives/navigation/back-button';
22+
export { IonNav } from './directives/navigation/nav';
2123
export {
2224
RouterLinkDelegateDirective,
2325
RouterLinkWithHrefDelegateDirective,
2426
} from './directives/navigation/router-link-delegate';
25-
export { IonNav } from './directives/navigation/nav';
2627
export { IonTabs } from './directives/navigation/tabs';
27-
export * from './directives/control-value-accessors';
2828

2929
export { ProxyCmp } from './utils/proxy';
3030

31-
export { IonicRouteStrategy } from './utils/routing';
3231
export { OverlayBaseController } from './utils/overlay';
32+
export { IonicRouteStrategy } from './utils/routing';
3333

3434
export { raf } from './utils/util';

packages/angular/common/src/providers/angular-delegate.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import {
22
ApplicationRef,
3-
NgZone,
4-
Injectable,
5-
Injector,
3+
ComponentRef,
4+
createComponent,
65
EnvironmentInjector,
76
inject,
8-
createComponent,
7+
Injectable,
98
InjectionToken,
10-
ComponentRef,
9+
Injector,
10+
NgZone,
1111
} from '@angular/core';
1212
import {
1313
FrameworkDelegate,
@@ -22,6 +22,9 @@ import { NavParams } from '../directives/navigation/nav-params';
2222

2323
import { ConfigToken } from './config';
2424

25+
// Token for injecting the modal element
26+
export const IonModalToken = new InjectionToken<HTMLIonModalElement>('IonModalToken');
27+
2528
// TODO(FW-2827): types
2629

2730
@Injectable()
@@ -142,8 +145,19 @@ export const attachView = (
142145
* The modern approach is to access the data directly
143146
* from the component's class instance.
144147
*/
148+
const providers = getProviders(params);
149+
150+
// If this is an ion-modal, provide the modal element as an injectable
151+
// so components inside the modal can inject it directly
152+
if (container.tagName.toLowerCase() === 'ion-modal') {
153+
providers.push({
154+
provide: IonModalToken,
155+
useValue: container,
156+
});
157+
}
158+
145159
const childInjector = Injector.create({
146-
providers: getProviders(params),
160+
providers,
147161
parent: injector,
148162
});
149163

packages/angular/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export {
2626
AngularDelegate,
2727
NavParams,
2828
IonicRouteStrategy,
29+
IonModalToken,
2930
ViewWillEnter,
3031
ViewWillLeave,
3132
ViewDidEnter,

packages/angular/standalone/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export {
2121
Config,
2222
Platform,
2323
NavParams,
24+
IonModalToken,
2425
IonicRouteStrategy,
2526
ViewWillEnter,
2627
ViewDidEnter,

packages/angular/test/apps/ng18/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
"@angular/platform-server": "^18.0.0",
3030
"@angular/router": "^18.0.0",
3131
"@angular/ssr": "^18.0.0",
32-
"@ionic/angular": "^8.0.0",
33-
"@ionic/angular-server": "^8.0.0",
32+
"@ionic/angular": "^8.6.0",
33+
"@ionic/angular-server": "^8.6.0",
3434
"core-js": "^3.33.2",
3535
"express": "^4.15.2",
3636
"ionicons": "^7.2.0",

packages/angular/test/apps/ng19/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
"@angular/platform-server": "^19.0.0",
3030
"@angular/router": "^19.0.0",
3131
"@angular/ssr": "^19.0.2",
32-
"@ionic/angular": "^8.4.0",
33-
"@ionic/angular-server": "^8.4.0",
32+
"@ionic/angular": "^8.6.0",
33+
"@ionic/angular-server": "^8.6.0",
3434
"core-js": "^3.33.2",
3535
"express": "^4.18.2",
3636
"ionicons": "^7.2.0",
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<p>
2+
modal works!
3+
<ion-button (click)="close()">Close Modal</ion-button>
4+
</p>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Component, Inject } from '@angular/core';
2+
import { IonButton, IonModalToken } from "@ionic/angular/standalone";
3+
4+
@Component({
5+
selector: 'app-modal',
6+
templateUrl: './modal.component.html',
7+
imports: [IonButton],
8+
standalone: true,
9+
})
10+
export class ModalComponent {
11+
constructor(@Inject(IonModalToken) private modalToken: HTMLIonModalElement) {
12+
this.modalToken.onDidDismiss().then(() => {
13+
console.log('Modal dismissed');
14+
});
15+
}
16+
17+
public close() {
18+
this.modalToken.dismiss();
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<ion-button (click)="open()">Open Modal</ion-button>
2+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Component } from '@angular/core';
2+
import { IonButton } from '@ionic/angular/standalone';
3+
import { ProgrammaticModalService } from './programmatic-modal.service';
4+
5+
@Component({
6+
selector: 'app-test',
7+
templateUrl: './programmatic-modal.component.html',
8+
standalone: true,
9+
imports: [IonButton]
10+
})
11+
export class ProgrammaticModalComponent {
12+
constructor(private modalService: ProgrammaticModalService) {}
13+
14+
public open() {
15+
this.modalService.open();
16+
}
17+
}
18+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { inject, Injectable } from '@angular/core';
2+
import { ModalController } from "@ionic/angular/standalone";
3+
import { ModalComponent } from "./modal/modal.component";
4+
5+
@Injectable({
6+
providedIn: 'root'
7+
})
8+
export class ProgrammaticModalService {
9+
#modalController = inject(ModalController);
10+
11+
async open() {
12+
const modal = await this.#modalController.create({
13+
component: ModalComponent,
14+
focusTrap: true,
15+
});
16+
await modal.present();
17+
}
18+
}

packages/angular/test/base/src/app/standalone/app-standalone/app.routes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const routes: Routes = [
1111
{ path: 'action-sheet-controller', loadComponent: () => import('../action-sheet-controller/action-sheet-controller.component').then(c => c.ActionSheetControllerComponent) },
1212
{ path: 'popover', loadComponent: () => import('../popover/popover.component').then(c => c.PopoverComponent) },
1313
{ path: 'modal', loadComponent: () => import('../modal/modal.component').then(c => c.ModalComponent) },
14+
{ path: 'programmatic-modal', loadComponent: () => import('../programmatic-modal/programmatic-modal.component').then(c => c.ProgrammaticModalComponent) },
1415
{ path: 'router-outlet', loadComponent: () => import('../router-outlet/router-outlet.component').then(c => c.RouterOutletComponent) },
1516
{ path: 'back-button', loadComponent: () => import('../back-button/back-button.component').then(c => c.BackButtonComponent) },
1617
{ path: 'router-link', loadComponent: () => import('../router-link/router-link.component').then(c => c.RouterLinkComponent) },

packages/angular/test/base/src/app/standalone/home-page/home-page.component.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@
8585
Modal Test
8686
</ion-label>
8787
</ion-item>
88+
<ion-item routerLink="/standalone/programmatic-modal">
89+
<ion-label>
90+
Programmatic Modal Test
91+
</ion-label>
92+
</ion-item>
8893
<ion-item routerLink="/standalone/overlay-controllers">
8994
<ion-label>
9095
Overlay Controllers Test
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<p>
2+
modal works!
3+
</p>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Component } from '@angular/core';
2+
import { IonButton } from "@ionic/angular/standalone";
3+
4+
@Component({
5+
selector: 'app-modal',
6+
templateUrl: './modal.component.html',
7+
imports: [IonButton],
8+
standalone: true,
9+
})
10+
export class ModalComponent {
11+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<ion-button (click)="open()">Open Modal</ion-button>
2+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Component } from '@angular/core';
2+
import { IonButton } from '@ionic/angular/standalone';
3+
import { ProgrammaticModalService } from './programmatic-modal.service';
4+
5+
@Component({
6+
selector: 'app-test',
7+
templateUrl: './programmatic-modal.component.html',
8+
standalone: true,
9+
imports: [IonButton]
10+
})
11+
export class ProgrammaticModalComponent {
12+
constructor(private modalService: ProgrammaticModalService) {}
13+
14+
public open() {
15+
this.modalService.open();
16+
}
17+
}
18+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { inject, Injectable } from '@angular/core';
2+
import { ModalController } from "@ionic/angular/standalone";
3+
import { ModalComponent } from "./modal/modal.component";
4+
5+
@Injectable({
6+
providedIn: 'root'
7+
})
8+
export class ProgrammaticModalService {
9+
#modalController = inject(ModalController);
10+
11+
async open() {
12+
const modal = await this.#modalController.create({
13+
component: ModalComponent,
14+
focusTrap: true,
15+
});
16+
await modal.present();
17+
}
18+
}

0 commit comments

Comments
 (0)