From abcad55157c25bcc115b1257b3dab35cae3b2313 Mon Sep 17 00:00:00 2001 From: Andrew Thomson Date: Tue, 7 Nov 2023 16:23:13 +0000 Subject: [PATCH] Modal.js should consider shadowRoot consider the possibility that document.activeElement could be a shadowRoot. In this case recursively look inside shadowRoots until the actual focused child is found. --- src/Modal.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Modal.js b/src/Modal.js index 51c16ce4c..29326bf26 100644 --- a/src/Modal.js +++ b/src/Modal.js @@ -306,12 +306,26 @@ class Modal extends React.Component { return this._element.querySelectorAll(focusableElements.join(', ')); } + getActiveElement(elem) { + const activeEl = elem.activeElement; + + if (!activeEl) { + return null; + } + + if (activeEl.shadowRoot) { + return this.getActiveElement(activeEl.shadowRoot); + } else { + return activeEl; + } + } + getFocusedChild() { let currentFocus; const focusableChildren = this.getFocusableChildren(); try { - currentFocus = document.activeElement; + currentFocus = this.getActiveElement(document); } catch (err) { currentFocus = focusableChildren[0]; }