heroicons_helper 0.2.2 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1567311352d143cb38d0ebf41388ab02cffa75dde06c9aa02694d06dfde7e3ef
4
- data.tar.gz: b865c9123848efdf54afda2336491a15f7289a9e2f91a2a005731436e248c01a
3
+ metadata.gz: 80e090d354ed7b42e47a7436e723b4bd438a75aaa558856a830ed78a3fcbb3d5
4
+ data.tar.gz: 475b59fd40ed8e85775eb620131fa0425f17608d6d1c8f2d1f4aa90c8327735b
5
5
  SHA512:
6
- metadata.gz: 9461351e92db53399116d07f77cbcde4094f50201f550b02d8dcaa125e9671a90b84300c831610be743c6be72d7f578c1069f9ec6e505af484481797225f9c91
7
- data.tar.gz: 67d4d42f881e0d435494eaa79890b55022d6187d2f99b26dea652d5a0517203ffcc4f531da01a64ed04b8a15f405f9e080539b25999ca823804b49148c7164aa
6
+ metadata.gz: d45fea85415d4fac7ed79f0cf1563dd3fd7dabf87780714dc41d5e3bc09ca3d9581d58f1312db7be08f9803fad2389417b90d3aea1fb74acb18c0ffe8b4ad1ff
7
+ data.tar.gz: 703138870cb157f666d37d3df838dfaccd1b5d12d20c2e98c78ec7db2b57ed804c074c00a220d61e431104b62ae9168eb9eb9c12c88805ab9a24581fe8c53d09
data/README.md CHANGED
@@ -22,10 +22,10 @@ require "heroicons_helper"
22
22
  include HeroiconsHelper
23
23
  ```
24
24
 
25
- You'll have a brand new method called `heroicons` whose signature looks like this:
25
+ You'll have a brand new method called `heroicon` whose signature looks like this:
26
26
 
27
27
  ```ruby
28
- heroicons(symbol, variant, attributes: {})
28
+ heroicon(symbol, variant, attributes: {})
29
29
  ```
30
30
 
31
31
  where
@@ -37,7 +37,7 @@ where
37
37
  This one method call returns an object that represents the Heroicon, and you should call `to_svg` to get the resulting SVG string:
38
38
 
39
39
  ```ruby
40
- outline_icon = heroicons("x", "outline")
40
+ outline_icon = heroicon("x", variant: HeroiconsHelper::Icon::VARIANT_OUTLINE)
41
41
  puts outline_icon.to_svg
42
42
  ```
43
43
  ```
@@ -58,7 +58,7 @@ icons_to_preload = [{
58
58
  },]
59
59
 
60
60
  HeroiconsHelper::Cache.preload!(icons_to_preload) do |found, icon|
61
- # An instance of `FakeClass` is stored in the cache
61
+ # An instance of `FakeClass` will be stored in the cache
62
62
  FakeClass.new(icon) unless found
63
63
  end
64
64
  ```
@@ -3,22 +3,29 @@
3
3
  module HeroiconsHelper
4
4
  # Icon to show heroicons by name and variant.
5
5
  class Icon
6
- attr_reader :path, :attributes, :width, :height, :symbol, :variant
6
+ attr_reader :path, :attributes, :width, :height, :symbol, :variant, :keywords
7
7
 
8
8
  VARIANT_OUTLINE = "outline"
9
9
  VARIANT_SOLID = "solid"
10
10
  VARIANTS = [VARIANT_OUTLINE, VARIANT_SOLID].freeze
11
11
 
12
12
  def initialize(symbol, variant, attributes: {})
13
- @symbol = symbol
14
- @variant = variant
13
+ @symbol = symbol.to_s
14
+ @variant = variant.to_s
15
15
 
16
- icon = get_heroicon(@symbol, @variant)
16
+ heroicon = get_heroicon(@symbol, @variant)
17
17
 
18
- @path = icon["path"]
19
- @width = icon["width"]
20
- @height = icon["height"]
21
- @attributes = attributes.dup.merge!(default_attributes)
18
+ @path = heroicon["path"]
19
+ @width = heroicon["width"]
20
+ @height = heroicon["height"]
21
+ @keywords = heroicon["keywords"]
22
+ @attributes = attributes.dup.compact
23
+ @attributes[:class] = classes
24
+ @attributes[:viewBox] = viewbox
25
+ @attributes.merge!(size)
26
+ @attributes[:version] = "1.1"
27
+ @attributes.merge!(variant_attributes)
28
+ @attributes.merge!(a11y)
22
29
  end
23
30
 
24
31
  # Returns an string representing a <svg> tag
@@ -26,53 +33,93 @@ module HeroiconsHelper
26
33
  "<svg xmlns=\"http://www.w3.org/2000/svg\" #{html_attributes}>#{@path}</svg>"
27
34
  end
28
35
 
29
- private
30
-
31
- def html_attributes
36
+ private def html_attributes
32
37
  attrs = []
33
38
  @attributes.each_pair { |attr, value| attrs << "#{attr}=\"#{value}\"" }
34
39
  attrs.map(&:strip).join(" ")
35
40
  end
36
41
 
37
- def default_attributes
42
+ # add some accessibility features to svg
43
+ private def a11y
44
+ accessible = {}
45
+
46
+ if @attributes[:"aria-label"].nil? && @attributes["aria-label"].nil?
47
+ accessible[:"aria-hidden"] = "true"
48
+ else
49
+ accessible[:role] = "img"
50
+ end
51
+
52
+ accessible
53
+ end
54
+
55
+ # prepare the octicon class
56
+ private def classes
57
+ "heroicon heroicon-#{@symbol}-#{@variant} #{@attributes[:class]} ".strip
58
+ end
59
+
60
+ private def variant_attributes
38
61
  case @variant
39
62
  when VARIANT_OUTLINE
40
63
  {
41
- viewBox: viewbox,
42
64
  fill: "none",
43
65
  stroke: "currentColor",
44
66
  }
45
67
  when VARIANT_SOLID
46
68
  {
47
- viewBox: viewbox,
48
69
  fill: "currentColor",
49
70
  }
50
71
  end
51
72
  end
52
73
 
53
- def viewbox
74
+ private def viewbox
54
75
  "0 0 #{@width} #{@height}"
55
76
  end
56
77
 
57
- def get_heroicon(symbol, variant)
58
- symbol_s = symbol.to_s
59
- raise ArgumentError, "Icon name can't be empty" if symbol_s.empty?
60
- variant_s = variant.to_s
61
- raise ArgumentError, "Variant `#{variant.inspect}` is invalid; must be one of #{VARIANTS.join(", ")}" unless VARIANTS.include?(variant_s)
78
+ # determine the height and width of the octicon based on :size option
79
+ private def size
80
+ size = {
81
+ width: @width,
82
+ height: @height,
83
+ }
84
+
85
+ # Specific size
86
+ unless @attributes[:width].nil? && @attributes[:height].nil?
87
+ size[:width] = @attributes[:width].nil? ? calculate_width(@attributes[:height]) : @attributes[:width]
88
+ size[:height] = @attributes[:height].nil? ? calculate_height(@attributes[:width]) : @attributes[:height]
89
+ @width = size[:width]
90
+ @height = size[:height]
91
+ end
92
+
93
+ size
94
+ end
95
+
96
+ private def calculate_width(height)
97
+ (height.to_i * @width) / @height
98
+ end
99
+
100
+ private def calculate_height(width)
101
+ (width.to_i * @height) / @width
102
+ end
103
+
104
+ private def get_heroicon(symbol, variant)
105
+ raise ArgumentError, "Icon name can't be empty" if symbol.empty?
106
+
107
+ raise ArgumentError, "Variant `#{variant.inspect}` is invalid; must be one of #{VARIANTS.join(", ")}" unless VARIANTS.include?(variant)
62
108
 
63
- icon = HeroiconsHelper::ICON_SYMBOLS[symbol_s]
109
+ icon = HeroiconsHelper::ICON_SYMBOLS[symbol]
64
110
 
65
111
  raise ArgumentError, "Couldn't find Heroicon for `#{symbol.inspect}`" unless icon
66
112
 
67
- icon_in_variant = icon["variants"][variant_s]
68
- raise ArgumentError, "Heroicon for `#{symbol.inspect}` doesn't have variant `#{variant.inspect}`" unless icon_in_variant
113
+ icon_variant = icon["variants"][variant]
114
+ raise ArgumentError, "Heroicon for `#{symbol.inspect}` doesn't have variant `#{variant.inspect}`" unless icon_variant
69
115
 
70
116
  {
71
117
  "name" => icon["name"],
72
- "variant" => variant_s,
73
- "width" => icon_in_variant["width"],
74
- "height" => icon_in_variant["height"],
75
- "path" => icon_in_variant["path"],
118
+ "variant" => variant,
119
+ "keywords" => icon["keywords"] || [],
120
+ "width" => icon_variant["width"],
121
+ "height" => icon_variant["height"],
122
+ "path" => icon_variant["path"],
76
123
  }
77
124
  end
78
125
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HeroiconsHelper
4
- VERSION = "0.2.2"
4
+ VERSION = "0.4.1"
5
5
  end
@@ -9,7 +9,7 @@ module HeroiconsHelper
9
9
  file_data = File.read(File.join(File.dirname(__FILE__), "./heroicons_helper/data.json"))
10
10
  ICON_SYMBOLS = JSON.parse(file_data).freeze
11
11
 
12
- def heroicons(symbol, variant, attributes: {})
12
+ def heroicon(symbol, variant:, **attributes)
13
13
  ::HeroiconsHelper::Icon.new(symbol, variant, attributes: attributes)
14
14
  end
15
15
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: heroicons_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Garen J. Torikian
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-06-10 00:00:00.000000000 Z
11
+ date: 2022-06-11 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A package that distributes Heroicons as a gem, for easy inclusion in
14
14
  Ruby projects.