Module: FontawesomeCdn::Helpers
- Defined in:
- lib/fontawesome_cdn.rb,
lib/fontawesome_cdn/helpers/icon.rb,
lib/fontawesome_cdn/helpers/include_font_awesome.rb
Overview
View helpers for loading Font Awesome assets (CDN or kit)
Constant Summary collapse
- FAMILIES =
%w[ classic duotone sharp sharp-duotone brands chisel etch jelly notdog slab thumbprint utility whiteboard ].freeze
- STYLES =
%w[solid regular light thin].freeze
Instance Method Summary collapse
-
#icon(name, text = nil, **options) ⇒ Object
Renders a Font Awesome icon.
-
#include_font_awesome(version = nil, kit: nil, **options) ⇒ Object
Helper for loading Font Awesome.
Instance Method Details
#icon(name, text = nil, **options) ⇒ Object
Renders a Font Awesome icon
Basic usage (default family and style are applied automatically):
<%= icon "user" %>
<%= icon "gear", "Settings" %>
<%= icon "bell", class: "fa-regular fa-2x fa-shake" %>
Using the fa: shortcut (tokens are automatically prefixed with fa-):
<%= icon "bell", fa: "regular 2x shake" %>
<%= icon "github", "Source code", fa: "brands", class: "link" %>
<%= icon "alien", fa: "duotone light" %>
Accessibility:
<%= icon "user", aria-hidden: false %>
Notes:
- Default family and style are applied unless explicitly overridden.
- No validation or conflict resolution is performed on classes.
- Users are free to combine multiple styles or families if needed.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/fontawesome_cdn/helpers/icon.rb', line 36 def icon(name, text = nil, **) # Allow the 2nd argument to be either text or the options hash if text.is_a?(Hash) = text text = nil end aria_hidden = if .key?(:"aria-hidden") .delete(:"aria-hidden") else FontawesomeCdn.configuration.default_aria_hidden end # "special" options we consume class_tokens = .delete(:class).to_s.split fa_tokens = .delete(:fa).to_s.split.map { |t| "fa-#{t}" } tokens = (class_tokens + fa_tokens).uniq family = tokens .map { |t| t.delete_prefix("fa-") } .find { |t| FAMILIES.include?(t) } || FontawesomeCdn.configuration.default_family.to_s style = tokens .map { |t| t.delete_prefix("fa-") } .find { |t| STYLES.include?(t) } || FontawesomeCdn.configuration.default_style.to_s # remove family + style from tokens so they can't reappear later tokens.delete("fa-#{family}") tokens.delete("fa-#{style}") classes = [] classes << "fa-#{family}" unless family == "classic" classes << "fa-#{style}" unless family == "brands" classes << "fa-#{name}" classes.concat(tokens) # keep remaining options as HTML attributes (style, data, id, title, ...) = .merge( class: classes.uniq.join(" "), "aria-hidden": aria_hidden ) icon_tag = content_tag(:i, nil, **) return icon_tag if text.nil? || text.to_s.empty? safe_join([icon_tag, ERB::Util.html_escape(text.to_s)], " ") end |
#include_font_awesome(version = nil, kit: nil, **options) ⇒ Object
Helper for loading Font Awesome
<%= include_font_awesome "7.3.0" %>
# => <link rel="stylesheet" ...> to cdnjs
<%= include_font_awesome kit: "YOUR-KIT" %>
# => <script src="https://kit.fontawesome.com/YOUR-KIT.js" crossorigin="anonymous"></script>
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/fontawesome_cdn/helpers/include_font_awesome.rb', line 14 def include_font_awesome(version = nil, kit: nil, **) # Prevent ambiguous usage raise ArgumentError, "Use either the version argument or the :kit option, not both" if version && kit if kit attrs = { src: "#{FontawesomeCdn::KIT_BASE_URL}/#{kit}.js", crossorigin: "anonymous" } tag.script(nil, **attrs, **) else validate_fontawesome_version!(version) attrs = { rel: "stylesheet", href: "#{FontawesomeCdn::CDN_BASE_URL}/#{version}/css/all.min.css", integrity: FontawesomeCdn::CDN_INTEGRITY_MAP[version], crossorigin: "anonymous", referrerpolicy: "no-referrer" } tag.link(**attrs, **) end end |