Skip to content

Commit

Permalink
fix(multiple): move afterRender calls back outside zone
Browse files Browse the repository at this point in the history
Updates some calls to afterRender / afterNextRender to explicitly run
outside the NgZone. These usages were originally explcitily run outside,
but the explicit call was removed when they were updated to use the
afterRender / afterNextRender function, since at the time it was
automatically outside the zone. The semantics of afterRender /
afterNextRendef have since changed so this is no longer true. Therefore
this PR restores the original explicit run outside.
  • Loading branch information
mmalerba committed May 1, 2024
1 parent dde3793 commit f37bad5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
17 changes: 9 additions & 8 deletions src/cdk/overlay/overlay-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
*/

import {Direction, Directionality} from '@angular/cdk/bidi';
import {coerceArray, coerceCssPixelValue} from '@angular/cdk/coercion';
import {ComponentPortal, Portal, PortalOutlet, TemplatePortal} from '@angular/cdk/portal';
import {Location} from '@angular/common';
import {
AfterRenderRef,
ComponentRef,
EmbeddedViewRef,
EnvironmentInjector,
NgZone,
afterNextRender,
afterRender,
untracked,
AfterRenderRef,
} from '@angular/core';
import {Location} from '@angular/common';
import {Observable, Subject, merge, SubscriptionLike, Subscription} from 'rxjs';
import {Observable, Subject, Subscription, SubscriptionLike, merge} from 'rxjs';
import {takeUntil} from 'rxjs/operators';
import {OverlayKeyboardDispatcher} from './dispatchers/overlay-keyboard-dispatcher';
import {OverlayOutsideClickDispatcher} from './dispatchers/overlay-outside-click-dispatcher';
import {OverlayConfig} from './overlay-config';
import {coerceCssPixelValue, coerceArray} from '@angular/cdk/coercion';
import {PositionStrategy} from './position/position-strategy';
import {ScrollStrategy} from './scroll';

Expand Down Expand Up @@ -495,10 +495,11 @@ export class OverlayRef implements PortalOutlet {
// Run this outside the Angular zone because there's nothing that Angular cares about.
// If it were to run inside the Angular zone, every test that used Overlay would have to be
// either async or fakeAsync.
this._backdropTimeout = this._ngZone.runOutsideAngular(() =>
setTimeout(() => {
this._disposeBackdrop(backdropToDetach);
}, 500),
this._backdropTimeout = this._ngZone.runOutsideAngular(
() =>
setTimeout(() => {
this._disposeBackdrop(backdropToDetach);
}, 500) as any,
);
}

Expand Down
38 changes: 20 additions & 18 deletions src/material/datepicker/calendar-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
*/

import {Platform, normalizePassiveListenerOptions} from '@angular/cdk/platform';
import {NgClass} from '@angular/common';
import {
AfterViewChecked,
ChangeDetectionStrategy,
Component,
ElementRef,
EventEmitter,
Injector,
Input,
Output,
ViewEncapsulation,
NgZone,
OnChanges,
SimpleChanges,
OnDestroy,
AfterViewChecked,
inject,
Output,
SimpleChanges,
ViewEncapsulation,
afterNextRender,
Injector,
inject,
} from '@angular/core';
import {NgClass} from '@angular/common';

/** Extra CSS classes that can be associated with a calendar cell. */
export type MatCalendarCellCssClasses = string | string[] | Set<string> | {[key: string]: any};
Expand Down Expand Up @@ -314,18 +314,20 @@ export class MatCalendarBody<D = any> implements OnChanges, OnDestroy, AfterView
_focusActiveCell(movePreview = true) {
afterNextRender(
() => {
setTimeout(() => {
const activeCell: HTMLElement | null = this._elementRef.nativeElement.querySelector(
'.mat-calendar-body-active',
);

if (activeCell) {
if (!movePreview) {
this._skipNextFocus = true;
this._ngZone.runOutsideAngular(() => {
setTimeout(() => {
const activeCell: HTMLElement | null = this._elementRef.nativeElement.querySelector(
'.mat-calendar-body-active',
);

if (activeCell) {
if (!movePreview) {
this._skipNextFocus = true;
}

activeCell.focus();
}

activeCell.focus();
}
});
});
},
{injector: this._injector},
Expand Down

0 comments on commit f37bad5

Please sign in to comment.