Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 41 additions & 0 deletions lib/ruby_ui/separator/separator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

module RubyUI
class Separator < Base
ORIENTATIONS = %i[horizontal vertical].freeze

def initialize(as: "div", orientation: :horizontal, decorative: true, **attrs)
raise ArgumentError, "Invalid orientation: #{orientation}" unless ORIENTATIONS.include?(orientation.to_sym)

@as = as
@orientation = orientation.to_sym
@decorative = decorative
super(**attrs)
end

def view_template(&)
public_send(@as, **attrs, &)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The as parameter could be a symbol, and then we could use tag phlex method here, eg.

Suggested change
public_send(@as, **attrs, &)
tag @as, **attrs, &

Copy link
Contributor Author

@lsouoliveira lsouoliveira Mar 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added it as tag(@as, **attrs, &) because, without the parentheses, the syntax highlighting was incorrect:

image

end

private

def default_attrs
{
role: (@decorative ? "none" : "separator"),
class: [
"shrink-0 bg-border",
orientation_classes
],
data: {
orientation: @orientation
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this data-attribute relevant (eg. for accessibility)? If not, we can remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's only for styling, allowing different styles to be applied based on the value of data-orientation.

}
end

def orientation_classes
return "h-[1px] w-full" if @orientation == :horizontal

"h-full w-[1px]"
end
end
end
39 changes: 39 additions & 0 deletions test/ruby_ui/separator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

require "test_helper"

class RubyUI::SeparatorTest < ComponentTest
def test_render
output = phlex do
RubyUI.Separator()
end

assert_match(/div/, output)
assert_match(/role="none"/, output)
assert_match(/data-orientation="horizontal"/, output)
end

def test_render_with_vertical_orientation
output = phlex do
RubyUI.Separator(orientation: :vertical)
end

assert_match(/data-orientation="vertical"/, output)
end

def test_render_with_decorative_false
output = phlex do
RubyUI.Separator(decorative: false)
end

assert_match(/role="separator"/, output)
end

def test_render_with_custom_tag
output = phlex do
RubyUI.Separator(as: "hr")
end

assert_match(/hr/, output)
end
end