Skip to content

A platform for community discussion. Free, open, simple.

FIX: modals should not leak keypresses outside of the modal (#36268)

Sam1d2ae5159b5a
diff --git a/frontend/discourse/app/components/d-modal.gjs b/frontend/discourse/app/components/d-modal.gjs
index 82e9bcb61c1..c049d8e2731 100644
--- a/frontend/discourse/app/components/d-modal.gjs
+++ b/frontend/discourse/app/components/d-modal.gjs
@@ -54,7 +54,8 @@ export default class DModal extends Component {
async setupModal(el) {
document.documentElement.addEventListener(
"keydown",
- this.handleDocumentKeydown
+ this.handleDocumentKeydown,
+ { capture: true }
);
this.wrapperElement = el;
@@ -71,7 +72,8 @@ export default class DModal extends Component {
cleanupModal() {
document.documentElement.removeEventListener(
"keydown",
- this.handleDocumentKeydown
+ this.handleDocumentKeydown,
+ { capture: true }
);
}
@@ -101,6 +103,7 @@ export default class DModal extends Component {
// skip when in a form or a textarea element
if (
event.target.closest("form") ||
+ document.activeElement?.closest("form") ||
document.activeElement?.nodeName === "TEXTAREA"
) {
return false;
@@ -190,6 +193,12 @@ export default class DModal extends Component {
return;
}
+ // Prevent keyboard events from leaking to elements behind the modal
+ if (!this.wrapperElement.contains(document.activeElement)) {
+ event.stopPropagation();
+ event.preventDefault();
+ }
+
if (event.key === "Escape" && this.dismissable) {
event.stopPropagation();
this.closeModal(CLOSE_INITIATED_BY_ESC);
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/modal/diff-modal.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/modal/diff-modal.gjs
index 8be86547c07..9fb7ad86b62 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/modal/diff-modal.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/modal/diff-modal.gjs
@@ -1,6 +1,7 @@
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { action } from "@ember/object";
+import didUpdate from "@ember/render-modifiers/modifiers/did-update";
import willDestroy from "@ember/render-modifiers/modifiers/will-destroy";
import { service } from "@ember/service";
import { htmlSafe } from "@ember/template";
@@ -187,6 +188,13 @@ export default class ModalDiffModal extends Component {
this.args.closeModal();
}
+ @action
+ focusConfirmBtn(element) {
+ if (!this.primaryBtnDisabled) {
+ element.focus();
+ }
+ }
+
#resetState() {
this.suggestion = "";
this.finalResult = "";
@@ -242,6 +250,7 @@ export default class ModalDiffModal extends Component {
@disabled={{this.primaryBtnDisabled}}
@action={{this.triggerConfirmChanges}}
@translatedLabel={{this.primaryBtnLabel}}
+ {{didUpdate this.focusConfirmBtn this.isStreaming}}
>
{{#if this.loading}}
<AiIndicatorWave @loading={{this.loading}} />
diff --git a/plugins/discourse-ai/spec/system/ai_helper/ai_proofreading_spec.rb b/plugins/discourse-ai/spec/system/ai_helper/ai_proofreading_spec.rb
index 911d2ff8a91..b6cd21174c7 100644
--- a/plugins/discourse-ai/spec/system/ai_helper/ai_proofreading_spec.rb
+++ b/plugins/discourse-ai/spec/system/ai_helper/ai_proofreading_spec.rb
@@ -60,6 +60,30 @@ RSpec.describe "AI Composer Proofreading Features", type: :system do
end
end
+ it "focuses the confirm button when streaming completes" do
+ visit "/new-topic"
+ composer.fill_content("hello worrld")
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(["hello world"]) do
+ composer.composer_input.send_keys(keyboard_shortcut)
+ expect(diff_modal).to have_diff("worrld", "world")
+ expect(diff_modal).to have_confirm_button_focused
+ end
+ end
+
+ it "confirms changes when pressing Enter" do
+ visit "/new-topic"
+ composer.fill_content("hello worrld")
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(["hello world"]) do
+ composer.composer_input.send_keys(keyboard_shortcut)
+ expect(diff_modal).to have_diff("worrld", "world")
+ expect(diff_modal).to have_confirm_button_focused
+ page.send_keys(:enter)
+ expect(composer).to have_value("hello world")
+ end
+ end
+
context "when using rich text editor" do
before { SiteSetting.rich_editor = true }
diff --git a/plugins/discourse-ai/spec/system/page_objects/modals/diff_modal.rb b/plugins/discourse-ai/spec/system/page_objects/modals/diff_modal.rb
index 4c6a5a27e7b..42a15188ddb 100644
--- a/plugins/discourse-ai/spec/system/page_objects/modals/diff_modal.rb
+++ b/plugins/discourse-ai/spec/system/page_objects/modals/diff_modal.rb
@@ -27,6 +27,10 @@ module PageObjects
has_css?(".inline-diff ins", text: new_value) &&
has_css?(".inline-diff del", text: old_value)
end
+
+ def has_confirm_button_focused?
+ page.has_css?(".d-modal__footer button.confirm:focus", wait: 5)
+ end
end
end
end