Class: HakumiComponents::Drawer::Component

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

Constant Summary collapse

Placement =
T.type_alias { Types::HtmlKey }
Size =
T.type_alias { T.any(Integer, Types::HtmlKey) }
ResolvedSize =
T.type_alias { T.any(Symbol, Integer) }
PLACEMENTS =
T.let(%i[left right top bottom].freeze, T::Array[Symbol])
SIZES =
T.let({
  default: 378,
  large: 736
}.freeze, T::Hash[Symbol, 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::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, message: nil, open: false, placement: :right, size: :default, width: nil, height: nil, closable: true, mask: true, mask_closable: true, keyboard: true, destroy_on_close: false, **html_options) ⇒ Component

Returns a new instance of Component.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/components/hakumi_components/drawer/component.rb', line 63

def initialize(
  title: nil,
  message: nil,
  open: false,
  placement: :right,
  size: :default,
  width: nil,
  height: nil,
  closable: true,
  mask: true,
  mask_closable: true,
  keyboard: true,
  destroy_on_close: false,
  **html_options
)
  @title = T.let(title, HakumiComponents::Types::Renderable)
  @message = T.let(message, HakumiComponents::Types::Renderable)
  @open = T.let(open, T::Boolean)
  @placement = T.let(placement.to_sym, Symbol)
  @size = T.let(size.is_a?(String) ? size.to_sym : size, ResolvedSize)
  @width = T.let(width, T.nilable(Integer))
  @height = T.let(height, T.nilable(Integer))
  @closable = T.let(closable, T::Boolean)
  @mask = T.let(mask, T::Boolean)
  @mask_closable = T.let(mask_closable, T::Boolean)
  @keyboard = T.let(keyboard, T::Boolean)
  @destroy_on_close = T.let(destroy_on_close, T::Boolean)
  @html_options = T.let(html_options, HakumiComponents::Types::HtmlAttributes)

  validate_props!
end

Class Method Details

.extract_controller_locals(params) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/components/hakumi_components/drawer/component.rb', line 29

def self.extract_controller_locals(params)
  {
    open: boolean_html_param(html_param(params, :open)),
    placement: symbol_html_param(html_param(params, :placement)),
    size: size_param(html_param(params, :size)),
    width: integer_html_param(html_param(params, :width)),
    height: integer_html_param(html_param(params, :height)),
    closable: boolean_html_param(html_param(params, :closable) || true),
    mask: boolean_html_param(html_param(params, :mask) || true),
    mask_closable: boolean_html_param(html_param(params, :mask_closable) || true),
    keyboard: boolean_html_param(html_param(params, :keyboard) || true),
    destroy_on_close: boolean_html_param(html_param(params, :destroy_on_close)),
    title: html_primitive_param(html_param(params, :title)),
    message: html_primitive_param(html_param(params, :message))
  }.compact
end

Instance Method Details

#body_contentObject



146
147
148
149
150
151
# File 'app/components/hakumi_components/drawer/component.rb', line 146

def body_content
  return render_value(body) if body?
  return render_value(content) if content.present?

  render_value(@message)
end

#data_attributesObject



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/components/hakumi_components/drawer/component.rb', line 111

def data_attributes
  data = T.let({
    controller: "hakumi--drawer",
    hakumi__drawer_open_value: @open,
    hakumi__drawer_placement_value: @placement.to_s,
    hakumi__drawer_mask_closable_value: @mask_closable,
    hakumi__drawer_keyboard_value: @keyboard,
    hakumi__drawer_destroy_on_close_value: @destroy_on_close
  }, HakumiComponents::Types::DataAttributes)
  existing = @html_options[:data]
  existing.each { |key, value| data[key] = value } if existing.is_a?(Hash)
  data
end

#drawer_classesObject



96
97
98
# File 'app/components/hakumi_components/drawer/component.rb', line 96

def drawer_classes
  class_names("drawer", { @placement => true, open: @open })
end

#header_contentObject



154
155
156
157
158
# File 'app/components/hakumi_components/drawer/component.rb', line 154

def header_content
  return render_value(header) if header?

  render_value(@title)
end

#render_footer?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'app/components/hakumi_components/drawer/component.rb', line 141

def render_footer?
  footer?
end

#render_header?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'app/components/hakumi_components/drawer/component.rb', line 136

def render_header?
  header? || value_present?(@title) || @closable || extra?
end

#root_attributesObject



101
102
103
104
105
106
107
108
# File 'app/components/hakumi_components/drawer/component.rb', line 101

def root_attributes
  base_attrs = {
    class: drawer_classes,
    data: data_attributes
  }

  merge_attributes(base_attrs, @html_options)
end

#wrapper_styleObject



126
127
128
129
130
131
132
133
# File 'app/components/hakumi_components/drawer/component.rb', line 126

def wrapper_style
  size_value = resolve_size
  if %i[left right].include?(@placement)
    "width: #{dimension_to_css(@width || size_value)};"
  else
    "height: #{dimension_to_css(@height || size_value)};"
  end
end