module Preact

Constants

VERSION

Public Class Methods

clone_element(ruby_preact_element, props = nil, children = nil, &block) click to toggle source
# File lib/preact.rb, line 259
def self.clone_element(ruby_preact_element, props = nil, children = nil, &block)
  block_result = `null`
  if block_given?
    block_result = block.call
    block_result = `null` unless block_result
  end
  native_props = props ? `Opal.Preact.to_native_preact_props(props)` : `null`
  `Opal.global.Preact.cloneElement(ruby_preact_element.$to_n(), native_props, block_result)`
end
create_context(const_name, default_value) click to toggle source
# File lib/preact.rb, line 269
def self.create_context(const_name, default_value)
  %x{
    Opal.global[const_name] = Opal.global.Preact.createContext(default_value);
    var new_const = #{Preact::ContextWrapper.new(`Opal.global[const_name]`)};
    #{Object.const_set(const_name, `new_const`)};
    return new_const;
  }
end
create_element(type, props = nil, children = nil, &block) click to toggle source
# File lib/preact.rb, line 234
def self.create_element(type, props = nil, children = nil, &block)
  %x{
    const operabu = self.render_buffer;
    let component = null;
    let native_props = null;
    if (typeof type.preact_component !== 'undefined') { component = type.preact_component; }
    else { component = type; }
    if (block !== nil) {
      operabu.push([]);
      // console.log("create_element pushed", Opal.Preact.render_buffer, Opal.Preact.render_buffer.toString());
      let block_result = block.$call();
      if (block_result && block_result !== nil) { Opal.Preact.render_block_result(block_result); }
      // console.log("create_element popping", Opal.Preact.render_buffer, Opal.Preact.render_buffer.toString());
      children = operabu.pop();
    } else if (children === nil) { children = []; }
    else if (typeof children === 'string') { children = [children]; }
    if (props && props !== nil) { native_props = self.to_native_preact_props(props); }
    return Opal.global.Preact.createElement.apply(this, [component, native_props].concat(children));
  }
end
create_ref() click to toggle source
# File lib/preact.rb, line 278
def self.create_ref
  Preact::Ref.new(`Opal.global.Preact.createRef()`)
end
hydrate(native_preact_element, container_node, replace_node) click to toggle source
# File lib/preact.rb, line 282
def self.hydrate(native_preact_element, container_node, replace_node)
  `Opal.global.Preact.hydrate(native_preact_element, container_node)`
end
location_hook(location) click to toggle source
# File lib/preact.rb, line 286
def self.location_hook(location)
  %x{
    if (Opal.global.locationHook) { return Opal.global.locationHook; }
    else if (Opal.global.staticLocationHook) { return Opal.global.staticLocationHook(location); }
    else { #{raise "Wouter Location Hooks not imported!"}; }
  }
end
render(native_preact_element, container_node, replace_node) click to toggle source
# File lib/preact.rb, line 294
def self.render(native_preact_element, container_node, replace_node)
  # container is a native DOM element
  if block_given?
    `Opal.global.Preact.render(native_preact_element, container_node, function() { block.$call() })`
  else
    `Opal.global.Preact.render(native_preact_element, container_node)`
  end
end
render_to_string(native_preact_element) click to toggle source
# File lib/preact.rb, line 304
def self.render_to_string(native_preact_element)
  `Opal.global.Preact.renderToString(native_preact_element)`
end
to_child_array(props_children) click to toggle source
# File lib/preact.rb, line 255
def self.to_child_array(props_children)
  `Opal.global.Preact.toChildArray(children)`
end
unmount_component_at_node(element_or_query) click to toggle source
# File lib/preact.rb, line 309
def self.unmount_component_at_node(element_or_query)
  if `(typeof element_or_query === 'string')` || (`(typeof element_or_query.$class === 'function')` && element_or_query.class == String)
    element = `document.body.querySelector(element_or_query)`
  elsif `(typeof element_or_query.$is_a === 'function')` && element_or_query.is_a?(Browser::Element)
    element = element_or_query.to_n
  else
    element = element_or_query
  end
  `Opal.global.Preact.render(null, element)`
end