Class: RSX::Children

Inherits:
Object
  • Object
show all
Defined in:
lib/rsx/children.rb

Overview

The children prop of a component element.

Children compile to a block rather than a finished string, so they are rendered inside the component that received them. That is what lets <Theme.Provider> change what its children see, lets a caching component skip building them at all, and means a component that never renders children never pays for them.

The block's value is memoized, and kept in its raw form as well, so React's render-prop pattern works: { ... }.

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Children

Returns a new instance of Children.



18
19
20
21
# File 'lib/rsx/children.rb', line 18

def initialize(&block)
  @block = block
  @value = UNSET
end

Instance Method Details

#==(other) ⇒ Object



70
71
72
# File 'lib/rsx/children.rb', line 70

def ==(other)
  to_s == other.to_s
end

#call(*arguments, **options, &block) ⇒ Object

Render props: RSX::Children.childrenchildren.call(item) passes a value back to the caller.



49
50
51
52
53
54
# File 'lib/rsx/children.rb', line 49

def call(*arguments, **options, &block)
  raw = value
  return raw.call(*arguments, **options, &block) if raw.respond_to?(:call)

  raw
end

#empty?Boolean Also known as: blank?

Returns:

  • (Boolean)


56
57
58
# File 'lib/rsx/children.rb', line 56

def empty?
  render.empty?
end

#html_safe?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/rsx/children.rb', line 44

def html_safe?
  true
end

#inspectObject



74
75
76
# File 'lib/rsx/children.rb', line 74

def inspect
  "#<RSX::Children #{@render ? @render.inspect : "(not rendered)"}>"
end

#lengthObject



66
67
68
# File 'lib/rsx/children.rb', line 66

def length
  render.length
end

#present?Boolean Also known as: any?

Returns:

  • (Boolean)


61
62
63
# File 'lib/rsx/children.rb', line 61

def present?
  !empty?
end

#renderObject Also known as: to_rsx, to_safe_string

The rendered markup. Used whenever children are interpolated as children.



30
31
32
# File 'lib/rsx/children.rb', line 30

def render
  @render ||= RSX.child(value)
end

#to_sObject



36
37
38
# File 'lib/rsx/children.rb', line 36

def to_s
  render.to_s
end

#to_strObject



40
41
42
# File 'lib/rsx/children.rb', line 40

def to_str
  render.to_s
end

#valueObject

The value the children expression evaluated to, before coercion.



24
25
26
27
# File 'lib/rsx/children.rb', line 24

def value
  @value = @block.call if @value.equal?(UNSET)
  @value
end