Module: Anchor::ViewHelper

Included in:
Component
Defined in:
app/helpers/anchor/view_helper.rb

Constant Summary collapse

ANCHOR_HELPERS =
i[
  action_menu
  assistive_tech_notifications
  autocomplete/results
  badge
  banner
  breadcrumbs
  button
  copy_to_clipboard
  dialog
  favicons
  filter
  icon
  link
  loading_indicator
  
  panel
  page
  popover
  prose
  side_nav
  tab_bar
  table
  text
  toast
].freeze

Instance Method Summary collapse

Instance Method Details

#anchor_form_with(**options) ⇒ Object



44
45
46
47
48
49
50
# File 'app/helpers/anchor/view_helper.rb', line 44

def anchor_form_with(**options, &)
  options[:builder] = Anchor::FormBuilder
  options[:class] = build_tag_values("flex flex-col gap-4", options[:class])
    .uniq

  form_with(**options, &)
end

#anchor_svg(name, aria: {}, data: {}, **attributes) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/helpers/anchor/view_helper.rb', line 52

def anchor_svg(name, aria: {}, data: {}, **attributes)
  file = ::Anchor::ViewComponents::SvgResolver.resolve(name)
  doc = Nokogiri::HTML::DocumentFragment.parse(file)
  svg = doc.at_css "svg"
  attributes[:class] = build_tag_values(attributes[:class]).uniq.join(" ")

  attributes.each_pair do |key, value|
    svg[key] = value
  end

  aria.each_pair do |key, value|
    svg["aria-#{key}"] = value
  end

  data.each_pair do |key, value|
    svg["data-#{key.to_s.dasherize}"] = value
  end

  doc.to_html.html_safe
end

#deep_blank?(value) ⇒ Boolean



91
92
93
94
95
96
97
98
99
100
# File 'app/helpers/anchor/view_helper.rb', line 91

def deep_blank?(value)
  case value
  when Array
    value.all?(&:blank?)
  when Hash
    value.values.all? { deep_blank?(_1) }
  else
    value.blank?
  end
end

#merge_options(new, original) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'app/helpers/anchor/view_helper.rb', line 80

def merge_options(new, original)
  new = new.dup
  original = original.dup
  original.except(:class, :data, :aria).merge(
    aria: original.fetch(:aria, {}).merge(new.delete(:aria) || {}),
    class: build_tag_values(original[:class], new.delete(:class)).uniq,
    data: original.fetch(:data, {}).merge(new.delete(:data) || {}),
    **new
  ).reject { |_, v| deep_blank?(v) }
end

#popover_trigger_attributes(popovertarget:, data: {}) ⇒ Object



73
74
75
76
77
78
# File 'app/helpers/anchor/view_helper.rb', line 73

def popover_trigger_attributes(popovertarget:, data: {})
  {
    data: { popover_target: "button", **data },
    popovertarget:,
  }
end