Skip to content

Commit e6f1932

Browse files
committed
Allow to pass strategy option to dropdown menu
1 parent eea116e commit e6f1932

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

lib/ruby_ui/dropdown_menu/dropdown_menu.rb

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ def view_template(&)
1515

1616
def default_attrs
1717
{
18+
class: [
19+
"group/dropdown-menu",
20+
(@strategy == "absolute") ? "is-absolute" : "is-fixed"
21+
],
1822
data: {
1923
controller: "ruby-ui--dropdown-menu",
2024
action: "click@window->ruby-ui--dropdown-menu#onClickOutside",

lib/ruby_ui/dropdown_menu/dropdown_menu_content.rb

+16-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module RubyUI
44
class DropdownMenuContent < Base
55
def view_template(&block)
6-
div(data: {ruby_ui__dropdown_menu_target: "content"}, class: "hidden", style: "width: max-content; position: absolute; top: 0; left: 0;") do
6+
div(**wrapper_attrs) do
77
div(**attrs, &block)
88
end
99
end
@@ -18,5 +18,20 @@ def default_attrs
1818
class: "z-50 min-w-[8rem] overflow-hidden rounded-md border bg-background p-1 text-foreground shadow-md data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 w-56"
1919
}
2020
end
21+
22+
def wrapper_attrs
23+
{
24+
class: [
25+
"z-50 hidden group-[.is-absolute]/dropdown-menu:absolute",
26+
"group-[.is-fixed]/dropdown-menu:fixed"
27+
],
28+
data: {ruby_ui__dropdown_menu_target: "content"},
29+
style: {
30+
width: "max-content",
31+
top: "0",
32+
left: "0"
33+
}
34+
}
35+
end
2136
end
2237
end

test/ruby_ui/dropdown_menu_test.rb

+40
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,44 @@ def test_render_with_all_items
2222

2323
assert_match(/Open/, output)
2424
end
25+
26+
def test_render_with_strategy_absolute
27+
output = phlex do
28+
RubyUI.DropdownMenu(options: {strategy: "absolute"}) do
29+
RubyUI.DropdownMenuTrigger(class: "w-full") do
30+
RubyUI.Button(variant: :outline) { "Open" }
31+
end
32+
RubyUI.DropdownMenuContent do
33+
RubyUI.DropdownMenuLabel { "My Account" }
34+
RubyUI.DropdownMenuSeparator
35+
RubyUI.DropdownMenuItem(href: "#") { "Profile" }
36+
RubyUI.DropdownMenuItem(href: "#") { "Billing" }
37+
RubyUI.DropdownMenuItem(href: "#") { "Team" }
38+
RubyUI.DropdownMenuItem(href: "#") { "Subscription" }
39+
end
40+
end
41+
end
42+
43+
assert_match(/is-absolute/, output)
44+
end
45+
46+
def test_render_with_strategy_fixed
47+
output = phlex do
48+
RubyUI.DropdownMenu(options: {strategy: "fixed"}) do
49+
RubyUI.DropdownMenuTrigger(class: "w-full") do
50+
RubyUI.Button(variant: :outline) { "Open" }
51+
end
52+
RubyUI.DropdownMenuContent do
53+
RubyUI.DropdownMenuLabel { "My Account" }
54+
RubyUI.DropdownMenuSeparator
55+
RubyUI.DropdownMenuItem(href: "#") { "Profile" }
56+
RubyUI.DropdownMenuItem(href: "#") { "Billing" }
57+
RubyUI.DropdownMenuItem(href: "#") { "Team" }
58+
RubyUI.DropdownMenuItem(href: "#") { "Subscription" }
59+
end
60+
end
61+
end
62+
63+
assert_match(/is-fixed/, output)
64+
end
2565
end

0 commit comments

Comments
 (0)