Class: RSX::Children
- Inherits:
-
Object
- Object
- RSX::Children
- 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
- #==(other) ⇒ Object
-
#call(*arguments, **options, &block) ⇒ Object
Render props: childrenchildren.call(item) passes a value back to the caller.
- #empty? ⇒ Boolean (also: #blank?)
- #html_safe? ⇒ Boolean
-
#initialize(&block) ⇒ Children
constructor
A new instance of Children.
- #inspect ⇒ Object
- #length ⇒ Object
- #present? ⇒ Boolean (also: #any?)
-
#render ⇒ Object
(also: #to_rsx, #to_safe_string)
The rendered markup.
- #to_s ⇒ Object
- #to_str ⇒ Object
-
#value ⇒ Object
The value the children expression evaluated to, before coercion.
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, **, &block) raw = value return raw.call(*arguments, **, &block) if raw.respond_to?(:call) raw end |
#empty? ⇒ Boolean Also known as: blank?
56 57 58 |
# File 'lib/rsx/children.rb', line 56 def empty? render.empty? end |
#html_safe? ⇒ Boolean
44 45 46 |
# File 'lib/rsx/children.rb', line 44 def html_safe? true end |
#inspect ⇒ Object
74 75 76 |
# File 'lib/rsx/children.rb', line 74 def inspect "#<RSX::Children #{@render ? @render.inspect : "(not rendered)"}>" end |
#length ⇒ Object
66 67 68 |
# File 'lib/rsx/children.rb', line 66 def length render.length end |
#present? ⇒ Boolean Also known as: any?
61 62 63 |
# File 'lib/rsx/children.rb', line 61 def present? !empty? end |
#render ⇒ Object 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_s ⇒ Object
36 37 38 |
# File 'lib/rsx/children.rb', line 36 def to_s render.to_s end |
#to_str ⇒ Object
40 41 42 |
# File 'lib/rsx/children.rb', line 40 def to_str render.to_s end |
#value ⇒ Object
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 |