Skip to content

Commit 2f46e07

Browse files
authored
Add a separator component (#231)
1 parent 64cf320 commit 2f46e07

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

lib/ruby_ui/separator/separator.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
module RubyUI
4+
class Separator < Base
5+
ORIENTATIONS = %i[horizontal vertical].freeze
6+
7+
def initialize(as: :div, orientation: :horizontal, decorative: true, **attrs)
8+
raise ArgumentError, "Invalid orientation: #{orientation}" unless ORIENTATIONS.include?(orientation.to_sym)
9+
10+
@as = as.to_sym
11+
@orientation = orientation.to_sym
12+
@decorative = decorative
13+
super(**attrs)
14+
end
15+
16+
def view_template(&)
17+
tag(@as, **attrs, &)
18+
end
19+
20+
private
21+
22+
def default_attrs
23+
{
24+
role: (@decorative ? "none" : "separator"),
25+
class: [
26+
"shrink-0 bg-border",
27+
orientation_classes
28+
]
29+
}
30+
end
31+
32+
def orientation_classes
33+
return "h-[1px] w-full" if @orientation == :horizontal
34+
35+
"h-full w-[1px]"
36+
end
37+
end
38+
end

test/ruby_ui/separator_test.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
class RubyUI::SeparatorTest < ComponentTest
6+
def test_render
7+
output = phlex do
8+
RubyUI.Separator()
9+
end
10+
11+
assert_match(/div/, output)
12+
assert_match(/role="none"/, output)
13+
assert_match(/h-\[1px\]/, output)
14+
end
15+
16+
def test_render_with_vertical_orientation
17+
output = phlex do
18+
RubyUI.Separator(orientation: :vertical)
19+
end
20+
21+
assert_match(/w-\[1px\]/, output)
22+
end
23+
24+
def test_render_with_decorative_false
25+
output = phlex do
26+
RubyUI.Separator(decorative: false)
27+
end
28+
29+
assert_match(/role="separator"/, output)
30+
end
31+
32+
def test_render_with_custom_tag
33+
output = phlex do
34+
RubyUI.Separator(as: "hr")
35+
end
36+
37+
assert_match(/<hr/, output)
38+
end
39+
end

0 commit comments

Comments
 (0)