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
3 changes: 3 additions & 0 deletions docs/app/javascript/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ application.register("ruby-ui--combobox", RubyUi__ComboboxController)
import RubyUi__CommandController from "./ruby_ui/command_controller"
application.register("ruby-ui--command", RubyUi__CommandController)

import RubyUi__CommandDialogController from "./ruby_ui/command_dialog_controller"
application.register("ruby-ui--command-dialog", RubyUi__CommandDialogController)

import RubyUi__ContextMenuController from "./ruby_ui/context_menu_controller"
application.register("ruby-ui--context-menu", RubyUi__ContextMenuController)

Expand Down
31 changes: 5 additions & 26 deletions docs/app/javascript/controllers/ruby_ui/command_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@ import Fuse from "fuse.js";

// Connects to data-controller="ruby-ui--command"
export default class extends Controller {
static targets = ["input", "group", "item", "empty", "content"];

static values = {
open: {
type: Boolean,
default: false,
},
};
static targets = ["input", "group", "item", "empty"];

connect() {
this.selectedIndex = -1;
Expand All @@ -22,24 +15,6 @@ export default class extends Controller {
this.inputTarget.focus();
this.searchIndex = this.buildSearchIndex();
this.toggleVisibility(this.emptyTargets, false);

if (this.openValue && this.hasContentTarget) {
this.open();
}
}

open(e) {
if (e) {
e.preventDefault();
}

if (!this.hasContentTarget) {
return;
}

document.body.insertAdjacentHTML("beforeend", this.contentTarget.innerHTML);
// prevent scroll on body
document.body.classList.add("overflow-hidden");
}

dismiss() {
Expand All @@ -49,6 +24,10 @@ export default class extends Controller {
this.element.remove();
}

focusInput() {
this.inputTarget?.focus();
}

filter(e) {
// Deselect any previously selected item
this.deselectAll();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Controller } from "@hotwired/stimulus";

// Connects to data-controller="ruby-ui--command-dialog"
export default class extends Controller {
static targets = ["content"];
static outlets = ["ruby-ui--command"];

rubyUiCommandOutletConnected(controller) {
this.openOutlet = controller;
}

rubyUiCommandOutletDisconnected() {
this.openOutlet = null;
}

open(e) {
if (e) {
e.preventDefault();
}

if (!this.hasContentTarget) {
return;
}

if (this.openOutlet) {
this.openOutlet.focusInput();
return;
}

document.body.insertAdjacentHTML("beforeend", this.contentTarget.innerHTML);
// prevent scroll on body
document.body.classList.add("overflow-hidden");
}
}
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
31 changes: 5 additions & 26 deletions gem/lib/ruby_ui/command/command_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@ import Fuse from "fuse.js";

// Connects to data-controller="ruby-ui--command"
export default class extends Controller {
static targets = ["input", "group", "item", "empty", "content"];

static values = {
open: {
type: Boolean,
default: false,
},
};
static targets = ["input", "group", "item", "empty"];

connect() {
this.selectedIndex = -1;
Expand All @@ -22,24 +15,6 @@ export default class extends Controller {
this.inputTarget.focus();
this.searchIndex = this.buildSearchIndex();
this.toggleVisibility(this.emptyTargets, false);

if (this.openValue && this.hasContentTarget) {
this.open();
}
}

open(e) {
if (e) {
e.preventDefault();
}

if (!this.hasContentTarget) {
return;
}

document.body.insertAdjacentHTML("beforeend", this.contentTarget.innerHTML);
// prevent scroll on body
document.body.classList.add("overflow-hidden");
}

dismiss() {
Expand All @@ -49,6 +24,10 @@ export default class extends Controller {
this.element.remove();
}

focusInput() {
this.inputTarget?.focus();
}

filter(e) {
// Deselect any previously selected item
this.deselectAll();
Expand Down
5 changes: 4 additions & 1 deletion gem/lib/ruby_ui/command/command_dialog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ def view_template(&)

def default_attrs
{
data: {controller: "ruby-ui--command"}
data: {
controller: "ruby-ui--command-dialog",
ruby_ui__command_dialog_ruby_ui__command_outlet: "[data-ruby-ui--command-dialog-instance]"
}
}
end
end
Expand Down
4 changes: 2 additions & 2 deletions gem/lib/ruby_ui/command/command_dialog_content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def initialize(size: :md, **attrs)
end

def view_template(&block)
template(data: {ruby_ui__command_target: "content"}) do
div(data: {controller: "ruby-ui--command"}) do
template(data: {ruby_ui__command_dialog_target: "content"}) do
div(data: {controller: "ruby-ui--command", ruby_ui__command_dialog_instance: true}) do
backdrop
div(**attrs, &block)
end
Expand Down
34 changes: 34 additions & 0 deletions gem/lib/ruby_ui/command/command_dialog_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Controller } from "@hotwired/stimulus";

// Connects to data-controller="ruby-ui--command-dialog"
export default class extends Controller {
static targets = ["content"];
static outlets = ["ruby-ui--command"];

rubyUiCommandOutletConnected(controller) {
this.openOutlet = controller;
}

rubyUiCommandOutletDisconnected() {
this.openOutlet = null;
}

open(e) {
if (e) {
e.preventDefault();
}

if (!this.hasContentTarget) {
return;
}

if (this.openOutlet) {
this.openOutlet.focusInput();
return;
}

document.body.insertAdjacentHTML("beforeend", this.contentTarget.innerHTML);
// prevent scroll on body
document.body.classList.add("overflow-hidden");
}
}
4 changes: 2 additions & 2 deletions gem/lib/ruby_ui/command/command_dialog_trigger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CommandDialogTrigger < Base
].freeze

def initialize(keybindings: DEFAULT_KEYBINDINGS, **attrs)
@keybindings = keybindings.map { |kb| "#{kb}->ruby-ui--command#open" }
@keybindings = keybindings.map { |kb| "#{kb}->ruby-ui--command-dialog#open" }
super(**attrs)
end

Expand All @@ -21,7 +21,7 @@ def view_template(&)
def default_attrs
{
data: {
action: ["click->ruby-ui--command#open", @keybindings.join(" ")]
action: ["click->ruby-ui--command-dialog#open", @keybindings.join(" ")]
}
}
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-controller="ruby-ui--command"/, output)
end
end