app/components/coco/base/icon/icon.rb in coveragebook_components-0.6.2 vs app/components/coco/base/icon/icon.rb in coveragebook_components-0.6.3
- old
+ new
@@ -4,35 +4,58 @@
ICON_CACHE = {}
InvalidIconError = Class.new(StandardError)
- accepts_option :size, from: %i[sm md lg xl xxl]
+ accepts_option :size, from: %i[xs sm md lg xl xxl]
accepts_option :spin, from: [true, false]
- accepts_option :style, from: [:line, :fill]
+ accepts_option :style, from: [:line, :fill, :custom]
+ before_render do
+ if name
+ set_tag_data_attr(:icon, name)
+ set_option_value(:style, :custom) if icon_data[:custom]
+ end
+ end
+
def initialize(name: nil, **kwargs)
@icon_name = name&.to_s&.tr("_", "-")
- set_tag_data_attr(:icon, name) if name
end
def name
@icon_name
end
def svg
- ICON_CACHE[name] ||= read_svg
+ content || icon_data[:svg]
end
+ def icon_data
+ ICON_CACHE[name] ||= {
+ custom: custom?,
+ svg: read_svg
+ }
+ end
+
+ def custom?
+ @_custom ||= File.exist?(custom_icon_path)
+ end
+
def read_svg
- File.read(svg_path).html_safe
- rescue
- if Rails.env.development? || Rails.env.test?
+ if custom?
+ File.read(custom_icon_path).html_safe
+ elsif File.exist?(icon_path)
+ File.read(icon_path).html_safe
+ elsif Rails.env.development? || Rails.env.test?
raise InvalidIconError, "`#{name}` is not a valid icon name"
end
end
- def svg_path
- Coco::Engine.root.join("app/assets/build/coco/icons/#{name}.svg")
+ def icon_path
+ @_icon_path ||= Coco::Engine.root.join("app/assets/build/coco/icons/#{name}.svg")
+ end
+
+ def custom_icon_path
+ @_custom_icon_path ||= Coco::Engine.root.join("app/assets/build/coco/icons/custom/#{name}.svg")
end
end
end