app/helpers/lookbook/component_helper.rb in lookbook-0.9.8 vs app/helpers/lookbook/component_helper.rb in lookbook-1.0.0.beta.0

- old
+ new

@@ -1,29 +1,41 @@ 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 + COMPONENT_CLASSES = {} # cache for constantized references - def icon(name, size: 4, **attrs) - render_component "icon", name: name, size: size, **attrs + def render_component(ref, **attrs, &block) + klass = component_class(ref) + comp = attrs.key?(:content) ? klass.new(**attrs.except(:content)).with_content(attrs[:content]) : klass.new(**attrs) + render comp, &block end - def code(language = "ruby", **opts, &block) - render_component "code", language: language, **opts, &block + def render_tag(tag = :div, **attrs, &block) + render Lookbook::TagComponent.new(tag: tag, **attrs), &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 component_class(ref) + klass = COMPONENT_CLASSES[ref] + if klass.nil? + ref = ref.to_s.tr("-", "_") + class_namespace = ref.camelize + begin + klass = "Lookbook::#{class_namespace}::Component".constantize + rescue + klass = "Lookbook::#{class_namespace}Component".constantize + end + COMPONENT_CLASSES[ref] = klass + end + klass + end def build_tag_values(*args) tag_values = [] args.each do |tag_value| case tag_value