module Ariadne::AttributesHelper
def aria(html_attrs, val)
def aria(html_attrs, val) html_attrs[:"aria-#{val}"] || html_attrs.dig(:aria, val.to_sym) end
def data(html_attrs, val)
def data(html_attrs, val) html_attrs[:"data-#{val}"] || html_attrs.dig(:data, val.to_sym) end
def merge_aria(*hashes)
{ aria: { labelled_by: id } }
html_attrs,
html_attrs[:aria] = merge_aria(
hashes. Consider using this pattern in component initializers:
It's designed to be used to normalize and merge aria information from system_arguments
=> { labelledby: "foo bar" }
Eg. merge_aria({ "aria-labelledby": "foo" }, { aria: { labelledby: "bar" } })
will combine these plural attributes into a composite string.
Certain aria attributes can contain multiple values separated by spaces. merge_aria
=> { disabled: "true", invalid: "true" }
Eg. merge_aria({ "aria-disabled": "true" }, { aria: { invalid: "true" } })
each hash and returns them in the new hash.
Merges hashes that contain "aria-*" keys and nested aria: hashes. Removes keys from
def merge_aria(*hashes) merge_prefixed_attribute_hashes( *hashes, prefix: :aria, plural_keys: PLURAL_ARIA_ATTRIBUTES ) end
def merge_data(*hashes)
{ data: { foo: "bar" } }
html_attrs,
html_attrs[:data] = merge_aria(
hashes. Consider using this pattern in component initializers:
It's designed to be used to normalize and merge data information from system_arguments
=> { target: "foo bar" }
Eg. merge_data({ "data-target": "foo" }, { data: { target: "bar" } })
will combine these plural attributes into a composite string.
Certain data attributes can contain multiple values separated by spaces. merge_data
=> { foo: "true", bar: "true" }
Eg. merge_data({ "data-foo": "true" }, { data: { bar: "true" } })
each hash and returns them in the new hash.
Merges hashes that contain "data-*" keys and nested data: hashes. Removes keys from
def merge_data(*hashes) merge_prefixed_attribute_hashes( *hashes, prefix: :data, plural_keys: PLURAL_DATA_ATTRIBUTES ) end
def merge_prefixed_attribute_hashes(*hashes, prefix:, plural_keys:)
def merge_prefixed_attribute_hashes(*hashes, prefix:, plural_keys:) {}.tap do |result| hashes.each do |hash| next unless hash prefix_hash = hash.delete(prefix) || {} prefix_hash.each_pair do |key, val| result[key] = if plural_keys.include?(key) [*(result[key] || "").split, val].join(" ").strip else val end end hash.delete_if do |key, val| key_s = key.to_s if key.start_with?("#{prefix}-") bare_key = key_s.sub("#{prefix}-", "").to_sym result[bare_key] = if plural_keys.include?(bare_key) [*(result[bare_key] || "").split, val].join(" ").strip else val end true else false end end end end end
def prepend_action(html_attrs, action)
def prepend_action(html_attrs, action) prepend_data_attribute(html_attrs, :action, action) end
def prepend_controller(html_attrs, name = stimulus_name)
def prepend_controller(html_attrs, name = stimulus_name) prepend_data_attribute(html_attrs, :controller, name) end
def prepend_data_attribute(html_attrs, attr_name, attr_value)
def prepend_data_attribute(html_attrs, attr_name, attr_value) html_attrs[:data] ||= {} html_attrs[:data][attr_name] = "#{attr_value} #{html_attrs[:data][attr_name]}".strip html_attrs end