Skip to content

Commit 97af219

Browse files
committed
[Bug Fix] Prevent Command dialog stacking (#230)
1 parent c13b7e1 commit 97af219

4 files changed

Lines changed: 28 additions & 1 deletion

File tree

docs/app/javascript/controllers/ruby_ui/command_controller.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Controller } from "@hotwired/stimulus";
22
import Fuse from "fuse.js";
33

4+
const OPEN_DIALOG_SELECTOR = "[data-ruby-ui--command-dialog]";
5+
46
// Connects to data-controller="ruby-ui--command"
57
export default class extends Controller {
68
static targets = ["input", "group", "item", "empty", "content"];
@@ -37,6 +39,12 @@ export default class extends Controller {
3739
return;
3840
}
3941

42+
const openDialog = document.querySelector(OPEN_DIALOG_SELECTOR);
43+
if (openDialog) {
44+
this.focusDialogInput(openDialog);
45+
return;
46+
}
47+
4048
document.body.insertAdjacentHTML("beforeend", this.contentTarget.innerHTML);
4149
// prevent scroll on body
4250
document.body.classList.add("overflow-hidden");
@@ -144,4 +152,9 @@ export default class extends Controller {
144152
this.itemTargets.forEach((item) => this.toggleAriaSelected(item, false));
145153
this.selectedIndex = -1;
146154
}
155+
156+
focusDialogInput(dialog) {
157+
const input = dialog.querySelector("[data-ruby-ui--command-target='input']");
158+
input?.focus();
159+
}
147160
}

gem/lib/ruby_ui/command/command_controller.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Controller } from "@hotwired/stimulus";
22
import Fuse from "fuse.js";
33

4+
const OPEN_DIALOG_SELECTOR = "[data-ruby-ui--command-dialog]";
5+
46
// Connects to data-controller="ruby-ui--command"
57
export default class extends Controller {
68
static targets = ["input", "group", "item", "empty", "content"];
@@ -37,6 +39,12 @@ export default class extends Controller {
3739
return;
3840
}
3941

42+
const openDialog = document.querySelector(OPEN_DIALOG_SELECTOR);
43+
if (openDialog) {
44+
this.focusDialogInput(openDialog);
45+
return;
46+
}
47+
4048
document.body.insertAdjacentHTML("beforeend", this.contentTarget.innerHTML);
4149
// prevent scroll on body
4250
document.body.classList.add("overflow-hidden");
@@ -144,4 +152,9 @@ export default class extends Controller {
144152
this.itemTargets.forEach((item) => this.toggleAriaSelected(item, false));
145153
this.selectedIndex = -1;
146154
}
155+
156+
focusDialogInput(dialog) {
157+
const input = dialog.querySelector("[data-ruby-ui--command-target='input']");
158+
input?.focus();
159+
}
147160
}

gem/lib/ruby_ui/command/command_dialog_content.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def initialize(size: :md, **attrs)
1818

1919
def view_template(&block)
2020
template(data: {ruby_ui__command_target: "content"}) do
21-
div(data: {controller: "ruby-ui--command"}) do
21+
div(data: {controller: "ruby-ui--command", ruby_ui__command_dialog: true}) do
2222
backdrop
2323
div(**attrs, &block)
2424
end

gem/test/ruby_ui/command_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,6 @@ def test_render_with_all_items
6060
end
6161

6262
assert_match(/Search/, output)
63+
assert_match(/data-ruby-ui--command-dialog/, output)
6364
end
6465
end

0 commit comments

Comments
 (0)