Skip to content

Commit f07ef72

Browse files
committed
Some improvements
1 parent 892dc4f commit f07ef72

File tree

7 files changed

+37
-10
lines changed

7 files changed

+37
-10
lines changed

lib/ruby_ui/combobox/combobox_checkbox.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ def view_template
1010

1111
def default_attrs
1212
{
13-
class: "peer h-4 w-4 shrink-0 rounded-sm border border-primary ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 accent-primary",
13+
class: [
14+
"peer h-4 w-4 shrink-0 rounded-sm border border-primary ring-offset-background accent-primary",
15+
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
16+
"disabled:cursor-not-allowed disabled:opacity-50"
17+
],
1418
data: {
1519
ruby_ui__combobox_target: "input",
1620
action: "ruby-ui--combobox#inputChanged"

lib/ruby_ui/combobox/combobox_controller.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Controller } from "@hotwired/stimulus";
2-
import { computePosition, autoUpdate, offset } from "@floating-ui/dom";
2+
import { computePosition, autoUpdate, offset, flip } from "@floating-ui/dom";
33

44
// Connects to data-controller="ruby-ui--combobox"
55
export default class extends Controller {
@@ -54,7 +54,8 @@ export default class extends Controller {
5454
openPopover(event) {
5555
event.preventDefault()
5656

57-
this.popoverTarget.style.positionAnchor
57+
this.updatePopoverPosition()
58+
this.updatePopoverWidth()
5859
this.triggerTarget.ariaExpanded = "true"
5960
this.selectedItemIndex = null
6061
this.itemTargets.forEach(item => item.ariaCurrent = "false")
@@ -141,7 +142,8 @@ export default class extends Controller {
141142
updatePopoverPosition() {
142143
this.cleanup = autoUpdate(this.triggerTarget, this.popoverTarget, () => {
143144
computePosition(this.triggerTarget, this.popoverTarget, {
144-
middleware: [offset(4)],
145+
placement: 'bottom-start',
146+
middleware: [offset(4), flip()],
145147
}).then(({ x, y }) => {
146148
Object.assign(this.popoverTarget.style, {
147149
left: `${x}px`,
@@ -150,4 +152,8 @@ export default class extends Controller {
150152
});
151153
});
152154
}
155+
156+
updatePopoverWidth() {
157+
this.popoverTarget.style.width = `${this.triggerTarget.offsetWidth}px`
158+
}
153159
}

lib/ruby_ui/combobox/combobox_item.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ def view_template(&)
1010

1111
def default_attrs
1212
{
13-
class: "flex flex-row gap-2 items-center rounded-sm px-2 py-1.5 text-sm outline-none cursor-pointer select-none has-[:checked]:bg-accent hover:bg-accent p-2 [&>svg]:pointer-events-none [&>svg]:size-4 [&>svg]:shrink-0 aria-[current=true]:bg-accent aria-[current=true]:ring aria-[current=true]:ring-offset-2 ",
13+
class: [
14+
"flex flex-row w-full text-wrap truncate gap-2 items-center rounded-sm px-2 py-1.5 text-sm outline-none cursor-pointer",
15+
"select-none has-[:checked]:bg-accent hover:bg-accent p-2",
16+
"[&>svg]:pointer-events-none [&>svg]:size-4 [&>svg]:shrink-0 aria-[current=true]:bg-accent aria-[current=true]:ring aria-[current=true]:ring-offset-2"
17+
],
1418
role: "option",
1519
data: {
1620
ruby_ui__combobox_target: "item"

lib/ruby_ui/combobox/combobox_popover.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ def default_attrs
1616
popover: true,
1717
data: {
1818
ruby_ui__combobox_target: "popover",
19-
action: "keydown.down->ruby-ui--combobox#keyDownPressed keydown.up->ruby-ui--combobox#keyUpPressed keydown.enter->ruby-ui--combobox#keyEnterPressed keydown.esc->ruby-ui--combobox#closeDialog:prevent"
19+
action: %w[
20+
keydown.down->ruby-ui--combobox#keyDownPressed
21+
keydown.up->ruby-ui--combobox#keyUpPressed
22+
keydown.enter->ruby-ui--combobox#keyEnterPressed
23+
keydown.esc->ruby-ui--combobox#closeDialog:prevent
24+
resize@window->ruby-ui--combobox#updatePopoverWidth
25+
]
2026
}
2127
}
2228
end

lib/ruby_ui/combobox/combobox_radio.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ def default_attrs
1313
class: "aspect-square h-4 w-4 rounded-full border border-primary accent-primary text-primary shadow focus:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50",
1414
data: {
1515
ruby_ui__combobox_target: "input",
16-
action: "ruby-ui--combobox#inputChanged"
16+
ruby_ui__form_field_target: "input",
17+
action: %w[
18+
ruby-ui--combobox#inputChanged
19+
input->ruby-ui--form-field#onInput
20+
invalid->ruby-ui--form-field#onInvalid
21+
]
1722
}
1823
}
1924
end

lib/ruby_ui/combobox/combobox_search_input.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def view_template
1919
def default_attrs
2020
{
2121
type: "search",
22-
class: "flex h-10 w-full rounded-md bg-transparent py-3 text-sm outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed disabled:opacity-50",
22+
class: "flex h-10 w-full rounded-md bg-transparent py-3 text-sm outline-none border-none placeholder:text-muted-foreground disabled:cursor-not-allowed disabled:opacity-50",
2323
role: "searchbox",
2424
placeholder: @placeholder,
2525
data: {

lib/ruby_ui/combobox/combobox_trigger.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ def initialize(placeholder: "", **)
99

1010
def view_template
1111
button(**attrs) do
12-
span(data: {ruby_ui__combobox_target: "triggerContent"}) { @placeholder }
12+
span(class: "truncate", data: {ruby_ui__combobox_target: "triggerContent"}) do
13+
@placeholder
14+
end
1315
icon
1416
end
1517
end
@@ -19,7 +21,7 @@ def view_template
1921
def default_attrs
2022
{
2123
type: "button",
22-
class: "flex h-full w-full items-center whitespace-nowrap rounded-md text-sm ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 border border-input bg-background hover:bg-accent hover:text-accent-foreground h-10 px-4 py-2 w-[200px] justify-between",
24+
class: "flex h-full w-full items-center whitespace-nowrap rounded-md text-sm ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 border border-input bg-background hover:bg-accent hover:text-accent-foreground h-10 px-4 py-2 justify-between",
2325
data: {
2426
placeholder: @placeholder,
2527
ruby_ui__combobox_target: "trigger",

0 commit comments

Comments
 (0)