Skip to content

Commit 278c2d7

Browse files
committed
refactor(modal): moving to @listen instead of a manual resize listener
1 parent d3b968b commit 278c2d7

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

core/src/components/modal/modal.tsx

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ComponentInterface, EventEmitter } from '@stencil/core';
2-
import { Component, Element, Event, Host, Method, Prop, State, Watch, h, writeTask } from '@stencil/core';
2+
import { Component, Element, Event, Host, Listen, Method, Prop, State, Watch, h, writeTask } from '@stencil/core';
33
import { findIonContent, printIonContentErrorMsg } from '@utils/content';
44
import { CoreDelegate, attachComponent, detachComponent } from '@utils/framework-delegate';
55
import { raf, inheritAttributes, hasLazyBuild, getElementRoot } from '@utils/helpers';
@@ -92,7 +92,6 @@ export class Modal implements ComponentInterface, OverlayInterface {
9292
private gestureAnimationDismissing = false;
9393

9494
// View transition properties for handling portrait/landscape switches
95-
private resizeListener?: () => void;
9695
private currentViewIsPortrait?: boolean;
9796
private viewTransitionAnimation?: Animation;
9897
private resizeTimeout?: any;
@@ -268,6 +267,19 @@ export class Modal implements ComponentInterface, OverlayInterface {
268267
}
269268
}
270269

270+
@Listen('resize', { target: 'window' })
271+
onWindowResize() {
272+
// Only handle resize for iOS card modals when no custom animations are provided
273+
if (getIonMode(this) !== 'ios' || !this.presentingElement || this.enterAnimation || this.leaveAnimation) {
274+
return;
275+
}
276+
277+
clearTimeout(this.resizeTimeout);
278+
this.resizeTimeout = setTimeout(() => {
279+
this.handleViewTransition();
280+
}, 50); // Debounce to avoid excessive calls during active resizing
281+
}
282+
271283
/**
272284
* If `true`, the component passed into `ion-modal` will
273285
* automatically be mounted when the modal is created. The
@@ -983,16 +995,6 @@ export class Modal implements ComponentInterface, OverlayInterface {
983995

984996
// Set initial view state
985997
this.currentViewIsPortrait = window.innerWidth < 768;
986-
987-
// Create debounced resize handler
988-
this.resizeListener = () => {
989-
clearTimeout(this.resizeTimeout);
990-
this.resizeTimeout = setTimeout(() => {
991-
this.handleViewTransition();
992-
}, 50); // Debounce to avoid excessive calls during active resizing
993-
};
994-
995-
window.addEventListener('resize', this.resizeListener);
996998
}
997999

9981000
private handleViewTransition() {
@@ -1048,11 +1050,6 @@ export class Modal implements ComponentInterface, OverlayInterface {
10481050
}
10491051

10501052
private cleanupViewTransitionListener() {
1051-
if (this.resizeListener) {
1052-
window.removeEventListener('resize', this.resizeListener);
1053-
this.resizeListener = undefined;
1054-
}
1055-
10561053
// Clear any pending resize timeout
10571054
if (this.resizeTimeout) {
10581055
clearTimeout(this.resizeTimeout);

0 commit comments

Comments
 (0)