Sha256: 64d4bdbc0e8d8411db39fa49cb6f878d65b4d9034f0f946d95b53ee13b129e20

Contents?: true

Size: 842 Bytes

Versions: 11

Compression:

Stored size: 842 Bytes

Contents

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
coveragebook_components-0.6.2 app/components/coco/base/icon/icon.rb
coveragebook_components-0.6.1 app/components/coco/base/icon/icon.rb
coveragebook_components-0.6.0 app/components/coco/base/icon/icon.rb
coveragebook_components-0.5.7 app/components/coco/base/icon/icon.rb
coveragebook_components-0.5.6 app/components/coco/base/icon/icon.rb
coveragebook_components-0.5.5 app/components/coco/base/icon/icon.rb
coveragebook_components-0.5.4 app/components/coco/base/icon/icon.rb
coveragebook_components-0.5.3 app/components/coco/base/icon/icon.rb
coveragebook_components-0.5.2 app/components/coco/base/icon/icon.rb
coveragebook_components-0.5.1 app/components/coco/base/icon/icon.rb
coveragebook_components-0.5.0 app/components/coco/base/icon/icon.rb