From 5c5073a80f028811aaa9e27d4dc9d9ce3f2cb6fd Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Mon, 17 Mar 2025 14:22:15 +0100 Subject: [PATCH] fix(material/timepicker): assertion error if reopened quickly Fixes that the timepicker was throwing an error if it gets closed and reopened before change detection has had a chance to run. Fixes #30637. --- src/material/timepicker/timepicker.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/material/timepicker/timepicker.ts b/src/material/timepicker/timepicker.ts index 77d7c92aaf63..7da38240c4f6 100644 --- a/src/material/timepicker/timepicker.ts +++ b/src/material/timepicker/timepicker.ts @@ -232,7 +232,13 @@ export class MatTimepicker implements OnDestroy, MatOptionParentComponent { const overlayRef = this._getOverlayRef(); overlayRef.updateSize({width: input.getOverlayOrigin().nativeElement.offsetWidth}); this._portal ??= new TemplatePortal(this._panelTemplate(), this._viewContainerRef); - overlayRef.attach(this._portal); + + // We need to check this in case `isOpen` was flipped, but change detection hasn't + // had a chance to run yet. See https://github.com/angular/components/issues/30637 + if (!overlayRef.hasAttached()) { + overlayRef.attach(this._portal); + } + this._onOpenRender?.destroy(); this._onOpenRender = afterNextRender( () => {