rails_iowaicon 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,19 @@
1
+ module RailsIowaicon
2
+ class UndefinedVariant < StandardError
3
+ def initialize(msg = "", exception_type = "custom")
4
+ msg = msg.empty? ? "Variant should be one of #{RailsIowaicon::VARIANTS.join(", ")}" : msg
5
+ @exception_type = exception_type
6
+
7
+ super(msg)
8
+ end
9
+ end
10
+
11
+ class UndefinedIcon < StandardError
12
+ def initialize(icon = "", exception_type = "custom")
13
+ msg = "Couldn't find icon for #{icon}"
14
+ @exception_type = exception_type
15
+
16
+ super(msg)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,42 @@
1
+ require "action_view"
2
+
3
+ module RailsIowaicon
4
+ module Helper
5
+ # To add a iowaicon, call <tt><%= iowaicon "icon_name" %></tt> on your erb template.
6
+ # Head over to https://brand.uiowa.edu/graphic-elements to view all the icons.
7
+ #
8
+ # == Options
9
+ # The helper method accepts mutiple arguments such as:
10
+ #
11
+ # === Variant
12
+ # There are two types of variants: 'outline' and 'solid', the default being the 'outline'.
13
+ # To specify the solid variant, call <tt><%= iowaicon "icon_name", variant: "solid" %></tt>
14
+ #
15
+ # === HTML attributes
16
+ # Any <tt>html</tt> attribute is supported, for eg:
17
+ #
18
+ # <tt><%= iowaicon "icon_name", class: "text-gray-500", data: { controller: "icon" } %></tt>
19
+ #
20
+ # === Handling the icon size
21
+ # Normally, if you're just using vanilla iowaicons with tailwindcss, you'd set <tt>w-5 h-5</tt> as class attributes
22
+ # on the svg. With this helper, you just need to set the <tt>size</tt> attribute on the icon.
23
+ #
24
+ # <tt><%= iowaicon "icon_name", size: 20 %></tt>
25
+ #
26
+ # This will set the <tt>height</tt> and <tt>width</tt> attribute on the svg.
27
+ #
28
+ # If the variant is set as <tt>outline</tt>, size automatically defaults to 24, and if the variant is set as
29
+ # <tt>solid</tt>, size automatically defaults to 20. However, this can be over-written with the <tt>size</tt>
30
+ # attribute.
31
+ #
32
+ # == Accessibility
33
+ # The helper method automatically sets <tt>aria-hidden=true</tt> if <tt>aria-label</tt> is not set, and
34
+ # if <tt>aria-label</tt> is set, then <tt>role=img</tt> is set automatically.
35
+ def iowaicon(symbol, title: nil, **options)
36
+ icon = RailsIowaicon.new(symbol, **options)
37
+
38
+ title_tag = content_tag(:title, title) if title
39
+ content_tag(:svg, title_tag.to_s.html_safe + icon.svg_path.html_safe, icon.options)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,71 @@
1
+ require_relative "errors"
2
+
3
+ module RailsIowaicon
4
+ class RailsIowaicon
5
+ VARIANTS = %w[outline solid].freeze
6
+
7
+ attr_reader :options
8
+
9
+ def initialize(icon, variant: "outline", size: nil, **options)
10
+ raise UndefinedVariant unless VARIANTS.include?(variant.to_s)
11
+
12
+ @icon = icon.to_s
13
+ @variant = variant.to_s
14
+ @options = options
15
+ @size = icon_size_with(size)
16
+
17
+ @options.merge!(a11y)
18
+ @options.merge!({color: "#FFcd00"}) if solid?
19
+ @options.merge!({
20
+ viewBox: "0 0 50 50",
21
+ height: @size,
22
+ width: @size,
23
+ version: "1.1",
24
+ fill: outline? ? "none" : "currentColor",
25
+ stroke: solid? ? "none" : "currentColor"
26
+ })
27
+ end
28
+
29
+ # Finds the svg icon with respect to variant.
30
+ def svg_path
31
+ icon = ICONS.dig(@variant, @icon)
32
+ raise UndefinedIcon, @icon unless icon
33
+
34
+ icon
35
+ end
36
+
37
+ private
38
+
39
+ def a11y
40
+ accessible = {}
41
+
42
+ if @options[:"aria-label"].nil? && @options["aria-label"].nil? && @options.dig(:aria, :label).nil?
43
+ accessible[:"aria-hidden"] = "true"
44
+ else
45
+ accessible[:role] = "img"
46
+ end
47
+
48
+ accessible
49
+ end
50
+
51
+ # If the user has explicitly stated the size attribute, then use that. If size attribute is not passed
52
+ # then default to 24 if variant is outline, else default to 20 if variant is solid.
53
+ def icon_size_with(size)
54
+ if outline? && size.nil?
55
+ 24
56
+ elsif solid? && size.nil?
57
+ 20
58
+ else
59
+ size
60
+ end
61
+ end
62
+
63
+ def outline?
64
+ @variant == "outline"
65
+ end
66
+
67
+ def solid?
68
+ @variant == "solid"
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,9 @@
1
+ require_relative "helper"
2
+
3
+ module RailsIowaicon
4
+ class Railtie < Rails::Railtie
5
+ initializer "rails_iowaicon.helper" do
6
+ ActionView::Base.send :include, Helper # rubocop:disable Lint/SendWithMixinArgument
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module RailsIowaicon
2
+ VERSION = "1.0.4".freeze
3
+ end
@@ -0,0 +1,9 @@
1
+ require "json"
2
+ require_relative "rails_iowaicon/version"
3
+ require_relative "rails_iowaicon/rails_iowaicon"
4
+ require_relative "rails_iowaicon/railtie" if defined? Rails
5
+
6
+ module RailsIowaicon
7
+ ICON_PATH = File.join(File.dirname(__FILE__), "../compressed/icons.json")
8
+ ICONS = JSON.parse(File.read(ICON_PATH)).freeze
9
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_iowaicon
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.4
5
+ platform: ruby
6
+ authors:
7
+ - chrisortman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-04-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ - chris-ortman@uiowa.edu
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE.txt
35
+ - README.md
36
+ - compressed/icons.json
37
+ - lib/rails_iowaicon.rb
38
+ - lib/rails_iowaicon/errors.rb
39
+ - lib/rails_iowaicon/helper.rb
40
+ - lib/rails_iowaicon/rails_iowaicon.rb
41
+ - lib/rails_iowaicon/railtie.rb
42
+ - lib/rails_iowaicon/version.rb
43
+ homepage: https://git.uiowa.edu/cortman/rails-iowaicons
44
+ licenses:
45
+ - MIT
46
+ metadata:
47
+ homepage_uri: https://git.uiowa.edu/cortman/rails-iowaicons
48
+ source_code_uri: https://git.uiowa.edu/cortman/rails-iowaicons
49
+ changelog_uri: https://git.uiowa.edu/cortman/rails-iowaicon/blob/main/CHANGELOG.md
50
+ rubygems_mfa_required: 'true'
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 2.6.6
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubygems_version: 3.2.32
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Ruby on Rails view helpers for Iowa brand icons
70
+ test_files: []