Module: Preact

Defined in:
lib/isomorfeus-preact.rb,
lib/preact.rb,
lib/preact/ref.rb,
lib/preact/props.rb,
lib/preact/state.rb,
lib/preact/params.rb,
lib/preact/version.rb,
lib/preact/option_hooks.rb,
lib/preact/context_wrapper.rb,
lib/preact/native_constant_wrapper.rb,
lib/isomorfeus/preact/ssr/top_level.rb

Overview

Preact::Component make modules available

Defined Under Namespace

Modules: Component, ComponentResolution, Elements, FunctionComponent Classes: ContextWrapper, NativeConstantWrapper, OptionHooks, Params, Props, Ref, State

Constant Summary collapse

VERSION =
'10.9.0'

Class Method Summary collapse

Class Method Details

.clone_element(native_preact_element, props = nil, children = nil, &block) ⇒ Object



240
241
242
243
244
245
246
247
248
# File 'lib/preact.rb', line 240

def self.clone_element(native_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(native_preact_element, native_props, block_result)`
end

.create_context(const_name, default_value) ⇒ Object



250
251
252
253
254
255
256
257
# File 'lib/preact.rb', line 250

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) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/preact.rb', line 215

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_refObject



259
260
261
# File 'lib/preact.rb', line 259

def self.create_ref
  Preact::Ref.new(`Opal.global.Preact.createRef()`)
end

.hydrate(native_preact_element, container_node, replace_node) ⇒ Object



263
264
265
# File 'lib/preact.rb', line 263

def self.hydrate(native_preact_element, container_node, replace_node)
  `Opal.global.Preact.hydrate(native_preact_element, container_node)`
end

.location_hook(location) ⇒ Object



267
268
269
270
271
272
273
# File 'lib/preact.rb', line 267

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) ⇒ Object



275
276
277
278
279
280
281
282
# File 'lib/preact.rb', line 275

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) ⇒ Object



2
3
4
# File 'lib/isomorfeus/preact/ssr/top_level.rb', line 2

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

.to_child_array(props_children) ⇒ Object



236
237
238
# File 'lib/preact.rb', line 236

def self.to_child_array(props_children)
  `Opal.global.Preact.toChildArray(children)`
end

.unmount_component_at_node(element_or_query) ⇒ Object

render_to_string is in top_level_ssr.rb



286
287
288
289
290
291
292
293
294
295
# File 'lib/preact.rb', line 286

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