Class: React::Component::Styles

Inherits:
Object
  • Object
show all
Defined in:
lib/react/component/styles.rb

Instance Method Summary collapse

Constructor Details

#initialize(native, props_prop = false) ⇒ Styles

Returns a new instance of Styles.



5
6
7
8
# File 'lib/react/component/styles.rb', line 5

def initialize(native, props_prop = false)
  @native = native
  @props_prop = props_prop
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(prop, *args, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/react/component/styles.rb', line 29

def method_missing(prop, *args, &block)
  %x{
    let value;
    if (#@props_prop) {
      if (!#@native.props[#@props_prop] || typeof #@native.props[#@props_prop][prop] === 'undefined') {
        console.warn("Style/Theme key '" + prop + "' returning nil!");
        return #{nil};
      }
      value = #@native.props[#@props_prop][prop];
    } else {
      if (!#@native || typeof #@native[prop] === 'undefined') {
        console.warn("Style/Theme key '" + prop + "' returning nil!");
        return #{nil};
      }
      value = #@native[prop];
    }
    if (typeof value === 'string' || typeof value === 'number' || Array.isArray(value)) { return value; }
    if (typeof value === 'function') { return value.apply(#@native, args); }
    return Opal.React.Component.Styles.$new(value);
  }
end

Instance Method Details

#[](prop) ⇒ Object



10
11
12
# File 'lib/react/component/styles.rb', line 10

def [](prop)
  method_missing(prop)
end

#[]=(prop, val) ⇒ Object



14
15
16
# File 'lib/react/component/styles.rb', line 14

def []=(prop, val)
  method_missing(prop, val)
end

#deep_merge(a_hash) ⇒ Object



18
19
20
21
# File 'lib/react/component/styles.rb', line 18

def deep_merge(a_hash)
  native_hash = a_hash.to_n
  React::Component::Styles.new(`Opal.React.merge_deep(#@native, native_hash)`)
end

#deep_merge!(a_hash) ⇒ Object



23
24
25
26
27
# File 'lib/react/component/styles.rb', line 23

def deep_merge!(a_hash)
  native_hash = a_hash.to_n
  `#@native = Opal.React.merge_deep(#@native, native_hash)`
  self
end

#to_hObject



51
52
53
54
55
56
# File 'lib/react/component/styles.rb', line 51

def to_h
  %x{
    if (#@props_prop) { return Opal.Hash.$new(#@native.props[#@props_prop]); }
    else { return Opal.Hash.$new(#@native); }
  }
end

#to_nObject



58
59
60
61
62
63
# File 'lib/react/component/styles.rb', line 58

def to_n
  %x{
    if (#@props_prop) { return #@native.props[#@props_prop]; }
    else { return #@native; }
  }
end