Fix autofocus sometimes not working on the search page (#1597)

This commit is contained in:
toasted-nutbread 2021-04-07 19:07:42 -04:00 committed by GitHub
parent e14b52ef84
commit e444141511
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 2 deletions

View File

@ -26,7 +26,7 @@
(async () => { (async () => {
try { try {
const documentFocusController = new DocumentFocusController(); const documentFocusController = new DocumentFocusController('#search-textbox');
documentFocusController.prepare(); documentFocusController.prepare();
await yomichan.prepare(); await yomichan.prepare();

View File

@ -22,13 +22,17 @@
* focus a dummy element inside the main content, which gives keyboard scroll focus to that element. * focus a dummy element inside the main content, which gives keyboard scroll focus to that element.
*/ */
class DocumentFocusController { class DocumentFocusController {
constructor() { constructor(autofocusElementSelector=null) {
this._autofocusElement = (autofocusElementSelector !== null ? document.querySelector(autofocusElementSelector) : null);
this._contentScrollFocusElement = document.querySelector('#content-scroll-focus'); this._contentScrollFocusElement = document.querySelector('#content-scroll-focus');
} }
prepare() { prepare() {
window.addEventListener('focus', this._onWindowFocus.bind(this), false); window.addEventListener('focus', this._onWindowFocus.bind(this), false);
this._updateFocusedElement(false); this._updateFocusedElement(false);
if (this._autofocusElement !== null && document.activeElement !== this._autofocusElement) {
this._autofocusElement.focus({preventScroll: true});
}
} }
blurElement(element) { blurElement(element) {