heroicons_helper 0.7.1 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,36 +6,37 @@ require "active_support/core_ext/string/output_safety"
6
6
  module HeroiconsHelper
7
7
  # Icon to show heroicons by name and variant.
8
8
  class Icon
9
- attr_reader :path, :attributes, :width, :height, :name, :variant, :keywords
9
+ attr_reader :inner, :attributes, :width, :height, :name, :variant, :keywords
10
10
 
11
11
  VARIANT_OUTLINE = "outline"
12
12
  VARIANT_SOLID = "solid"
13
13
  VARIANT_MINI = "mini"
14
- VALID_VARIANTS = Set.new([VARIANT_OUTLINE, VARIANT_SOLID, VARIANT_MINI]).freeze
14
+ VARIANT_MICRO = "micro"
15
+ VALID_VARIANTS = Set.new([VARIANT_OUTLINE, VARIANT_SOLID, VARIANT_MINI, VARIANT_MICRO]).freeze
15
16
 
16
- def initialize(name, variant, unsafe: false, attributes: {})
17
+ def initialize(name, variant, size: nil, attributes: {})
17
18
  @name = name.to_s
18
19
  @variant = variant.to_s
19
- @unsafe = unsafe
20
20
 
21
21
  heroicon = get_heroicon(@name, @variant)
22
22
 
23
- @path = safe? ? ActiveSupport::SafeBuffer.new(heroicon["path"]) : heroicon["path"]
24
- @width = heroicon["width"]
25
- @height = heroicon["height"]
23
+ @inner = heroicon["inner"]
24
+ @width = size || heroicon["width"]
25
+ @height = size || heroicon["height"]
26
26
  @keywords = heroicon["keywords"]
27
- @attributes = attributes.dup.compact
28
- @attributes[:class] = classes
29
- @attributes[:viewBox] = viewbox
30
- @attributes.merge!(size)
31
- @attributes[:version] = "1.1"
32
- @attributes.merge!(variant_attributes)
33
- @attributes.merge!(a11y)
27
+ @attributes = attributes.merge(heroicon["attributes"])
28
+ @attributes.merge!({
29
+ "class" => classes,
30
+ "viewBox" => viewbox,
31
+ "version" => "1.1",
32
+ })
33
+ .merge!(a11y)
34
+ .compact!
34
35
  end
35
36
 
36
37
  # Returns an string representing a <svg> tag
37
38
  def to_svg
38
- "<!-- Heroicon name: #{@variant}/#{@name} --><svg xmlns=\"http://www.w3.org/2000/svg\" #{html_attributes}>#{@path}</svg>"
39
+ "<svg #{html_attributes}>#{@inner}</svg>"
39
40
  end
40
41
 
41
42
  private def safe?
@@ -53,9 +54,9 @@ module HeroiconsHelper
53
54
  accessible = {}
54
55
 
55
56
  if @attributes[:"aria-label"].nil? && @attributes["aria-label"].nil?
56
- accessible[:"aria-hidden"] = "true"
57
+ accessible["aria-hidden"] = "true"
57
58
  else
58
- accessible[:role] = "img"
59
+ accessible["role"] = "img"
59
60
  end
60
61
 
61
62
  accessible
@@ -63,44 +64,13 @@ module HeroiconsHelper
63
64
 
64
65
  # prepare the heroicon class
65
66
  private def classes
66
- "heroicon heroicon-#{@name}-#{@variant} #{@attributes[:class]} ".strip
67
- end
68
-
69
- private def variant_attributes
70
- case @variant
71
- when VARIANT_OUTLINE
72
- {
73
- fill: "none",
74
- }
75
- when VARIANT_SOLID, VARIANT_MINI
76
- {
77
- fill: "currentColor",
78
- }
79
- end
67
+ "heroicon heroicon-#{@variant}-#{@name} #{@attributes[:class]} ".strip
80
68
  end
81
69
 
82
70
  private def viewbox
83
71
  "0 0 #{@width} #{@height}"
84
72
  end
85
73
 
86
- # determine the height and width of the heroicon based on :size option
87
- private def size
88
- size = {
89
- width: @width,
90
- height: @height,
91
- }
92
-
93
- # Specific size
94
- unless @attributes[:width].nil? && @attributes[:height].nil?
95
- size[:width] = @attributes[:width].nil? ? calculate_width(@attributes[:height]) : @attributes[:width]
96
- size[:height] = @attributes[:height].nil? ? calculate_height(@attributes[:width]) : @attributes[:height]
97
- @width = size[:width]
98
- @height = size[:height]
99
- end
100
-
101
- size
102
- end
103
-
104
74
  private def calculate_width(height)
105
75
  (height.to_i * @width) / @height
106
76
  end
@@ -114,7 +84,7 @@ module HeroiconsHelper
114
84
 
115
85
  raise ArgumentError, "Variant `#{variant.inspect}` is invalid; must be one of #{VALID_VARIANTS.join(", ")}" unless VALID_VARIANTS.include?(variant)
116
86
 
117
- icon = HeroiconsHelper::ICON_NAMES[name]
87
+ icon = HeroiconsHelper::ICONS[name]
118
88
 
119
89
  raise ArgumentError, "Couldn't find Heroicon for `#{name.inspect}`" unless icon
120
90
 
@@ -127,7 +97,8 @@ module HeroiconsHelper
127
97
  "keywords" => icon["keywords"] || [],
128
98
  "width" => icon_variant["width"],
129
99
  "height" => icon_variant["height"],
130
- "path" => icon_variant["path"],
100
+ "attributes" => icon_variant["attributes"],
101
+ "inner" => icon_variant["inner"],
131
102
  }
132
103
  end
133
104
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HeroiconsHelper
4
- VERSION = "0.7.1"
4
+ VERSION = "0.9.0"
5
5
  end
@@ -7,20 +7,20 @@ require "json"
7
7
 
8
8
  module HeroiconsHelper
9
9
  file_data = File.read(File.join(File.dirname(__FILE__), "./heroicons_helper/data.json"))
10
- ICON_NAMES = JSON.parse(file_data).freeze
10
+ ICONS = JSON.parse(file_data).freeze
11
11
 
12
- def heroicon(name, variant:, unsafe: false, **attributes)
12
+ def heroicon(name, variant:, size: nil, **attributes)
13
13
  cache_key = HeroiconsHelper::Cache.get_key(
14
14
  name: name,
15
15
  variant: variant,
16
- unsafe: unsafe,
16
+ size: size,
17
17
  attributes: attributes,
18
18
  )
19
19
 
20
20
  cached_heroicon = HeroiconsHelper::Cache.read(cache_key)
21
21
  return cached_heroicon unless cached_heroicon.nil?
22
22
 
23
- heroicon = ::HeroiconsHelper::Icon.new(name, variant, unsafe: unsafe, attributes: attributes)
23
+ heroicon = ::HeroiconsHelper::Icon.new(name, variant, size: size, attributes: attributes)
24
24
  HeroiconsHelper::Cache.set(cache_key, heroicon)
25
25
 
26
26
  heroicon