font-awesome-sass icon indicating copy to clipboard operation
font-awesome-sass copied to clipboard

Support for icon on right of text for helper

Open jwoertink opened this issue 10 years ago • 5 comments

Right now with the helper icon you can do

button type="button" class="btn btn-default"
  = icon('arrow-right', 'Login')  # <i class="fa fa-arrow-right"></i> Login

It would be nice if there was a way to specify the icon on the right hand side.

button type="button" class="btn btn-default"
  = icon('arrow-right', 'Login', rtl: true) # Login <i class="fa fa-arrow-right"></i>

jwoertink avatar Nov 25 '14 17:11 jwoertink

bump

rhnorment avatar Dec 19 '14 16:12 rhnorment

This is an ugly way of doing it, but you can make it into a helper like icon_right('angle-double-right','Next'), but the below does allow for the icon to show on the right.

<%= link_to "Next #{icon('angle-double-right')}".html_safe, "#", class: 'btn btn-default' %>

kobaltz avatar Apr 20 '15 18:04 kobaltz

The font-awesome-rails gem has similar functionality, implemented as follows:

= icon('arrow-right', 'Login', right: true) # Login <i class="fa fa-arrow-right"></i>

andreykul avatar Oct 21 '15 16:10 andreykul

I'm assuming there's been no resolution to this issue?

kampeynco avatar Feb 25 '19 19:02 kampeynco

I'm assuming there's been no resolution to this issue?

Looking at the font-awesome-rails helper as well as this gems helper.

The solution below is working as a replacement allowing you to add the option to position the icon to the right of any text.

fa_icon('fab', 'font-awesome', 'Font Awesome', id: 'my-icon', class: 'strong', right: true)
def fa_icon(style, name, text = nil, html_options = {})
  text, html_options = nil, text if text.is_a?(Hash)

  content_class = "#{style} fa-#{name}"
  content_class += " #{html_options[:class]}" if html_options.key?(:class)
  html_options[:class] = content_class

  icon = content_tag(:i, nil, html_options)
  right_icon = html_options.delete(:right)
  icon_join(icon, text, right_icon)
end
private

def icon_join(icon, text, reverse_order = false)
  return icon if text.blank?

  elements = [icon, ERB::Util.html_escape(text)]
  elements.reverse! if reverse_order
  safe_join(elements, ' ')
end

CapDev avatar Apr 27 '20 00:04 CapDev