Class: HakumiComponents::Skeleton::Component

Inherits:
BaseComponent
  • Object
show all
Extended by:
T::Sig
Defined in:
app/components/hakumi_components/skeleton/component.rb

Constant Summary collapse

ConfigToggle =
T.type_alias { T::Boolean }
ConfigInput =
T.type_alias { T.any(ConfigToggle, Types::HtmlAttributes) }
ConfigParam =
T.type_alias { T.any(ActionController::Parameters, Types::HtmlAttributeValue) }
ConfigValue =
T.type_alias { T.nilable(T.any(ConfigToggle, Types::HtmlAttributes)) }
AvatarInput =
T.type_alias { T.any(ConfigInput, HakumiComponents::Skeleton::AvatarConfig) }
TitleInput =
T.type_alias { T.any(ConfigInput, HakumiComponents::Skeleton::TitleConfig) }
ParagraphInput =
T.type_alias { T.any(ConfigInput, HakumiComponents::Skeleton::ParagraphConfig) }
DEFAULT_PARAGRAPH_ROWS =
T.let(3, Integer)

Constants inherited from BaseComponent

BaseComponent::ControllerOptions, BaseComponent::DateInput, BaseComponent::DateLikeValue, BaseComponent::DimensionInput, BaseComponent::HtmlPayloadInput, BaseComponent::I18nOptionValue, BaseComponent::PresenceArray, BaseComponent::PresenceScalar, BaseComponent::PresenceValue, BaseComponent::RawHtmlInput, BaseComponent::SIZES, BaseComponent::SizeValue, BaseComponent::SymbolInput

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseComponent

#append_data_token, boolean_html_param, #build_inline_style, cast_boolean, #cast_boolean, #class_names, #component_classes, #data_attributes_from, #dimension_to_css, #ensure_dom_id!, float_html_param, #generate_id, #html_classes, html_param, html_primitive_param, #html_style, #i18n_scope, integer_html_param, #merge_attributes, #render_value, #size_to_pixels, #stimulus_attrs, string_html_param, string_or_symbol_array_html_param, symbol_html_param, #t_default, #translate_with_default, #validate_inclusion!, #validate_required!, #value_present?

Constructor Details

#initialize(loading: true, active: false, avatar: false, title: true, paragraph: true, round: false, **html_options) ⇒ Component

Returns a new instance of Component.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/components/hakumi_components/skeleton/component.rb', line 49

def initialize(
  loading: true,
  active: false,
  avatar: false,
  title: true,
  paragraph: true,
  round: false,
  **html_options
)
  @loading = T.let(loading, T::Boolean)
  @active = T.let(active, T::Boolean)
  @round = T.let(round, T::Boolean)
  @avatar = T.let(HakumiComponents::Skeleton::AvatarConfig.coerce(avatar), T.nilable(HakumiComponents::Skeleton::AvatarConfig))
  @title = T.let(HakumiComponents::Skeleton::TitleConfig.coerce(title), T.nilable(HakumiComponents::Skeleton::TitleConfig))
  @paragraph = T.let(HakumiComponents::Skeleton::ParagraphConfig.coerce(paragraph), T.nilable(HakumiComponents::Skeleton::ParagraphConfig))
  @html_options = T.let(html_options, Types::HtmlAttributes)
end

Class Method Details

.extract_controller_locals(params) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/components/hakumi_components/skeleton/component.rb', line 19

def self.extract_controller_locals(params)
  loading_value = html_param(params, :loading)
  avatar_value = html_param(params, :avatar)
  title_value = html_param(params, :title)
  paragraph_value = html_param(params, :paragraph)

  locals = {
    loading: loading_value.nil? ? true : boolean_html_param(loading_value) != false,
    active: boolean_html_param(html_param(params, :active)) == true,
    round: boolean_html_param(html_param(params, :round)) == true
  }

  locals[:avatar] = normalize_config_param(avatar_value) unless avatar_value.nil?
  locals[:title] = normalize_config_param(title_value) unless title_value.nil?
  locals[:paragraph] = normalize_config_param(paragraph_value) unless paragraph_value.nil?

  locals
end

.normalize_config_param(value) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/components/hakumi_components/skeleton/component.rb', line 138

def self.normalize_config_param(value)
  return nil if value.nil?

  if value.is_a?(ActionController::Parameters) || value.is_a?(Hash)
    source = value.is_a?(ActionController::Parameters) ? value.to_unsafe_h : value
    attrs = T.let({}, Types::HtmlAttributes)
    source.each do |key, entry|
      next unless key.is_a?(String) || key.is_a?(Symbol)
      next unless entry.nil? || entry.is_a?(String) || entry.is_a?(Symbol) || entry.is_a?(TrueClass) || entry.is_a?(FalseClass) || entry.is_a?(Numeric) || entry.is_a?(Hash) || entry.is_a?(Array)

      attrs[key.to_sym] = entry
    end
    return attrs
  end

  return cast_boolean(value) if value.is_a?(String) || value.is_a?(Symbol) || value.is_a?(TrueClass) || value.is_a?(FalseClass) || value.is_a?(Numeric)

  nil
end

Instance Method Details

#avatar?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'app/components/hakumi_components/skeleton/component.rb', line 81

def avatar?
  !@avatar.nil?
end

#avatar_classesObject



96
97
98
99
100
101
102
# File 'app/components/hakumi_components/skeleton/component.rb', line 96

def avatar_classes
  modifiers = {}
  modifiers[avatar_size] = true if avatar_size != :default
  modifiers[avatar_shape] = true if avatar_shape

  class_names("skeleton-avatar", modifiers)
end

#avatar_styleObject



105
106
107
108
109
# File 'app/components/hakumi_components/skeleton/component.rb', line 105

def avatar_style
  return nil unless avatar_size.is_a?(Numeric)

  "width: #{avatar_size}px; height: #{avatar_size}px;"
end

#loading?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'app/components/hakumi_components/skeleton/component.rb', line 68

def loading?
  @loading
end

#paragraph?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'app/components/hakumi_components/skeleton/component.rb', line 91

def paragraph?
  !@paragraph.nil?
end

#paragraph_row_style(width) ⇒ Object



129
130
131
132
133
# File 'app/components/hakumi_components/skeleton/component.rb', line 129

def paragraph_row_style(width)
  return nil unless width

  "width: #{width};"
end

#paragraph_row_widthsObject



120
121
122
123
124
125
126
# File 'app/components/hakumi_components/skeleton/component.rb', line 120

def paragraph_row_widths
  return [] unless paragraph?
  paragraph = @paragraph
  return [] if paragraph.nil?

  paragraph.row_widths(DEFAULT_PARAGRAPH_ROWS).map { |value| dimension_to_css(value) }
end

#title?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'app/components/hakumi_components/skeleton/component.rb', line 86

def title?
  !@title.nil?
end

#title_styleObject



112
113
114
115
116
117
# File 'app/components/hakumi_components/skeleton/component.rb', line 112

def title_style
  width = @title.nil? ? nil : dimension_to_css(@title.width)
  return nil unless width

  "width: #{width};"
end

#wrapper_attributesObject



73
74
75
76
77
78
# File 'app/components/hakumi_components/skeleton/component.rb', line 73

def wrapper_attributes
  merge_attributes(
    { class: wrapper_classes },
    @html_options.except(:class)
  )
end