@energycap/components 0.46.20 → 0.46.21

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.
@@ -4157,6 +4157,7 @@ class PopupContainerDirective {
4157
4157
  });
4158
4158
  }
4159
4159
  if (!this.popperRef) {
4160
+ this.previouslyFocusedElement = this.document.activeElement;
4160
4161
  // Add the popper template as an embedded view since PopperJS
4161
4162
  // manipulates DOM elements.
4162
4163
  this.popupViewRef = this.viewContainer.createEmbeddedView(this.popup);
@@ -4180,11 +4181,30 @@ class PopupContainerDirective {
4180
4181
  this.globalCloseSubscription = undefined;
4181
4182
  }
4182
4183
  if (this.popperRef && this.popupViewRef) {
4184
+ // Determine this before destroying the view: once destroyed, the popup's
4185
+ // DOM nodes are gone and `document.activeElement` will already have fallen
4186
+ // back to <body>, so containment can no longer be checked afterward.
4187
+ const shouldRestoreFocus = this.isFocusInsidePopup() || this.document.activeElement === this.document.body;
4183
4188
  this.popupViewRef.destroy();
4184
4189
  this.popperRef.destroy();
4185
4190
  this.popperRef = undefined;
4186
4191
  this.popperStatusChange.emit('hidden');
4192
+ if (shouldRestoreFocus && this.previouslyFocusedElement?.focus) {
4193
+ this.previouslyFocusedElement.focus();
4194
+ }
4195
+ }
4196
+ }
4197
+ /**
4198
+ * Whether the current `document.activeElement` is contained within the popup's DOM.
4199
+ *
4200
+ * @memberof PopupContainerDirective
4201
+ */
4202
+ isFocusInsidePopup() {
4203
+ if (!this.popupViewRef) {
4204
+ return false;
4187
4205
  }
4206
+ const activeElement = this.document.activeElement;
4207
+ return this.popupViewRef.rootNodes.some(node => node.contains && node.contains(activeElement));
4188
4208
  }
4189
4209
  /**
4190
4210
  * Updates the popup container position