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.

42 lines
1.3 KiB

  1. # Font Awesome Helper
  2. module FaIconHelper
  3. ##
  4. # mandatory:
  5. # name: without 'fa-' prefix
  6. # might be a symbol, automatic conversion of undashed symbol
  7. # for instance symbol :magnify_lens is converted to string 'magnify-lens'
  8. # optional:
  9. # stroke: [:solid|:regular|:brands] solid as default value
  10. # size: [2x, 3x, ... ]
  11. # class: extra classes, any tailwind keyword...
  12. # aria-hidden: true as default value
  13. #
  14. # example:
  15. # fa_icon(:user)
  16. # fa_icon(:user, stroke: :regular)
  17. # fa_icon(:user, size: '2x')
  18. # fa_icon(:user, class: 'text-blue-500')
  19. ##
  20. def fa_icon(name, html_options = {})
  21. warn_useless_options html_options
  22. icon = to_dash(name)
  23. stroke = to_dash(html_options[:stroke] || :solid)
  24. content_class = "fa-#{icon} fa-#{stroke}"
  25. content_class << " fa-#{html_options[:size]}" if html_options.key? :size
  26. content_class << " #{html_options[:class]}" if html_options.key? :class
  27. tag.i(class: content_class, 'aria-hidden': html_options['aria-hidden'] || true)
  28. end
  29. private
  30. def to_dash(symbol) = symbol.to_s.dasherize
  31. def warn_useless_options(hash)
  32. hash.except(:stroke, :size, :class, 'aria-hidden').each_key do |key|
  33. SemanticLogger[Module.nesting[0]].warn "useless option <#{key}>, please consider removal for safety reason!"
  34. end
  35. end
  36. end