@angular/cdk 14.1.0 → 14.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -909,6 +909,24 @@ class OverlayRef {
909
909
  this._locationChanges = this._location.subscribe(() => this.dispose());
910
910
  }
911
911
  this._outsideClickDispatcher.add(this);
912
+ // TODO(crisbeto): the null check is here, because the portal outlet returns `any`.
913
+ // We should be guaranteed for the result to be `ComponentRef | EmbeddedViewRef`, but
914
+ // `instanceof EmbeddedViewRef` doesn't appear to work at the moment.
915
+ if (typeof attachResult?.onDestroy === 'function') {
916
+ // In most cases we control the portal and we know when it is being detached so that
917
+ // we can finish the disposal process. The exception is if the user passes in a custom
918
+ // `ViewContainerRef` that isn't destroyed through the overlay API. Note that we use
919
+ // `detach` here instead of `dispose`, because we don't know if the user intends to
920
+ // reattach the overlay at a later point. It also has the advantage of waiting for animations.
921
+ attachResult.onDestroy(() => {
922
+ if (this.hasAttached()) {
923
+ // We have to delay the `detach` call, because detaching immediately prevents
924
+ // other destroy hooks from running. This is likely a framework bug similar to
925
+ // https://github.com/angular/angular/issues/46119
926
+ this._ngZone.runOutsideAngular(() => Promise.resolve().then(() => this.detach()));
927
+ }
928
+ });
929
+ }
912
930
  return attachResult;
913
931
  }
914
932
  /**