You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

55 lines
1.7 KiB

# Font Awesome Helper
module FaIconHelper
LOGGER = SemanticLogger[Module.nesting[0]]
##
# mandatory:
# name: without 'fa-' prefix
# might be a symbol, automatic conversion of undashed symbol
# for instance symbol :magnify_lens is converted to string 'magnify-lens'
# optional:
# style: [:solid|:regular|:brands] solid as default value
# size: [2, 3, ..., 10 ]
# class: extra classes, any tailwind keyword...
# aria-hidden: true as default value
#
# example:
# fa_icon(:user)
# fa_icon(:user, style: :regular)
# fa_icon(:user, size: 2)
# fa_icon(:user, class: 'text-blue-500')
##
def fa_icon(name, html_options = {})
warn_useless_options html_options
fa_icon = "fa-#{to_dash(name)}"
fa_style = "fa-#{to_dash(html_options[:style] || :solid)}"
fa_size = to_fa_size(html_options[:size])
content_class = [fa_icon, fa_style, fa_size, html_options[:class]].compact.join(' ')
tag.i(class: content_class, 'aria-hidden': html_options['aria-hidden'] || true)
end
private
def to_dash(symbol) = symbol.to_s.dasherize
def to_fa_size(value)
return unless value
size = value.to_i
unless (1..10).include? size
LOGGER.warn "unparseable size <#{value}>, should be an integer from a range of 1 to 10!",
Rails.backtrace_cleaner.clean(caller[1..])&.first
return
end
return if size == 1
"fa-#{size}x"
end
def warn_useless_options(hash)
hash.except(:style, :size, :class, 'aria-hidden').each_key do |key|
LOGGER.warn "useless option <#{key}>, please consider removal for safety reason!",
Rails.backtrace_cleaner.clean(caller[3..])&.first
end
end
end