Sha256: 15e8d807546ddc833e4a4e88258e6c998c74c2fefcd507edfd2b5aa17ff88f2b

Contents?: true

Size: 1.64 KB

Versions: 120

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

require "octicons"

module Primer
  module Alpha
    # OcticonSymbols renders a symbol dictionary using a list of <%= link_to_octicons %>.
    class OcticonSymbols < Primer::Component
      # @param icons [Array<Hash>] List of icons to render, in the format { symbol: :icon_name, size: :small }
      def initialize(icons: [])
        @icons = {}
        icons.each do |icon|
          symbol = icon[:symbol]
          size = Primer::Beta::Octicon::SIZE_MAPPINGS[
            fetch_or_fallback(Primer::Beta::Octicon::SIZE_OPTIONS, icon[:size] || Primer::Beta::Octicon::SIZE_DEFAULT, Primer::Beta::Octicon::SIZE_DEFAULT)
          ]

          cache_key = Primer::Octicon::Cache.get_key(symbol: symbol, size: size)

          if (cache_icon = Primer::Octicon::Cache.read(cache_key))
            icon_instance = cache_icon
          else
            icon_instance = Octicons::Octicon.new(symbol, height: size)

            Primer::Octicon::Cache.set(cache_key, icon_instance)
          end

          # Don't put the same icon twice
          @icons[[symbol, icon_instance.height]] = icon_instance if @icons[[symbol, icon_instance.height]].nil?
        end
      end

      def render?
        @icons.any?
      end

      def symbol_tags
        safe_join(
          @icons.values.map do |icon|
            content_tag(
              :symbol,
              icon.path.html_safe, # rubocop:disable Rails/OutputSafety
              id: "octicon_#{icon.symbol}_#{icon.height}",
              viewBox: icon.options[:viewBox],
              width: icon.width,
              height: icon.height
            )
          end
        )
      end
    end
  end
end

Version data entries

120 entries across 120 versions & 2 rubygems

Version Path
openproject-primer_view_components-0.15.0 app/components/primer/alpha/octicon_symbols.rb
openproject-primer_view_components-0.14.0 app/components/primer/alpha/octicon_symbols.rb
openproject-primer_view_components-0.13.1 app/components/primer/alpha/octicon_symbols.rb
primer_view_components-0.13.2 app/components/primer/alpha/octicon_symbols.rb
primer_view_components-0.13.1 app/components/primer/alpha/octicon_symbols.rb
openproject-primer_view_components-0.13.0 app/components/primer/alpha/octicon_symbols.rb
primer_view_components-0.13.0 app/components/primer/alpha/octicon_symbols.rb
primer_view_components-0.12.0 app/components/primer/alpha/octicon_symbols.rb
primer_view_components-0.11.0 app/components/primer/alpha/octicon_symbols.rb
openproject-primer_view_components-0.12.1 app/components/primer/alpha/octicon_symbols.rb
primer_view_components-0.10.0 app/components/primer/alpha/octicon_symbols.rb
openproject-primer_view_components-0.11.0 app/components/primer/alpha/octicon_symbols.rb
openproject-primer_view_components-0.10.0 app/components/primer/alpha/octicon_symbols.rb
primer_view_components-0.9.0 app/components/primer/alpha/octicon_symbols.rb
openproject-primer_view_components-0.9.1 app/components/primer/alpha/octicon_symbols.rb
openproject-primer_view_components-0.9.0 app/components/primer/alpha/octicon_symbols.rb
openproject-primer_view_components-0.8.0 app/components/primer/alpha/octicon_symbols.rb
primer_view_components-0.8.0 app/components/primer/alpha/octicon_symbols.rb
primer_view_components-0.7.0 app/components/primer/alpha/octicon_symbols.rb
openproject-primer_view_components-0.7.0 app/components/primer/alpha/octicon_symbols.rb