Class: HakumiComponents::Popover::Component

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

Constant Summary collapse

Placement =
T.type_alias { T.any(Symbol, String) }
TriggerValue =
T.type_alias { T.any(Symbol, String, T::Array[T.any(Symbol, String)]) }
PLACEMENTS =
T.let(%i[
  top topLeft topRight
  bottom bottomLeft bottomRight
  left leftTop leftBottom
  right rightTop rightBottom
  auto
].freeze, T::Array[Symbol])
TRIGGERS =
T.let(%i[click hover focus].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(title: nil, body: nil, trigger_body: nil, placement: :top, trigger: :hover, open: nil, arrow: true, disabled: false, auto_adjust_overflow: true, **html_options) ⇒ Component

Returns a new instance of Component.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/components/hakumi_components/popover/component.rb', line 55

def initialize(
  title: nil,
  body: nil,
  trigger_body: nil,
  placement: :top,
  trigger: :hover,
  open: nil,
  arrow: true,
  disabled: false,
  auto_adjust_overflow: true,
  **html_options
)
  @title = T.let(title, HakumiComponents::Types::Renderable)
  @body = T.let(body, HakumiComponents::Types::Renderable)
  @trigger_body = T.let(trigger_body, HakumiComponents::Types::Renderable)
  @placement = T.let(placement.to_sym, Symbol)
  @trigger = T.let(trigger, T.nilable(TriggerValue))
  @open = T.let(open, T.nilable(T::Boolean))
  @arrow = T.let(arrow, T::Boolean)
  @disabled = T.let(disabled, T::Boolean)
  @auto_adjust_overflow = T.let(auto_adjust_overflow, T::Boolean)
  @html_options = T.let(html_options, HakumiComponents::Types::HtmlAttributes)
  raw_wrapper_id = @html_options[:id]
  @wrapper_id = T.let(raw_wrapper_id&.to_s, T.nilable(String))

  validate_props!
end

Class Method Details

.extract_controller_locals(params) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/components/hakumi_components/popover/component.rb', line 27

def self.extract_controller_locals(params)
  {
    title: html_primitive_param(html_param(params, :title)),
    body: html_primitive_param(html_param(params, :body)),
    trigger_body: html_primitive_param(html_param(params, :trigger_body)),
    placement: symbol_html_param(html_param(params, :placement)),
    trigger: trigger_param(html_param(params, :trigger)),
    open: boolean_html_param(html_param(params, :open)),
    arrow: html_param(params, :arrow).nil? ? true : boolean_html_param(html_param(params, :arrow)),
    disabled: boolean_html_param(html_param(params, :disabled)),
    auto_adjust_overflow: html_param(params, :auto_adjust_overflow).nil? ? true : boolean_html_param(html_param(params, :auto_adjust_overflow))
  }
end

Instance Method Details

#data_attributesObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/components/hakumi_components/popover/component.rb', line 113

def data_attributes
  data = T.let({
    controller: "hakumi--popover",
    "hakumi--popover-placement-value": @placement,
    "hakumi--popover-trigger-value": normalized_triggers.join(","),
    "hakumi--popover-disabled-value": @disabled,
    "hakumi--popover-arrow-value": @arrow,
    "hakumi--popover-auto-adjust-overflow-value": @auto_adjust_overflow
  }, HakumiComponents::Types::DataAttributes)
  data[:"hakumi--popover-open-value"] = @open unless @open.nil?
  existing = @html_options[:data]
  existing.each { |key, value| data[key] = value } if existing.is_a?(Hash)
  data
end

#popover_classesObject



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

def popover_classes
  class_names("popover", { "arrow-hidden": !@arrow })
end

#popover_content?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'app/components/hakumi_components/popover/component.rb', line 149

def popover_content?
  @body.present?
end

#popover_dom_idObject



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

def popover_dom_id
  return if wrapper_id.blank?
  "#{wrapper_id}-popover"
end

#render_popover_contentObject



139
140
141
# File 'app/components/hakumi_components/popover/component.rb', line 139

def render_popover_content
  render_value(@body)
end

#render_titleObject



134
135
136
# File 'app/components/hakumi_components/popover/component.rb', line 134

def render_title
  render_value(@title)
end

#title_present?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'app/components/hakumi_components/popover/component.rb', line 144

def title_present?
  @title.present?
end

#trigger_contentObject



129
130
131
# File 'app/components/hakumi_components/popover/component.rb', line 129

def trigger_content
  content.presence || render_value(@trigger_body || @body)
end

#wrapper_classesObject



84
85
86
87
88
89
90
91
92
93
94
# File 'app/components/hakumi_components/popover/component.rb', line 84

def wrapper_classes
  extras = T.let([], T::Array[T.nilable(String)])
  extras << "hakumi-popover-disabled" if @disabled
  extras.concat(Array(html_classes(@html_options)).compact)

  class_names(
    "popover-wrapper",
    {},
    extras
  )
end

#wrapper_idObject



97
98
99
# File 'app/components/hakumi_components/popover/component.rb', line 97

def wrapper_id
  @wrapper_id
end