FIX: attempts to make autofocus more resilient in modals (#10680)

The current situation could cause a transition on the button to end after/during modal has shown and causing the button to get focus again. Browsers would then refuse to switch focus.

This is a kinda convulted solution, but it's a general purpose solution which doesn't involve changing anything in plugins/themes or core templates.
This commit is contained in:
Joffrey JAFFEUX 2020-09-16 11:05:02 +02:00 committed by GitHub
parent 32d6286bea
commit 4af48f7998
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 2 deletions

View File

@ -1,3 +1,4 @@
import afterTransition from "discourse/lib/after-transition";
import I18n from "I18n"; import I18n from "I18n";
import { next } from "@ember/runloop"; import { next } from "@ember/runloop";
import { on } from "discourse-common/utils/decorators"; import { on } from "discourse-common/utils/decorators";
@ -116,5 +117,15 @@ export default Component.extend({
} else { } else {
this.set("dismissable", true); this.set("dismissable", true);
} }
if (this.element) {
const autofocusInputs = this.element.querySelectorAll(
".modal-body input[autofocus]"
);
if (autofocusInputs.length) {
afterTransition(() => autofocusInputs[0].focus());
}
}
}, },
}); });

View File

@ -26,8 +26,6 @@ export default Controller.extend(ModalFunctionality, {
element element
.closest(".modal-inner-container") .closest(".modal-inner-container")
.addEventListener("mousedown", this.mouseDown); .addEventListener("mousedown", this.mouseDown);
document.activeElement.blur();
}); });
}, },