MCPcopy Create free account
hub / github.com/angular/components / close

Function close

src/material/datepicker/datepicker-base.ts:662–715  ·  view source on GitHub ↗

Close the calendar.

()

Source from the content-addressed store, hash-verified

660
661 /** Close the calendar. */
662 close(): void {
663 // Skip reopening if there's an in-progress animation to avoid overlapping
664 // sequences which can cause "changed after checked" errors. See #25837.
665 if (!this._opened || this._componentRef?.instance._isAnimating) {
666 return;
667 }
668
669 const canRestoreFocus =
670 this.restoreFocus &&
671 this._focusedElementBeforeOpen &&
672 typeof this._focusedElementBeforeOpen.focus === 'function';
673
674 const completeClose = () => {
675 // The `_opened` could've been reset already if
676 // we got two events in quick succession.
677 if (this._opened) {
678 this._opened = false;
679 this.closedStream.emit();
680 }
681 };
682
683 if (this._componentRef) {
684 const {instance, location} = this._componentRef;
685 instance._animationDone.pipe(take(1)).subscribe(() => {
686 const activeElement = this._document.activeElement;
687
688 // Since we restore focus after the exit animation, we have to check that
689 // the user didn't move focus themselves inside the `close` handler.
690 if (
691 canRestoreFocus &&
692 (!activeElement ||
693 activeElement === this._document.activeElement ||
694 location.nativeElement.contains(activeElement))
695 ) {
696 this._focusedElementBeforeOpen!.focus();
697 }
698
699 this._focusedElementBeforeOpen = null;
700 this._destroyOverlay();
701 });
702 instance._startExitAnimation();
703 }
704
705 if (canRestoreFocus) {
706 // Because IE moves focus asynchronously, we can't count on it being restored before we've
707 // marked the datepicker as closed. If the event fires out of sequence and the element that
708 // we're refocusing opens the datepicker on focus, the user could be stuck with not being
709 // able to close the calendar at all. We work around it by making the logic, that marks
710 // the datepicker as closed, async as well.
711 setTimeout(completeClose);
712 } else {
713 completeClose();
714 }
715 }
716
717 /** Applies the current pending selection on the overlay to the model. */
718 _applyPendingSelection() {

Callers

nothing calls this directly

Calls 3

completeCloseFunction · 0.85
focusMethod · 0.65
_startExitAnimationMethod · 0.45

Tested by

no test coverage detected