heroicons_helper 0.7.0 → 0.8.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,45 +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
- stroke: "currentColor",
75
- }
76
- when VARIANT_SOLID, VARIANT_MINI
77
- {
78
- fill: "currentColor",
79
- }
80
- end
67
+ "heroicon heroicon-#{@variant}-#{@name} #{@attributes[:class]} ".strip
81
68
  end
82
69
 
83
70
  private def viewbox
84
71
  "0 0 #{@width} #{@height}"
85
72
  end
86
73
 
87
- # determine the height and width of the heroicon based on :size option
88
- private def size
89
- size = {
90
- width: @width,
91
- height: @height,
92
- }
93
-
94
- # Specific size
95
- unless @attributes[:width].nil? && @attributes[:height].nil?
96
- size[:width] = @attributes[:width].nil? ? calculate_width(@attributes[:height]) : @attributes[:width]
97
- size[:height] = @attributes[:height].nil? ? calculate_height(@attributes[:width]) : @attributes[:height]
98
- @width = size[:width]
99
- @height = size[:height]
100
- end
101
-
102
- size
103
- end
104
-
105
74
  private def calculate_width(height)
106
75
  (height.to_i * @width) / @height
107
76
  end
@@ -115,7 +84,7 @@ module HeroiconsHelper
115
84
 
116
85
  raise ArgumentError, "Variant `#{variant.inspect}` is invalid; must be one of #{VALID_VARIANTS.join(", ")}" unless VALID_VARIANTS.include?(variant)
117
86
 
118
- icon = HeroiconsHelper::ICON_NAMES[name]
87
+ icon = HeroiconsHelper::ICONS[name]
119
88
 
120
89
  raise ArgumentError, "Couldn't find Heroicon for `#{name.inspect}`" unless icon
121
90
 
@@ -128,7 +97,8 @@ module HeroiconsHelper
128
97
  "keywords" => icon["keywords"] || [],
129
98
  "width" => icon_variant["width"],
130
99
  "height" => icon_variant["height"],
131
- "path" => icon_variant["path"],
100
+ "attributes" => icon_variant["attributes"],
101
+ "inner" => icon_variant["inner"],
132
102
  }
133
103
  end
134
104
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HeroiconsHelper
4
- VERSION = "0.7.0"
4
+ VERSION = "0.8.0"
5
5
  end
@@ -7,21 +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,
17
- height: attributes[:height],
18
- width: attributes[:width],
16
+ size: size,
17
+ attributes: attributes,
19
18
  )
20
19
 
21
20
  cached_heroicon = HeroiconsHelper::Cache.read(cache_key)
22
21
  return cached_heroicon unless cached_heroicon.nil?
23
22
 
24
- heroicon = ::HeroiconsHelper::Icon.new(name, variant, unsafe: unsafe, attributes: attributes)
23
+ heroicon = ::HeroiconsHelper::Icon.new(name, variant, size: size, attributes: attributes)
25
24
  HeroiconsHelper::Cache.set(cache_key, heroicon)
26
25
 
27
26
  heroicon