Skip to content

Commit 764030d

Browse files
authored
fix(multiple): memory leak when forcing focus (#24520)
When we force focus on an element, we were adding a couple of event listeners that were never removed.
1 parent ff01196 commit 764030d

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

src/cdk-experimental/dialog/dialog-container.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,14 @@ export class CdkDialogContainer extends BasePortalOutlet implements OnDestroy {
267267
element.tabIndex = -1;
268268
// The tabindex attribute should be removed to avoid navigating to that element again
269269
this._ngZone.runOutsideAngular(() => {
270-
element.addEventListener('blur', () => element.removeAttribute('tabindex'));
271-
element.addEventListener('mousedown', () => element.removeAttribute('tabindex'));
270+
const callback = () => {
271+
element.removeEventListener('blur', callback);
272+
element.removeEventListener('mousedown', callback);
273+
element.removeAttribute('tabindex');
274+
};
275+
276+
element.addEventListener('blur', callback);
277+
element.addEventListener('mousedown', callback);
272278
});
273279
}
274280
element.focus(options);

src/material/bottom-sheet/bottom-sheet-container.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,14 @@ export class MatBottomSheetContainer extends BasePortalOutlet implements OnDestr
210210
element.tabIndex = -1;
211211
// The tabindex attribute should be removed to avoid navigating to that element again
212212
this._ngZone.runOutsideAngular(() => {
213-
element.addEventListener('blur', () => element.removeAttribute('tabindex'));
214-
element.addEventListener('mousedown', () => element.removeAttribute('tabindex'));
213+
const callback = () => {
214+
element.removeEventListener('blur', callback);
215+
element.removeEventListener('mousedown', callback);
216+
element.removeAttribute('tabindex');
217+
};
218+
219+
element.addEventListener('blur', callback);
220+
element.addEventListener('mousedown', callback);
215221
});
216222
}
217223
element.focus(options);

src/material/dialog/dialog-container.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,14 @@ export abstract class _MatDialogContainerBase extends BasePortalOutlet {
174174
element.tabIndex = -1;
175175
// The tabindex attribute should be removed to avoid navigating to that element again
176176
this._ngZone.runOutsideAngular(() => {
177-
element.addEventListener('blur', () => element.removeAttribute('tabindex'));
178-
element.addEventListener('mousedown', () => element.removeAttribute('tabindex'));
177+
const callback = () => {
178+
element.removeEventListener('blur', callback);
179+
element.removeEventListener('mousedown', callback);
180+
element.removeAttribute('tabindex');
181+
};
182+
183+
element.addEventListener('blur', callback);
184+
element.addEventListener('mousedown', callback);
179185
});
180186
}
181187
element.focus(options);

src/material/sidenav/drawer.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,14 @@ export class MatDrawer implements AfterViewInit, AfterContentChecked, OnDestroy
388388
element.tabIndex = -1;
389389
// The tabindex attribute should be removed to avoid navigating to that element again
390390
this._ngZone.runOutsideAngular(() => {
391-
element.addEventListener('blur', () => element.removeAttribute('tabindex'));
392-
element.addEventListener('mousedown', () => element.removeAttribute('tabindex'));
391+
const callback = () => {
392+
element.removeEventListener('blur', callback);
393+
element.removeEventListener('mousedown', callback);
394+
element.removeAttribute('tabindex');
395+
};
396+
397+
element.addEventListener('blur', callback);
398+
element.addEventListener('mousedown', callback);
393399
});
394400
}
395401
element.focus(options);

0 commit comments

Comments
 (0)