Class: CubitComponents::BaseComponent
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- CubitComponents::BaseComponent
show all
- Defined in:
- lib/cubit_components/base_component.rb
Direct Known Subclasses
BoxComponent, ContainerComponent, FlexComponent, GridComponent, HStackComponent, PageComponent, PageContainerComponent, PlaceholderComponent, SpreadComponent, StackComponent, VStackComponent
Constant Summary
collapse
- STYLE_CONFIG =
StyleConfig.new
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(tokens: [], as: "div") ⇒ BaseComponent
Returns a new instance of BaseComponent.
32
33
34
35
36
37
|
# File 'lib/cubit_components/base_component.rb', line 32
def initialize(tokens: [], as: "div")
@tokens = Array(tokens).flat_map { |t|
t.is_a?(String) ? t.split.map(&:to_sym) : t
}
@tag_name = as
end
|
Instance Attribute Details
#tag_name ⇒ Object
Returns the value of attribute tag_name.
39
40
41
|
# File 'lib/cubit_components/base_component.rb', line 39
def tag_name
@tag_name
end
|
Class Method Details
.inline_template(source) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/cubit_components/base_component.rb', line 20
def self.inline_template(source)
compiled = ActionView::Template::Handlers::ERB::Erubi.new(source, bufvar: "@output_buffer").src
class_eval <<~RUBY, __FILE__, __LINE__ + 1
def call
@output_buffer = ActionView::OutputBuffer.new
begin
#{compiled}
end.to_s
end
RUBY
end
|
.style(&block) ⇒ Object
10
11
12
13
14
15
16
17
18
|
# File 'lib/cubit_components/base_component.rb', line 10
def self.style(&block)
dsl = StyleDSL.new
dsl.instance_eval(&block)
config = dsl.config
const_set(:STYLE_CONFIG, config)
config.presences.each do |token|
define_method("#{token.to_s.tr("-", "_")}?") { @tokens.include?(token) }
end
end
|
Instance Method Details
#call ⇒ Object
41
42
43
|
# File 'lib/cubit_components/base_component.rb', line 41
def call
content_tag(tag_name, content, class: html_classes.map(&:to_s).join(" "))
end
|
#html_classes ⇒ Object
45
46
47
48
49
50
51
52
53
|
# File 'lib/cubit_components/base_component.rb', line 45
def html_classes
config = self.class.const_get(:STYLE_CONFIG)
expanded = @tokens.flat_map { |t| expand_token(config, t) }
kept_defaults = config.defaults
.reject { |d| d.replaced_by && expanded.any? { |t| match?(d.replaced_by, t) } }
.map(&:token)
visible = expanded.reject { |t| config.presences.include?(t) }
(kept_defaults + visible).uniq
end
|