Skip to content

Commit 398415a

Browse files
authored
Add breadcrumb component (#207)
1 parent 733ba94 commit 398415a

File tree

8 files changed

+196
-0
lines changed

8 files changed

+196
-0
lines changed

lib/ruby_ui/breadcrumb/breadcrumb.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
module RubyUI
4+
class Breadcrumb < Base
5+
def view_template(&)
6+
nav(**attrs, &)
7+
end
8+
9+
private
10+
11+
def default_attrs
12+
{
13+
aria: {label: "breadcrumb"}
14+
}
15+
end
16+
end
17+
end
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+
module RubyUI
4+
class BreadcrumbEllipsis < Base
5+
def view_template(&)
6+
span(**attrs) do
7+
icon
8+
span(class: "sr-only") { "More" }
9+
end
10+
end
11+
12+
private
13+
14+
def icon
15+
svg(
16+
xmlns: "http://www.w3.org/2000/svg",
17+
class: "w-4 h-4",
18+
viewbox: "0 0 24 24",
19+
fill: "none",
20+
stroke: "currentColor",
21+
stroke_width: "2",
22+
stroke_linecap: "round",
23+
stroke_linejoin: "round"
24+
) do |s|
25+
s.circle(cx: "12", cy: "12", r: "1")
26+
s.circle(cx: "19", cy: "12", r: "1")
27+
s.circle(cx: "5", cy: "12", r: "1")
28+
end
29+
end
30+
31+
def default_attrs
32+
{
33+
aria: {hidden: true},
34+
class: "flex h-9 w-9 items-center justify-center",
35+
role: "presentation"
36+
}
37+
end
38+
end
39+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
module RubyUI
4+
class BreadcrumbItem < Base
5+
def view_template(&)
6+
li(**attrs, &)
7+
end
8+
9+
private
10+
11+
def default_attrs
12+
{
13+
class: "inline-flex items-center gap-1.5"
14+
}
15+
end
16+
end
17+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
module RubyUI
4+
class BreadcrumbLink < Base
5+
def initialize(href: "#", **attrs)
6+
@href = href
7+
super(**attrs)
8+
end
9+
10+
def view_template(&)
11+
a(href: @href, **attrs, &)
12+
end
13+
14+
private
15+
16+
def default_attrs
17+
{
18+
class: "transition-colors hover:text-foreground"
19+
}
20+
end
21+
end
22+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
module RubyUI
4+
class BreadcrumbList < Base
5+
def view_template(&)
6+
ol(**attrs, &)
7+
end
8+
9+
private
10+
11+
def default_attrs
12+
{
13+
class: "flex flex-wrap items-center gap-1.5 break-words text-sm text-muted-foreground sm:gap-2.5"
14+
}
15+
end
16+
end
17+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
module RubyUI
4+
class BreadcrumbPage < Base
5+
def view_template(&)
6+
span(**attrs, &)
7+
end
8+
9+
private
10+
11+
def default_attrs
12+
{
13+
aria: {disabled: true, current: "page"},
14+
class: "font-normal text-foreground",
15+
role: "link"
16+
}
17+
end
18+
end
19+
end
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 BreadcrumbSeparator < Base
5+
def view_template(&block)
6+
li(**attrs) do
7+
if block
8+
block.call
9+
else
10+
icon
11+
end
12+
end
13+
end
14+
15+
private
16+
17+
def icon
18+
svg(
19+
xmlns: "http://www.w3.org/2000/svg",
20+
class: "w-4 h-4",
21+
viewbox: "0 0 24 24",
22+
fill: "none",
23+
stroke: "currentColor",
24+
stroke_width: "2",
25+
stroke_linecap: "round",
26+
stroke_linejoin: "round"
27+
) { |s| s.path(d: "m9 18 6-6-6-6") }
28+
end
29+
30+
def default_attrs
31+
{
32+
aria: {hidden: true},
33+
class: "[&>svg]:w-3.5 [&>svg]:h-3.5",
34+
role: "presentation"
35+
}
36+
end
37+
end
38+
end

test/ruby_ui/breadcrumb_test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
class RubyUI::BreadcrumbTest < ComponentTest
6+
def test_render_with_all_items
7+
output = phlex do
8+
RubyUI.Breadcrumb do
9+
RubyUI.BreadcrumbList do
10+
RubyUI.BreadcrumbItem do
11+
RubyUI.BreadcrumbLink(href: "#") { "Home" }
12+
end
13+
RubyUI.BreadcrumbSeparator()
14+
RubyUI.BreadcrumbItem do
15+
RubyUI.BreadcrumbLink(href: "/docs/accordion") { "Components" }
16+
end
17+
RubyUI.BreadcrumbSeparator()
18+
RubyUI.BreadcrumbItem do
19+
RubyUI.BreadcrumbPage { "Breadcrumb" }
20+
end
21+
end
22+
end
23+
end
24+
25+
assert_match(/Components/, output)
26+
end
27+
end

0 commit comments

Comments
 (0)