Skip to content

Commit 175f8d6

Browse files
KathrynmHansenKathryn Hansen
andauthored
add new link component with optional icon (#6)
* add new link component with optional icon * code review, add icon to alt text, change to a span, add new target --------- Co-authored-by: Kathryn Hansen <[email protected]>
1 parent 5852a2f commit 175f8d6

File tree

6 files changed

+62
-1
lines changed

6 files changed

+62
-1
lines changed
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<span class='flex'>
2+
<% if @icon.present? %>
3+
<%= image_tag icon_image_path, alt: icon_alt_text, class: "size-[20px]" %>
4+
<% end %>
5+
<%= link_to @label, @url, target: "_blank", rel: "noopener noreferrer" %>
6+
</span>

app/components/link_component.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
class LinkComponent < ViewComponent::Base
4+
def initialize(label:, url:, icon: nil)
5+
@label = label
6+
@url = url
7+
@icon = icon
8+
end
9+
10+
private
11+
12+
def icon_image_path
13+
case @icon
14+
when "info"
15+
"icons/information.svg"
16+
else
17+
""
18+
end
19+
end
20+
21+
def icon_alt_text
22+
descriptor = case @icon
23+
when "info"
24+
"info"
25+
else
26+
""
27+
end
28+
descriptor + " icon"
29+
end
30+
end

cfa_ui_components.gemspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ Gem::Specification.new do |spec|
1818
"lib/cfa_ui_components/**/*",
1919
"app/components/**/*",
2020
"app/assets/stylesheets/cfa_ui_components.tailwind.css",
21-
"lib/tasks/cfa_ui_components.rake"
21+
"lib/tasks/cfa_ui_components.rake",
22+
"app/assets/images/icons/*"
2223
]
2324
spec.require_paths = [ "lib" ]
2425

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
class LinkComponentTest < ViewComponent::TestCase
6+
def test_component_renders_something_useful
7+
# assert_equal(
8+
# %(<span>Hello, components!</span>),
9+
# render_inline(LinkComponent.new(message: "Hello, components!")).css("span").to_html
10+
# )
11+
end
12+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class LinkComponentPreview < ViewComponent::Preview
2+
def default
3+
render(LinkComponent.new(label: I18n.t("continue"), url: "/continue"))
4+
end
5+
6+
def with_icon
7+
render(LinkComponent.new(label: I18n.t("continue"), url: "/continue", icon: "info"))
8+
end
9+
end

0 commit comments

Comments
 (0)