Sha256: 9bcae2e4e19cd9ce379ba372ec3d0206929d1e9e1e5e9c0881977f93cce1e511

Contents?: true

Size: 1.25 KB

Versions: 15

Compression:

Stored size: 1.25 KB

Contents

module Preact
  def self.render_to_string(native_preact_element)
    `Opal.global.Preact.renderToString(native_preact_element)`
  end
end

module Isomorfeus
  class TopLevel
    class << self
      attr_accessor :ssr_route_path
      attr_accessor :transport_ws_url

      def render_component_to_string(component_name, props)
        component = nil
        %x{
          if (typeof component_name === 'string' || component_name instanceof String) {
            component = component_name.split(".").reduce(function(o, x) {
              return (o !== null && typeof o[x] !== "undefined" && o[x] !== null) ? o[x] : null;
            }, Opal.global)
          } else {
            component = component_name;
          }
        }
        component = cached_component_class(component_name) unless component
        Preact.render_to_string(Preact.create_element(component, `Opal.Hash.$new(props)`))
      end

      def cached_component_classes
        @cached_component_classes ||= {}
      end

      def cached_component_class(class_name)
        return cached_component_classes[class_name] if cached_component_classes.key?(class_name)
        cached_component_classes[class_name] = "::#{class_name}".constantize
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
isomorfeus-preact-10.6.60 lib/isomorfeus/preact/ssr/top_level.rb
isomorfeus-preact-10.6.59 lib/isomorfeus/preact/ssr/top_level.rb
isomorfeus-preact-10.6.58 lib/isomorfeus/preact/ssr/top_level.rb
isomorfeus-preact-10.6.57 lib/isomorfeus/preact/ssr/top_level.rb
isomorfeus-preact-10.6.56 lib/isomorfeus/preact/ssr/top_level.rb
isomorfeus-preact-10.6.55 lib/isomorfeus/preact/ssr/top_level.rb
isomorfeus-preact-10.6.54 lib/isomorfeus/preact/ssr/top_level.rb
isomorfeus-preact-10.6.53 lib/isomorfeus/preact/ssr/top_level.rb
isomorfeus-preact-10.6.52 lib/isomorfeus/preact/ssr/top_level.rb
isomorfeus-preact-10.6.51 lib/isomorfeus/preact/ssr/top_level.rb
isomorfeus-preact-10.6.50 lib/isomorfeus/top_level_ssr.rb
isomorfeus-preact-10.6.49 lib/isomorfeus/top_level_ssr.rb
isomorfeus-preact-10.6.48 lib/isomorfeus/top_level_ssr.rb
isomorfeus-preact-10.6.47 lib/isomorfeus/top_level_ssr.rb
isomorfeus-preact-10.6.46 lib/isomorfeus/top_level_ssr.rb