Class: BulmaX::BaseComponent

Inherits:
Phlex::HTML
  • Object
show all
Includes:
ComponentDsl, Shared::AriaOptions, Shared::DataOptions, Shared::FlexOptions, Shared::GlobalOptions, Shared::SpacingOptions, Shared::TextOptions
Defined in:
lib/bulma_x/base_component.rb

Constant Summary collapse

BOOLEAN =
[true, false].freeze
COLORS =
%w[primary secondary link info success warning danger text].freeze
COLOR_MODIFIER =
%w[light dark soft bold on-scheme].freeze
COLOR_PALETTE =
(0..100).step(5).to_a.map { Kernel.format('%<value>02d', value: _1) }.freeze
MODIFIERS_PALETTE =
COLORS +
COLORS
.product(COLOR_MODIFIER).map { _1.join('-') } +
COLORS
.product(COLOR_PALETTE).map { _1.join('-') } +
COLORS
.product(COLOR_MODIFIER, COLOR_PALETTE)
.map { _1.join('-') } +
[nil] +
COLORS + %w[light dark white black]
MODIFIERS_DECLINED =
COLORS + COLORS.product(COLOR_MODIFIER).map { _1.join('-') } + [nil] + %w[light dark]
MODIFIERS =
(COLORS + [nil])
BLANK_VALUES =
[nil, false, '', [], {}].freeze

Constants included from Shared::SpacingOptions

Shared::SpacingOptions::VALID_SPACING_KEYS, Shared::SpacingOptions::VALID_SPACING_VALUES

Instance Method Summary collapse

Methods included from Shared::AriaOptions

#aria_attributes, included

Methods included from Shared::GlobalOptions

#global_attributes, #global_classes, included

Methods included from Shared::DataOptions

#data_attributes, included

Methods included from Shared::FlexOptions

#flex_classes, included

Methods included from Shared::SpacingOptions

included, #spacing_classes

Methods included from Shared::TextOptions

included, #text_classes

Methods included from ComponentDsl

included

Constructor Details

#initializeBaseComponent

Returns a new instance of BaseComponent.



46
47
48
49
50
51
# File 'lib/bulma_x/base_component.rb', line 46

def initialize(**, &)
  super(&)

  build_from_options(**)
  validate!(**)
end

Instance Method Details

#after_templateObject



119
120
121
122
# File 'lib/bulma_x/base_component.rb', line 119

def after_template
  super
  comment { "</#{self.class.name}>".html_safe }
end

#attributes(**kwargs) ⇒ Object

NOTE: attributes will remove all pairs having a falsy value, including false bool. Use “false” string to keep it.



96
97
98
# File 'lib/bulma_x/base_component.rb', line 96

def attributes(**kwargs)
  compact_blank(kwargs)
end

#base_attributesObject



80
81
82
83
84
# File 'lib/bulma_x/base_component.rb', line 80

def base_attributes
  %i[data_attributes aria_attributes global_attributes].each_with_object({}) do |method, acc|
    acc.merge!(public_send(method)) if respond_to?(method)
  end
end

#base_classesObject



71
72
73
74
75
76
77
78
# File 'lib/bulma_x/base_component.rb', line 71

def base_classes
  [
    respond_to?(:spacing_classes) && spacing_classes,
    respond_to?(:text_classes) && text_classes,
    respond_to?(:flex_classes) && flex_classes,
    respond_to?(:global_classes) && global_classes
  ].flatten
end

#before_templateObject



114
115
116
117
# File 'lib/bulma_x/base_component.rb', line 114

def before_template
  comment { "<#{self.class.name}>".html_safe }
  super
end

#classes(*args) ⇒ Object

NOTE: classes will remove all pairs having a falsy value, including false bool. Use “false” string to keep it.



87
88
89
90
91
92
93
# File 'lib/bulma_x/base_component.rb', line 87

def classes(*args)
  compact_args = compact_blank(args.flatten)

  return {} if compact_args.empty?

  { class: compact_args.join(' ') }
end

#compact_blank(obj) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/bulma_x/base_component.rb', line 102

def compact_blank(obj)
  case obj
  when Array
    obj.reject { BLANK_VALUES.include?(it) }
  when Hash
    obj.reject { |_, v| BLANK_VALUES.include?(v) }
  else
    obj
  end
end

#css(str) ⇒ Object



100
# File 'lib/bulma_x/base_component.rb', line 100

def css(str) = str.to_s.tr('_', '-')

#render_rootObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bulma_x/base_component.rb', line 55

def render_root(&)
  public_send(
    root_tag,
    **classes(root_classes),
    **attributes(**root_attributes)
  ) do
    # NOTE: Returning nil in the block may return the generated HTML in some engines
    # For instance in slim :
    # = render BulmaX::Block.new do
    #   = 'void' if false
    # Will render a block which content is the HTML of the BulmaX::Block
    # To avoid this, capturing ensure we always return the same thing
    capture(&) if block_given?
  end
end

#view_templateObject



53
# File 'lib/bulma_x/base_component.rb', line 53

def view_template(&) = render_root(&)