Skip to content

Commit 14dd0d3

Browse files
HDingercamertron
andauthored
Add support for trailing and leading visual icons in Primer::Beta::Link (second try) (#3041)
Co-authored-by: Cameron Dutro <[email protected]> Co-authored-by: camertron <[email protected]>
1 parent 6e35af0 commit 14dd0d3

File tree

14 files changed

+117
-42
lines changed

14 files changed

+117
-42
lines changed

.changeset/odd-dots-laugh.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/view-components': minor
3+
---
4+
5+
Support leading and trailing icons for Links
-120 Bytes
Loading
1.14 KB
Loading
1.11 KB
Loading

app/components/primer/base_component.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,16 @@ class BaseComponent < Primer::Component
151151
# | :- | :- | :- |
152152
# | classes | String | CSS class name value to be concatenated with generated Primer CSS classes. |
153153
# | test_selector | String | Adds `data-test-selector='given value'` in non-Production environments for testing purposes. |
154+
# | trim | Boolean | Calls `strip` on the content to remove trailing and leading white spaces. |
154155
def initialize(tag:, classes: nil, **system_arguments)
155156
@tag = tag
156157

157158
@system_arguments = validate_arguments(tag: tag, **system_arguments)
158159

159160
@result = Primer::Classify.call(**@system_arguments.merge(classes: classes))
160161

162+
@trim = !!@system_arguments.delete(:trim)
163+
161164
@system_arguments[:"data-view-component"] = true
162165
# Filter out Primer keys so they don't get assigned as HTML attributes
163166
@content_tag_args = add_test_selector(@system_arguments).except(*Primer::Classify::Utilities::UTILITIES.keys)
@@ -167,7 +170,7 @@ def call
167170
if SELF_CLOSING_TAGS.include?(@tag)
168171
tag(@tag, @content_tag_args.merge(@result))
169172
else
170-
content_tag(@tag, content, @content_tag_args.merge(@result))
173+
content_tag(@tag, @trim ? trimmed_content : content, @content_tag_args.merge(@result))
171174
end
172175
end
173176
end

app/components/primer/beta/button.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,6 @@ def before_render
159159
"Button--invisible-noVisuals"
160160
)
161161
end
162-
163-
def trimmed_content
164-
return if content.blank?
165-
166-
trimmed_content = content.strip
167-
168-
return trimmed_content unless content.html_safe?
169-
170-
# strip unsets `html_safe`, so we have to set it back again to guarantee that HTML blocks won't break
171-
trimmed_content.html_safe # rubocop:disable Rails/OutputSafety
172-
end
173162
end
174163
end
175164
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<%= render Primer::ConditionalWrapper.new(condition: tooltip?, trim: true, tag: :span, position: :relative) do %>
2+
<%= render(Primer::BaseComponent.new(trim: true, **@system_arguments)) do %>
3+
<%= render(Primer::BaseComponent.new(tag: :span, classes: "Link-content", trim: true)) do %>
4+
<% if leading_visual %>
5+
<%= leading_visual %>
6+
<% end %>
7+
<%= content %>
8+
<% if trailing_visual %>
9+
<%= trailing_visual %>
10+
<% end %>
11+
<% end %>
12+
<% end %>
13+
<%= tooltip if tooltip? %>
14+
<% end %>

app/components/primer/beta/link.pcss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,12 @@
6767
color: inherit !important;
6868
}
6969
}
70+
71+
.Link-content {
72+
display: inline-flex;
73+
align-items: center;
74+
/* stylelint-disable-next-line primer/typography */
75+
line-height: normal;
76+
gap: var(--base-size-4);
77+
text-decoration: inherit;
78+
}

app/components/primer/beta/link.rb

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,32 @@ class Link < Primer::Component
3030
Primer::Alpha::Tooltip.new(**system_arguments)
3131
}
3232

33+
# Leading visuals appear to the left of the link text.
34+
#
35+
# Use:
36+
#
37+
# - `leading_visual_icon` which accepts the arguments accepted by <%= link_to_component(Primer::Beta::Octicon) %>.
38+
#
39+
# @param system_arguments [Hash] Same arguments as <%= link_to_component(Primer::Beta::Octicon) %>.
40+
renders_one :leading_visual, types: {
41+
icon: lambda { |**system_arguments|
42+
Primer::Beta::Octicon.new(**system_arguments)
43+
}
44+
}
45+
46+
# Trailing visuals appear to the right of the link text.
47+
#
48+
# Use:
49+
#
50+
# - `trailing_visual_icon` which accepts the arguments accepted by <%= link_to_component(Primer::Beta::Octicon) %>.
51+
#
52+
# @param system_arguments [Hash] Same arguments as <%= link_to_component(Primer::Beta::Octicon) %>.
53+
renders_one :trailing_visual, types: {
54+
icon: lambda { |**system_arguments|
55+
Primer::Beta::Octicon.new(**system_arguments)
56+
}
57+
}
58+
3359
# @param href [String] URL to be used for the Link. Required. If the requirements are not met an error will be raised in non production environments. In production, an empty link element will be rendered.
3460
# @param scheme [Symbol] <%= one_of(Primer::Beta::Link::SCHEME_MAPPINGS.keys) %>
3561
# @param muted [Boolean] Uses light gray for Link color, and blue on hover.
@@ -54,20 +80,6 @@ def initialize(href: nil, scheme: DEFAULT_SCHEME, muted: false, underline: false
5480
def before_render
5581
raise ArgumentError, "href is required" if @system_arguments[:href].nil? && !Rails.env.production?
5682
end
57-
58-
def call
59-
if tooltip.present?
60-
render Primer::BaseComponent.new(tag: :span, position: :relative) do
61-
render(Primer::BaseComponent.new(**@system_arguments)) do
62-
content
63-
end.to_s + tooltip.to_s
64-
end
65-
else
66-
render(Primer::BaseComponent.new(**@system_arguments)) do
67-
content
68-
end
69-
end
70-
end
7183
end
7284
end
7385
end

app/components/primer/button_component.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,5 @@ def initialize(
111111
def link?
112112
@scheme == LINK_SCHEME
113113
end
114-
115-
def trimmed_content
116-
return if content.blank?
117-
118-
trimmed_content = content.strip
119-
120-
return trimmed_content unless content.html_safe?
121-
122-
# strip unsets `html_safe`, so we have to set it back again to guarantee that HTML blocks won't break
123-
trimmed_content.html_safe # rubocop:disable Rails/OutputSafety
124-
end
125114
end
126115
end

0 commit comments

Comments
 (0)