|
|
@ -1,32 +1,30 @@ |
|
|
|
# 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: |
|
|
|
# stroke: [:solid|:regular|:brands] solid as default value |
|
|
|
# size: [2x, 3x, ... ] |
|
|
|
# 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, stroke: :regular) |
|
|
|
# fa_icon(:user, size: '2x') |
|
|
|
# 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 |
|
|
|
|
|
|
|
icon = to_dash(name) |
|
|
|
stroke = to_dash(html_options[:stroke] || :solid) |
|
|
|
|
|
|
|
content_class = "fa-#{icon} fa-#{stroke}" |
|
|
|
content_class << " fa-#{html_options[:size]}" if html_options.key? :size |
|
|
|
content_class << " #{html_options[:class]}" if html_options.key? :class |
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
@ -34,9 +32,24 @@ module FaIconHelper |
|
|
|
|
|
|
|
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(:stroke, :size, :class, 'aria-hidden').each_key do |key| |
|
|
|
SemanticLogger[Module.nesting[0]].warn "useless option <#{key}>, please consider removal for safety reason!" |
|
|
|
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 |