Sha256: 3f6c86aa656e80c0657010d9a858b13c5a4ed87ccb04310fe47bc37a6423a656

Contents?: true

Size: 1.15 KB

Versions: 20

Compression:

Stored size: 1.15 KB

Contents

module Lookbook
  module ComponentHelper
    def render_component(name, **attrs, &block)
      attrs[:classes] = class_names(attrs[:class])
      render "lookbook/components/#{name.underscore}", **attrs.except(:class), &block
    end

    def icon(name, size: 4, **attrs)
      render_component "icon", name: name, size: size, **attrs
    end

    def code(language = "ruby", **opts, &block)
      render_component "code", language: language, **opts, &block
    end

    if Rails.version.to_f < 6.1
      def class_names(*args)
        tokens = build_tag_values(*args).flat_map { |value| value.to_s.split(/\s+/) }.uniq
        safe_join(tokens, " ")
      end
    end

    alias_method :component, :render_component

    private

    def build_tag_values(*args)
      tag_values = []
      args.each do |tag_value|
        case tag_value
        when Hash
          tag_value.each do |key, val|
            tag_values << key.to_s if val && key.present?
          end
        when Array
          tag_values.concat build_tag_values(*tag_value)
        else
          tag_values << tag_value.to_s if tag_value.present?
        end
      end
      tag_values
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
lookbook-0.9.8 app/helpers/lookbook/component_helper.rb
lookbook-0.9.7 app/helpers/lookbook/component_helper.rb
lookbook-0.9.6 app/helpers/lookbook/component_helper.rb
lookbook-0.9.5 app/helpers/lookbook/component_helper.rb
lookbook-0.9.4 app/helpers/lookbook/component_helper.rb
lookbook-0.9.3 app/helpers/lookbook/component_helper.rb
lookbook-0.9.2 app/helpers/lookbook/component_helper.rb
lookbook-0.9.1 app/helpers/lookbook/component_helper.rb
lookbook-0.9.0 app/helpers/lookbook/component_helper.rb
lookbook-0.8.3 app/helpers/lookbook/component_helper.rb
lookbook-0.8.2 app/helpers/lookbook/component_helper.rb
lookbook-0.8.1 app/helpers/lookbook/component_helper.rb
lookbook-0.8.0 app/helpers/lookbook/component_helper.rb
lookbook-0.8.0.beta.0 app/helpers/lookbook/component_helper.rb
lookbook-0.7.4 app/helpers/lookbook/component_helper.rb
lookbook-0.7.3 app/helpers/lookbook/component_helper.rb
lookbook-0.7.2 app/helpers/lookbook/component_helper.rb
lookbook-0.7.2.beta.5 app/helpers/lookbook/component_helper.rb
lookbook-0.7.2.beta.4 app/helpers/lookbook/component_helper.rb
lookbook-0.7.2.beta.3 app/helpers/lookbook/component_helper.rb