Skip to content

Commit 32e75ee

Browse files
committed
fix(material/tabs): attach content inside the zone
The tabs run their animation events outside the zone which means that attaching the content as a result will be outside the zone as well. These changes bring it back into the zone to ensure that things like error handling work correctly. Fixes #31867.
1 parent 6275d71 commit 32e75ee

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/material/tabs/tab-body.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {_animationsDisabled} from '../core';
3939
@Directive({selector: '[matTabBodyHost]'})
4040
export class MatTabBodyPortal extends CdkPortalOutlet implements OnInit, OnDestroy {
4141
private _host = inject(MatTabBody);
42+
private _ngZone = inject(NgZone);
4243

4344
/** Subscription to events for when the tab body begins centering. */
4445
private _centeringSub = Subscription.EMPTY;
@@ -59,13 +60,15 @@ export class MatTabBodyPortal extends CdkPortalOutlet implements OnInit, OnDestr
5960
.pipe(startWith(this._host._isCenterPosition()))
6061
.subscribe((isCentering: boolean) => {
6162
if (this._host._content && isCentering && !this.hasAttached()) {
62-
this.attach(this._host._content);
63+
// Attach in the zone since the events from the tab body may be happening outside.
64+
// See: https://github.com/angular/components/issues/31867
65+
this._ngZone.run(() => this.attach(this._host._content));
6366
}
6467
});
6568

6669
this._leavingSub = this._host._afterLeavingCenter.subscribe(() => {
6770
if (!this._host.preserveContent) {
68-
this.detach();
71+
this._ngZone.run(() => this.detach());
6972
}
7073
});
7174
}
@@ -117,7 +120,7 @@ export type MatTabBodyOriginState = 'left' | 'right';
117120
changeDetection: ChangeDetectionStrategy.Default,
118121
host: {
119122
'class': 'mat-mdc-tab-body',
120-
// In most cases the `visibility: hidden` that we set on the off-screen content is enough
123+
// In most cases the `hidden` that we set on the off-screen content is enough
121124
// to stop interactions with it, but if a child element sets its own `visibility`, it'll
122125
// override the one from the parent. This ensures that even those elements will be removed
123126
// from the accessibility tree.

0 commit comments

Comments
 (0)