Module: MaquinaComponents::ComponentsHelper

Defined in:
app/helpers/maquina_components/components_helper.rb

Overview

Shared plumbing for component partials.

Constant Summary collapse

COMPONENT_IDENTITY_KEYS =

Keys a component owns outright: its identity and its styling hooks.

[:component, :variant, :size].freeze

Instance Method Summary collapse

Instance Method Details

#component_identity_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'app/helpers/maquina_components/components_helper.rb', line 51

def component_identity_key?(key)
  name = key.to_s
  COMPONENT_IDENTITY_KEYS.include?(name.to_sym) ||
    name.end_with?("_part", "-part")
end

#merge_component_data(html_options, **component_data) ⇒ Hash

Merges a caller's data: hash with a component's own data attributes.

Component values win for the identity keys only — :component, :variant, :size and any *_part key (data-table-part, data-sidebar-part, ...) — so callers can't accidentally break a component's styling hooks. For every other key the caller wins, which is what makes per-instance overrides (a drawer trigger pointing at one specific drawer, an aria hook, a Stimulus value) possible.

The Stimulus token keys :controller and :action concatenate instead, so a caller can attach extra behavior without losing the component's:

<%= render "components/combobox", name: "country",
    data: { controller: "analytics", action: "change->analytics#track" } %>
# => data-controller="combobox analytics"

nil values are dropped, so active: (active ? true : nil) renders data-active="true" or no attribute at all rather than "false".

Parameters:

  • html_options (Hash)

    the partial's **html_options (data: is consumed)

  • component_data (Hash)

    the component's own data attributes

Returns:

  • (Hash)

    the merged data hash



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/helpers/maquina_components/components_helper.rb', line 26

def merge_component_data(html_options, **component_data)
  user_data = html_options.delete(:data) || {}

  # Caller wins by default; identity keys below are restored afterwards.
  merged = component_data.merge(user_data.symbolize_keys)

  component_data.each_key do |key|
    next unless component_identity_key?(key)

    merged[key] = component_data[key]
  end

  [:controller, :action].each do |key|
    user_value = user_data[key] || user_data[key.to_s]
    next if user_value.blank? || component_data[key].blank?

    merged[key] = "#{component_data[key]} #{user_value}"
  end

  merged.compact
end