Class: HakumiComponents::Tooltip::Component

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

Constant Summary collapse

Trigger =
T.type_alias { Types::HtmlKey }
Placement =
T.type_alias { Types::HtmlKey }
Color =
T.type_alias { Types::HtmlKey }
TRIGGERS =
T.let(%i[hover focus click].freeze, T::Array[Symbol])
PLACEMENTS =
T.let(%i[
  top top_left top_right
  bottom bottom_left bottom_right
  left left_top left_bottom
  right right_top right_bottom
].freeze, T::Array[Symbol])
COLORS =
T.let(%i[default pink red yellow orange cyan green blue purple geekblue magenta volcano gold lime].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

Instance Attribute 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, placement: :top, trigger: :hover, arrow: true, color: :default, open: nil, fresh: false, **html_options) ⇒ Component

Returns a new instance of Component.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/components/hakumi_components/tooltip/component.rb', line 34

def initialize(
  title: nil,
  placement: :top,
  trigger: :hover,
  arrow: true,
  color: :default,
  open: nil,
  fresh: false,
  **html_options
)
  @title = T.let(title, HakumiComponents::Types::Renderable)
  @placement = T.let(placement.to_sym, Symbol)
  @trigger = T.let(trigger.to_sym, Symbol)
  @arrow = T.let(cast_boolean(arrow) != false, T::Boolean)
  @color = T.let(color.is_a?(String) ? color : color.to_sym, Color)
  @open = T.let(open, T.nilable(T::Boolean))
  @fresh = T.let(cast_boolean(fresh) ? true : false, T::Boolean)
  @html_options = T.let(html_options, HakumiComponents::Types::HtmlAttributes)

  validate_props!
end

Instance Attribute Details

#arrowObject (readonly)

Returns the value of attribute arrow.



63
64
65
# File 'app/components/hakumi_components/tooltip/component.rb', line 63

def arrow
  @arrow
end

#colorObject (readonly)

Returns the value of attribute color.



66
67
68
# File 'app/components/hakumi_components/tooltip/component.rb', line 66

def color
  @color
end

#freshObject (readonly)

Returns the value of attribute fresh.



63
64
65
# File 'app/components/hakumi_components/tooltip/component.rb', line 63

def fresh
  @fresh
end

#openObject (readonly)

Returns the value of attribute open.



69
70
71
# File 'app/components/hakumi_components/tooltip/component.rb', line 69

def open
  @open
end

#placementObject (readonly)

Returns the value of attribute placement.



60
61
62
# File 'app/components/hakumi_components/tooltip/component.rb', line 60

def placement
  @placement
end

#titleObject (readonly)

Returns the value of attribute title.



57
58
59
# File 'app/components/hakumi_components/tooltip/component.rb', line 57

def title
  @title
end

#triggerObject (readonly)

Returns the value of attribute trigger.



60
61
62
# File 'app/components/hakumi_components/tooltip/component.rb', line 60

def trigger
  @trigger
end

Instance Method Details

#tooltip_classesObject



80
81
82
# File 'app/components/hakumi_components/tooltip/component.rb', line 80

def tooltip_classes
  class_names("tooltip", {}, [ placement_class, (color_class if preset_color?) ])
end

#tooltip_content?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'app/components/hakumi_components/tooltip/component.rb', line 107

def tooltip_content?
  value_present?(@title)
end

#tooltip_textObject



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

def tooltip_text
  render_value(@title)
end

#wrapper_attributesObject



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

def wrapper_attributes
  values = {
    placement: @placement.to_s.dasherize,
    trigger: @trigger.to_s,
    arrow: @arrow,
    fresh: @fresh
  }
  values[:open] = @open unless @open.nil?
  values[:color] = @color.to_s if custom_color?

  merge_attributes(
    { class: wrapper_classes, data: stimulus_attrs(stimulus_controller, values) },
    @html_options
  )
end

#wrapper_classesObject



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

def wrapper_classes
  extras = T.let([], T::Array[T.nilable(String)])
  extras << "hakumi-tooltip-open" if @open == true
  extras.concat(Array(html_classes(@html_options)).compact)
  class_names("tooltip-wrapper", {}, extras)
end