Sha256: 81599cd960a0f0ad41ccc2cec9a8caf836100948212b1e854b78274a772ed30a

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

require "webpacker/react/railtie" if defined?(Rails)
require "webpacker/react/helpers"
require "webpacker/react/component"

module Playbook
  module PbKitHelper

    def pb_rails(kit, props: {}, &block)
      previous = prefix_partial_path_with_controller_namespace
      self.prefix_partial_path_with_controller_namespace = false
      kit = build_view_model(kit.to_s, dark_mode_props(props), &block)
      render(partial: kit, as: :object)
    ensure
      self.prefix_partial_path_with_controller_namespace = previous
    end

    def pb_react(kit, props:{dark: dark_mode}, options: {})
      ::Webpacker::React::Component.new(kit.camelize).render(props, options)
    end

  private
    def rails_props
      if ENV["dark_mode"].is_set?
        dark_mode_props
      else
        props
      end
    end  

    def react_props
      if ENV["dark_mode"].is_set?
        dark_mode
      else
        false
      end
    end 

    def is_subkit?(kit)
      kit.match(%r{[/\\]})
    end

    def build_view_model(kit, props, &block)
      folder = is_subkit?(kit) ? kit.split("/")[0] : kit
      item = is_subkit?(kit) ? kit.split("/")[-1] : kit
      class_name = "Playbook::Pb#{folder.camelize}::#{item.camelize}"
      class_name.safe_constantize.new(props, &block)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
playbook_ui-7.0.1.pre.alpha3 app/helpers/playbook/pb_kit_helper.rb