diff --git a/client/src/ui.js b/client/src/ui.js index 8d4c5bf824..0bdd6aa8f7 100644 --- a/client/src/ui.js +++ b/client/src/ui.js @@ -625,6 +625,39 @@ function (/** marked~ */marked, /** DOMPurify~ */ DOMPurify) { $($modalContainer.get($modalContainer.length - 2)).removeClass('overlaid'); }; + /** + * @private + * @param {Element} element + * @return {Element|null} + */ + Dialog.prototype._findClosestFocusableElement = function (element) { + let isVisible = !!( + element.offsetWidth || + element.offsetHeight || + element.getClientRects().length + ); + + if (isVisible) { + element.focus({preventScroll: true}); + + return element; + } + + let $element = $(element); + + if ($element.closest('.dropdown-menu').length) { + let $button = $element.closest('.btn-group').find(`[data-toggle="dropdown"]`); + + if ($button.length) { + $button.get(0).focus({preventScroll: true}); + + return $button.get(0); + } + } + + return null; + }; + /** * Close. */ @@ -635,7 +668,11 @@ function (/** marked~ */marked, /** DOMPurify~ */ DOMPurify) { if (this.activeElement) { setTimeout(() => { - this.activeElement.focus({preventScroll: true}); + let element = this._findClosestFocusableElement(this.activeElement); + + if (element) { + element.focus({preventScroll: true}); + } }, 50); } }