Some arguments for UI components are optional, so we don't have to provide all of them when using them. I had added a `defined?(name)` check to use default values when an argument is not provided. Jeremy pointed out that it was working by accident. In the code `name = defined?(name)`, `defined?(name)` always returns `local-variable` because it’s on the right-hand side of the assignment, while the local variable is on the left-hand side. When we remove the `defined?` calls, the remaining code is equivalent to `||=`.
50 lines
1.6 KiB
Plaintext
50 lines
1.6 KiB
Plaintext
<% type ||= "primary"
|
|
link ||= nil
|
|
icon ||= nil
|
|
extra_class ||= nil
|
|
attributes ||= {}
|
|
right_icon ||= nil %>
|
|
|
|
<% case type %>
|
|
<% when "primary" %>
|
|
<% color = "bg-orange-600 hover:bg-orange-700 focus-visible:outline-orange-600" %>
|
|
<% when "danger" %>
|
|
<% color = "bg-rose-600 hover:bg-rose-700 focus-visible:outline-rose-600" %>
|
|
<% end %>
|
|
|
|
<% if link %>
|
|
<a
|
|
href="<%= link %>"
|
|
class="inline-flex items-center justify-center rounded-md px-3 py-2 text-sm font-semibold text-white shadow-sm focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 <%= color %> <%= extra_class %>"
|
|
<% attributes.each do |atr_key, atr_value| %>
|
|
<%= atr_key %>="<%= atr_value %>"
|
|
<% end%>
|
|
>
|
|
<% if icon %>
|
|
<%== render("components/icon", locals: { name: icon, classes: "ml-0.5 mr-1.5 h-5 w-5" }) %>
|
|
<% end %>
|
|
<%= text %>
|
|
</a>
|
|
<% else %>
|
|
<button
|
|
class="inline-flex items-center justify-center rounded-md px-3 py-2 text-sm font-semibold text-white shadow-sm focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 <%= color %> <%= extra_class %>"
|
|
<% attributes.each do |atr_key, atr_value| %>
|
|
<%= atr_key %>="<%= atr_value %>"
|
|
<% end%>
|
|
>
|
|
<% if icon %>
|
|
<%== render(
|
|
"components/icon",
|
|
locals: {
|
|
name: icon,
|
|
classes: "h-5 w-5 #{(text && !text.empty?) ? "ml-0.5 mr-1.5" : "mx-0"}"
|
|
}
|
|
) %>
|
|
<% end %>
|
|
<%= text %>
|
|
<% if right_icon %>
|
|
<%== render("components/icon", locals: { name: right_icon, classes: "ml-1.5 -mr-1 h-5 w-5" }) %>
|
|
<% end %>
|
|
</button>
|
|
<% end %>
|