Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/app/javascript/controllers/ruby_ui/command_controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Controller } from "@hotwired/stimulus";
import Fuse from "fuse.js";

const OPEN_DIALOG_SELECTOR = "[data-ruby-ui--command-dialog]";

// Connects to data-controller="ruby-ui--command"
export default class extends Controller {
static targets = ["input", "group", "item", "empty", "content"];
Expand Down Expand Up @@ -37,6 +39,12 @@ export default class extends Controller {
return;
}

const openDialog = document.querySelector(OPEN_DIALOG_SELECTOR);
if (openDialog) {
this.focusDialogInput(openDialog);
return;
}

document.body.insertAdjacentHTML("beforeend", this.contentTarget.innerHTML);
// prevent scroll on body
document.body.classList.add("overflow-hidden");
Expand Down Expand Up @@ -144,4 +152,9 @@ export default class extends Controller {
this.itemTargets.forEach((item) => this.toggleAriaSelected(item, false));
this.selectedIndex = -1;
}

focusDialogInput(dialog) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it re-implements Stimulus target lookup by hand and hard-codes the controller identifier inside a selector string. If the controller is ever renamed, the input target moves, or the input gets wrapped in another element, this silently stops working, and the controller is now coupled to another instance's internal markup.

Using querySelector is a code smell in hotwire

Copy link
Copy Markdown
Contributor Author

@djalmaaraujo djalmaaraujo May 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid point — querySelector + hardcoded selector + manual target lookup couples this controller to another instance's markup.

Refactored to use Stimulus outlets (af84363): split into a new ruby-ui--command-dialog controller that owns the trigger + content target and declares a ruby-ui--command outlet. Outlet connected/disconnected callbacks track the active cloned dialog, and open() calls focusInput() on the outlet instead of querying the DOM. Inner ruby-ui--command now only handles input/items/filter/keydown/dismiss, with no hasContentTarget dual-personality. Marker attribute is now declarative outlet config, not a runtime querySelector.

const input = dialog.querySelector("[data-ruby-ui--command-target='input']");
input?.focus();
}
}
6 changes: 6 additions & 0 deletions docs/app/views/docs/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ def view_template
RUBY
end

Heading(level: 2) { "Single instance" }

p(class: "text-muted-foreground") do
plain "The Command dialog is single-instance. Activating a trigger while the dialog is already open refocuses the existing dialog instead of stacking another one on top, so repeated keybindings or trigger clicks behave predictably."
end

render Components::ComponentSetup::Tabs.new(component_name: component)

render Docs::ComponentsTable.new(component_files(component))
Expand Down
13 changes: 13 additions & 0 deletions gem/lib/ruby_ui/command/command_controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Controller } from "@hotwired/stimulus";
import Fuse from "fuse.js";

const OPEN_DIALOG_SELECTOR = "[data-ruby-ui--command-dialog]";

// Connects to data-controller="ruby-ui--command"
export default class extends Controller {
static targets = ["input", "group", "item", "empty", "content"];
Expand Down Expand Up @@ -37,6 +39,12 @@ export default class extends Controller {
return;
}

const openDialog = document.querySelector(OPEN_DIALOG_SELECTOR);
if (openDialog) {
this.focusDialogInput(openDialog);
return;
}

document.body.insertAdjacentHTML("beforeend", this.contentTarget.innerHTML);
// prevent scroll on body
document.body.classList.add("overflow-hidden");
Expand Down Expand Up @@ -144,4 +152,9 @@ export default class extends Controller {
this.itemTargets.forEach((item) => this.toggleAriaSelected(item, false));
this.selectedIndex = -1;
}

focusDialogInput(dialog) {
const input = dialog.querySelector("[data-ruby-ui--command-target='input']");
input?.focus();
}
}
2 changes: 1 addition & 1 deletion gem/lib/ruby_ui/command/command_dialog_content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize(size: :md, **attrs)

def view_template(&block)
template(data: {ruby_ui__command_target: "content"}) do
div(data: {controller: "ruby-ui--command"}) do
div(data: {controller: "ruby-ui--command", ruby_ui__command_dialog: true}) do
backdrop
div(**attrs, &block)
end
Expand Down
1 change: 1 addition & 0 deletions gem/test/ruby_ui/command_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@ def test_render_with_all_items
end

assert_match(/Search/, output)
assert_match(/data-ruby-ui--command-dialog/, output)
end
end