fix(overlays): only use elementToFocusAfterHide when provided as HTMLElement

This commit is contained in:
Thijs Louisse 2024-03-08 10:51:33 +01:00 committed by Thijs Louisse
parent 675f7b1cdd
commit f76f4b36b0
2 changed files with 18 additions and 9 deletions

View file

@ -0,0 +1,5 @@
---
'@lion/overlays': patch
---
fix: only use elementToFocusAfterHide when provided as HTMLElement

View file

@ -244,8 +244,9 @@ export class OverlayController extends EventTargetShim {
* @type {HTMLElement} * @type {HTMLElement}
*/ */
get contentWrapperNode() { get contentWrapperNode() {
return /** @type {HTMLElement} */ (this.__contentWrapperNode || return /** @type {HTMLElement} */ (
this.config?.contentWrapperNode); this.__contentWrapperNode || this.config?.contentWrapperNode
);
} }
/** /**
@ -262,8 +263,9 @@ export class OverlayController extends EventTargetShim {
* @type {HTMLElement} * @type {HTMLElement}
*/ */
get elementToFocusAfterHide() { get elementToFocusAfterHide() {
return /** @type {HTMLElement} */ (this.__elementToFocusAfterHide || return /** @type {HTMLElement} */ (
this.config?.elementToFocusAfterHide); this.__elementToFocusAfterHide || this.config?.elementToFocusAfterHide
);
} }
/** /**
@ -588,8 +590,9 @@ export class OverlayController extends EventTargetShim {
if (this.__isContentNodeProjected && this.contentWrapperNode.isConnected) { if (this.__isContentNodeProjected && this.contentWrapperNode.isConnected) {
// We need to keep track of the original local context. // We need to keep track of the original local context.
/** config [l2], [l4] */ /** config [l2], [l4] */
this.__originalContentParent = /** @type {HTMLElement} */ (this.contentWrapperNode this.__originalContentParent = /** @type {HTMLElement} */ (
.parentNode); this.contentWrapperNode.parentNode
);
} else if (cfgToAdd.contentNode && cfgToAdd.contentNode.isConnected) { } else if (cfgToAdd.contentNode && cfgToAdd.contentNode.isConnected) {
// We need to keep track of the original local context. // We need to keep track of the original local context.
/** config [l1], [l3], [g1] */ /** config [l1], [l3], [g1] */
@ -905,15 +908,16 @@ export class OverlayController extends EventTargetShim {
/** @protected */ /** @protected */
_restoreFocus() { _restoreFocus() {
const { activeElement } = /** @type {* & ShadowRoot} */ (this const { activeElement } = /** @type {* & ShadowRoot} */ (
.__contentWrapperNode).getRootNode(); this.__contentWrapperNode
).getRootNode();
// We only are allowed to move focus if we (still) 'own' it. // We only are allowed to move focus if we (still) 'own' it.
// Otherwise we assume the 'outside world' has, purposefully, taken over // Otherwise we assume the 'outside world' has, purposefully, taken over
if ( if (
activeElement && activeElement &&
/** @type {HTMLElement} */ (this.__contentWrapperNode).contains(activeElement) /** @type {HTMLElement} */ (this.__contentWrapperNode).contains(activeElement)
) { ) {
if (this.elementToFocusAfterHide) { if (this.elementToFocusAfterHide instanceof HTMLElement) {
this.elementToFocusAfterHide.focus(); this.elementToFocusAfterHide.focus();
} else { } else {
activeElement.blur(); activeElement.blur();