Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
19 changes: 19 additions & 0 deletions docs/app/javascript/controllers/ruby_ui/command_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ export default class extends Controller {
},
};

static openInstance = null;

connect() {
this.selectedIndex = -1;

if (!this.hasInputTarget) {
return;
}

this.constructor.openInstance = this;
this.inputTarget.focus();
this.searchIndex = this.buildSearchIndex();
this.toggleVisibility(this.emptyTargets, false);
Expand All @@ -28,6 +31,12 @@ export default class extends Controller {
}
}

disconnect() {
if (this.constructor.openInstance === this) {
this.constructor.openInstance = null;
}
}

open(e) {
if (e) {
e.preventDefault();
Expand All @@ -37,6 +46,12 @@ export default class extends Controller {
return;
}

const openInstance = this.constructor.openInstance;
if (openInstance) {
openInstance.focusInput();
return;
}

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

focusInput() {
this.inputTarget?.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
19 changes: 19 additions & 0 deletions gem/lib/ruby_ui/command/command_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ export default class extends Controller {
},
};

static openInstance = null;

connect() {
this.selectedIndex = -1;

if (!this.hasInputTarget) {
return;
}

this.constructor.openInstance = this;
this.inputTarget.focus();
this.searchIndex = this.buildSearchIndex();
this.toggleVisibility(this.emptyTargets, false);
Expand All @@ -28,6 +31,12 @@ export default class extends Controller {
}
}

disconnect() {
if (this.constructor.openInstance === this) {
this.constructor.openInstance = null;
}
}

open(e) {
if (e) {
e.preventDefault();
Expand All @@ -37,6 +46,12 @@ export default class extends Controller {
return;
}

const openInstance = this.constructor.openInstance;
if (openInstance) {
openInstance.focusInput();
return;
}

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

focusInput() {
this.inputTarget?.focus();
}
}
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-controller="ruby-ui--command"/, output)
end
end