@vaadin/vaadin-overlay
Advanced tools
Comparing version 3.2.3 to 3.2.4
@@ -14,3 +14,3 @@ { | ||
"name": "@vaadin/vaadin-overlay", | ||
"version": "3.2.3", | ||
"version": "3.2.4", | ||
"main": "vaadin-overlay.js", | ||
@@ -17,0 +17,0 @@ "author": "Vaadin Ltd", |
@@ -311,2 +311,10 @@ /** | ||
/** | ||
* Set to true to enable restoring of focus when overlay is closed. | ||
*/ | ||
restoreFocusOnClose: { | ||
type: Boolean, | ||
value: false, | ||
}, | ||
_mouseDownInside: { | ||
@@ -510,2 +518,4 @@ type: Boolean | ||
if (opened) { | ||
// Store focused node. | ||
this.__restoreFocusNode = this._getActiveElement(); | ||
this._animatedOpening(); | ||
@@ -533,3 +543,4 @@ | ||
const name = getComputedStyle(this).getPropertyValue('animation-name'); | ||
return name && name != 'none'; | ||
const hidden = getComputedStyle(this).getPropertyValue('display') === 'none'; | ||
return !hidden && name && name != 'none'; | ||
} | ||
@@ -591,2 +602,16 @@ | ||
this.removeAttribute('closing'); | ||
if (this.restoreFocusOnClose && this.__restoreFocusNode) { | ||
// If the activeElement is `<body>` or inside the overlay, | ||
// we are allowed to restore the focus. In all the other | ||
// cases focus might have been moved elsewhere by another | ||
// component or by the user interaction (e.g. click on a | ||
// button outside the overlay). | ||
const activeElement = this._getActiveElement(); | ||
if (activeElement === document.body || this._deepContains(activeElement)) { | ||
this.__restoreFocusNode.focus(); | ||
} | ||
this.__restoreFocusNode = null; | ||
} | ||
}; | ||
@@ -865,2 +890,31 @@ | ||
} | ||
_getActiveElement() { | ||
let active = document._activeElement || document.activeElement; | ||
// document.activeElement can be null | ||
// https://developer.mozilla.org/en-US/docs/Web/API/Document/activeElement | ||
// In IE 11, it can also be an object when operating in iframes | ||
// or document.documentElement (when overlay closed on outside click). | ||
// In these cases, default it to document.body. | ||
if (!active || active === document.documentElement || active instanceof Element === false) { | ||
active = document.body; | ||
} | ||
while (active.shadowRoot && active.shadowRoot.activeElement) { | ||
active = active.shadowRoot.activeElement; | ||
} | ||
return active; | ||
} | ||
_deepContains(node) { | ||
if (this.contains(node)) { | ||
return true; | ||
} | ||
let n = node; | ||
const doc = node.ownerDocument; | ||
// walk from node to `this` or `document` | ||
while (n && n !== doc && n !== this) { | ||
n = n.parentNode || n.host; | ||
} | ||
return n === this; | ||
} | ||
} | ||
@@ -867,0 +921,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49278
994