Class: HakumiComponents::Message::Component

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

Constant Summary collapse

Type =
T.type_alias { T.any(Symbol, String) }
TYPES =
T.let(%i[success info warning error loading].freeze, T::Array[Symbol])

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(message: nil, type: nil, duration: 3.0, key: nil, icon: nil, closable: false, max_count: nil, top: nil, close_label: nil, **html_options) ⇒ Component

Returns a new instance of Component.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/components/hakumi_components/message/component.rb', line 44

def initialize(
  message: nil,
  type: nil,
  duration: 3.0,
  key: nil,
  icon: nil,
  closable: false,
  max_count: nil,
  top: nil,
  # i18n
  close_label: nil,
  **html_options
)
  @message = T.let(message, HakumiComponents::Types::HtmlPrimitive)
  @type = T.let(type&.to_sym, T.nilable(Symbol))
  @duration = T.let(duration.nil? ? 3.0 : duration.to_f, Float)
  @key = T.let(key, HakumiComponents::Types::HtmlPrimitive)
  @icon = T.let(icon, HakumiComponents::Types::HtmlPrimitive)
  @closable = T.let(closable, T::Boolean)
  @max_count = T.let(max_count, T.nilable(Integer))
  @top = T.let(top, T.nilable(Integer))
  @close_label = T.let(close_label, T.nilable(String))
  @html_options = T.let(html_options, HakumiComponents::Types::HtmlAttributes)

  validate_props!
end

Class Method Details

.extract_controller_locals(params) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/components/hakumi_components/message/component.rb', line 17

def self.extract_controller_locals(params)
  {
    message: html_primitive_param(html_param(params, :message)) || "Generated message content.",
    type: symbol_html_param(html_param(params, :type)),
    duration: float_html_param(html_param(params, :duration)),
    key: html_primitive_param(html_param(params, :key)),
    icon: html_primitive_param(html_param(params, :icon)),
    closable: boolean_html_param(html_param(params, :closable)),
    max_count: integer_html_param(html_param(params, :max_count)),
    top: integer_html_param(html_param(params, :top))
  }
end

Instance Method Details

#close_label_textObject



103
104
105
# File 'app/components/hakumi_components/message/component.rb', line 103

def close_label_text
  @close_label || t_default(:close, default: "Close")
end

#data_attributesObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/components/hakumi_components/message/component.rb', line 87

def data_attributes
  attrs = T.let({
    controller: "hakumi--message",
    hakumi__message_duration_value: @duration,
    hakumi__message_max_count_value: @max_count,
    hakumi__message_top_value: @top,
    hakumi__message_close_label_value: close_label_text
  }, HakumiComponents::Types::DataAttributes)

  payload = initial_payload
  attrs[:hakumi__message_payload_value] = payload.to_json if payload.present?

  attrs
end

#inline_styleObject



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

def inline_style
  styles = T.let([], T::Array[String])
  styles << "--hakumi-message-top: #{@top}px" if @top
  styles.join("; ") if styles.any?
end

#root_attributesObject



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/components/hakumi_components/message/component.rb', line 72

def root_attributes
  base_attrs = {
    class: wrapper_classes,
    data: data_attributes,
    role: "status",
    "aria-live": "polite",
    "aria-atomic": "true"
  }

  base_attrs[:style] = inline_style if inline_style

  merge_attributes(base_attrs, @html_options)
end

#wrapper_classesObject



108
109
110
# File 'app/components/hakumi_components/message/component.rb', line 108

def wrapper_classes
  component_classes("message", {}, @html_options)
end