module Coco class Icon < Coco::Component include Concerns::AcceptsOptions ICON_CACHE = {} InvalidIconError = Class.new(StandardError) accepts_option :size, from: %i[sm md lg xl xxl] accepts_option :spin, from: [true, false] accepts_option :style, from: [:line, :fill] 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 end def read_svg File.read(svg_path).html_safe rescue if 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") end end end