Skip to content

Commit 803d586

Browse files
committed
Add a separator component
1 parent 67b8695 commit 803d586

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

lib/ruby_ui/separator/separator.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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
11+
@orientation = orientation.to_sym
12+
@decorative = decorative
13+
super(**attrs)
14+
end
15+
16+
def view_template(&)
17+
public_send(@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+
data: {
30+
orientation: @orientation
31+
}
32+
}
33+
end
34+
35+
def orientation_classes
36+
return "h-[1px] w-full" if @orientation == :horizontal
37+
38+
"h-full w-[1px]"
39+
end
40+
end
41+
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(/data-orientation="horizontal"/, 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(/data-orientation="vertical"/, 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)