@angular/material 14.2.1 → 14.2.2

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.
@@ -5,9 +5,9 @@ import { Overlay, FlexibleConnectedPositionStrategy, OverlayConfig, OverlayModul
5
5
  import * as i6 from '@angular/cdk/portal';
6
6
  import { ComponentPortal, TemplatePortal, PortalModule } from '@angular/cdk/portal';
7
7
  import * as i1 from '@angular/common';
8
- import { CommonModule } from '@angular/common';
8
+ import { DOCUMENT, CommonModule } from '@angular/common';
9
9
  import * as i0 from '@angular/core';
10
- import { EventEmitter, Component, ViewEncapsulation, ChangeDetectionStrategy, Input, Output, Injectable, Optional, SkipSelf, InjectionToken, Inject, ViewChild, forwardRef, Directive, Attribute, ContentChild, inject, InjectFlags, Self, TemplateRef, NgModule } from '@angular/core';
10
+ import { EventEmitter, Component, ViewEncapsulation, ChangeDetectionStrategy, Input, Output, Injectable, Optional, SkipSelf, InjectionToken, Inject, ViewChild, forwardRef, inject, Directive, Attribute, ContentChild, InjectFlags, Self, TemplateRef, NgModule } from '@angular/core';
11
11
  import * as i3 from '@angular/material/button';
12
12
  import { MatButtonModule } from '@angular/material/button';
13
13
  import { CdkScrollableModule } from '@angular/cdk/scrolling';
@@ -2241,6 +2241,7 @@ class MatDatepickerBase {
2241
2241
  this._dir = _dir;
2242
2242
  this._model = _model;
2243
2243
  this._inputStateChanges = Subscription.EMPTY;
2244
+ this._document = inject(DOCUMENT);
2244
2245
  /** The view that the calendar should start in. */
2245
2246
  this.startView = 'month';
2246
2247
  this._touchUi = false;
@@ -2449,29 +2450,40 @@ class MatDatepickerBase {
2449
2450
  if (!this._opened) {
2450
2451
  return;
2451
2452
  }
2452
- if (this._componentRef) {
2453
- const instance = this._componentRef.instance;
2454
- instance._startExitAnimation();
2455
- instance._animationDone.pipe(take(1)).subscribe(() => this._destroyOverlay());
2456
- }
2453
+ const canRestoreFocus = this._restoreFocus &&
2454
+ this._focusedElementBeforeOpen &&
2455
+ typeof this._focusedElementBeforeOpen.focus === 'function';
2457
2456
  const completeClose = () => {
2458
2457
  // The `_opened` could've been reset already if
2459
2458
  // we got two events in quick succession.
2460
2459
  if (this._opened) {
2461
2460
  this._opened = false;
2462
2461
  this.closedStream.emit();
2463
- this._focusedElementBeforeOpen = null;
2464
2462
  }
2465
2463
  };
2466
- if (this._restoreFocus &&
2467
- this._focusedElementBeforeOpen &&
2468
- typeof this._focusedElementBeforeOpen.focus === 'function') {
2464
+ if (this._componentRef) {
2465
+ const { instance, location } = this._componentRef;
2466
+ instance._startExitAnimation();
2467
+ instance._animationDone.pipe(take(1)).subscribe(() => {
2468
+ const activeElement = this._document.activeElement;
2469
+ // Since we restore focus after the exit animation, we have to check that
2470
+ // the user didn't move focus themselves inside the `close` handler.
2471
+ if (canRestoreFocus &&
2472
+ (!activeElement ||
2473
+ activeElement === this._document.activeElement ||
2474
+ location.nativeElement.contains(activeElement))) {
2475
+ this._focusedElementBeforeOpen.focus();
2476
+ }
2477
+ this._focusedElementBeforeOpen = null;
2478
+ this._destroyOverlay();
2479
+ });
2480
+ }
2481
+ if (canRestoreFocus) {
2469
2482
  // Because IE moves focus asynchronously, we can't count on it being restored before we've
2470
2483
  // marked the datepicker as closed. If the event fires out of sequence and the element that
2471
2484
  // we're refocusing opens the datepicker on focus, the user could be stuck with not being
2472
2485
  // able to close the calendar at all. We work around it by making the logic, that marks
2473
2486
  // the datepicker as closed, async as well.
2474
- this._focusedElementBeforeOpen.focus();
2475
2487
  setTimeout(completeClose);
2476
2488
  }
2477
2489
  else {