Class: Ariadne::BaseComponent

Inherits:
ViewComponentContrib::Base
  • Object
show all
Includes:
AttributesHelper, ViewComponent::HTMLAttrs, ViewComponent::StyleVariants, ViewHelper, ViewComponentContrib::StyleVariants, ViewComponentContrib::TranslationHelper
Defined in:
app/components/ariadne/base_component.rb

Overview

:nodoc:

Constant Summary collapse

ACCEPT_ANYTHING =
lambda { |static_content = nil, &block|
  next static_content if static_content.present?

  view_context.capture { block&.call }
}

Constants included from ViewHelper

ViewHelper::HELPERS

Constants included from AttributesHelper

AttributesHelper::PLURAL_ARIA_ATTRIBUTES, AttributesHelper::PLURAL_DATA_ATTRIBUTES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ViewHelper

generate_id

Methods included from AttributesHelper

#aria, #data, #merge_aria, #merge_data, #merge_prefixed_attribute_hashes, #prepend_action, #prepend_controller, #prepend_data_attribute

Methods included from ViewComponent::StyleVariants

#merged_styles

Class Method Details

.component_id(&block) ⇒ Object



44
45
46
# File 'app/components/ariadne/base_component.rb', line 44

def component_id(&block)
  @component_id ||= block || proc { self.class.component_name.delete_prefix("ui/").gsub(/[^a-z0-9]+/, "-") }
end

.component_nameObject



40
41
42
# File 'app/components/ariadne/base_component.rb', line 40

def component_name
  @component_name ||= name.sub(/::Component$/, "").underscore
end

.i18n_scopeObject



53
54
55
# File 'app/components/ariadne/base_component.rb', line 53

def i18n_scope
  @i18n_scope ||= component_name.split("/")
end

.stimulus_nameObject



48
49
50
51
# File 'app/components/ariadne/base_component.rb', line 48

def stimulus_name
  # @tag stimulus-id
  @stimulus_name ||= component_name.gsub(/[^a-z0-9]+/, "-")
end

.translate(key, scope: nil, **options) ⇒ Object



57
58
59
# File 'app/components/ariadne/base_component.rb', line 57

def translate(key, scope: nil, **options)
  I18n.t(key, **options, scope: [*i18n_scope, *Array.wrap(scope)])
end

Instance Method Details

#class_for(name) ⇒ Object



77
78
79
80
81
82
83
# File 'app/components/ariadne/base_component.rb', line 77

def class_for(name)
  # @tag isolated-styles
  # stripping away UI and Component
  modulename = self.class.name.split("::")[1...-1].join("--").downcase

  "ariadne-#{modulename}-#{name}"
end

#component(name) ⇒ Object

Support relative component names within components



65
66
67
68
69
70
71
# File 'app/components/ariadne/base_component.rb', line 65

def component(name, ...)
  return super unless name.starts_with?(".")

  full_name = Pathname.new(File.join(self.class.component_name, name)).cleanpath.to_s

  super(full_name, ...)
end

#component_idObject



73
74
75
# File 'app/components/ariadne/base_component.rb', line 73

def component_id
  @component_id ||= instance_eval(&self.class.component_id)
end

#html_attributesObject



97
98
99
# File 'app/components/ariadne/base_component.rb', line 97

def html_attributes
  tag.attributes(html_attrs.except(:class))
end

#in_turbo_frame(**options) ⇒ Object



85
86
87
# File 'app/components/ariadne/base_component.rb', line 85

def in_turbo_frame(**options)
  Ariadne::Turbo::Frame::Component.new(component: self, **options)
end

#in_turbo_stream(action: "update", **options) ⇒ Object



89
90
91
# File 'app/components/ariadne/base_component.rb', line 89

def in_turbo_stream(action: "update", **options)
  Ariadne::Turbo::StreamAction::Component.new(component: self, action: action, **options)
end

#optionsObject



93
94
95
# File 'app/components/ariadne/base_component.rb', line 93

def options
  @options ||= self.class.dry_initializer.attributes(self)
end

#stylesObject



101
102
# File 'app/components/ariadne/base_component.rb', line 101

def styles
end

#validate_aria_label!(html_attrs) ⇒ Object

Raises:

  • (ArgumentError)


104
105
106
107
108
# File 'app/components/ariadne/base_component.rb', line 104

def validate_aria_label!(html_attrs)
  aria_label = aria(html_attrs, "label")
  aria_labelledby = aria(html_attrs, "labelledby")
  raise ArgumentError, "`aria-label` or `aria-labelledby` is required." if aria_label.blank? && aria_labelledby.blank?
end