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:

Direct Known Subclasses

Ariadne::Behaviors::Tooltip, ConditionalWrapper, Form::BaseComponent, Form::BaseInputComponent, Form::ToggleGroup::Component, Form::ToggleGroup::Option::Component, Layout::Content::Component, Layout::Grid::Component, Layout::Grid::Item::Component, Layout::LabelBlock::Component, Layout::Narrow::Component, Layout::NavBar::Component, Layout::SectionBlock::Component, Layout::SectionBlock::Header::Component, Layout::Sidebar::Component, Layout::Sidebar::Footer::Component, Layout::Sidebar::Group::Component, Layout::Sidebar::Group::Item::Component, Layout::Sidebar::Header::Component, Layout::TwoPanel::Component, Layout::Wide::Component, UI::Accordion::Component, UI::Accordion::Item::Component, UI::Avatar::Component, UI::Badge::Component, UI::Banner::Component, UI::Blankslate::Component, UI::Button::Component, UI::Card::Body::Component, UI::Card::Component, UI::Card::Footer::Component, UI::Card::Header::Component, UI::ClipboardCopy::Component, UI::ColorDot::Component, UI::Combobox::Component, UI::Dialog::Component, UI::Dialog::Footer::Component, UI::Heroicon::Component, UI::Image::Component, UI::Link::Component, UI::List::Component, UI::List::Item::Component, UI::Pagination::Component, UI::Popover::Component, UI::RelativeTime::Component, UI::Shortcut::Component, UI::Skeleton::Component, UI::StatsPanel::Component, UI::StatsPanel::Item::Component, UI::Table::Cell::Component, UI::Table::Component, UI::Table::Footer::Component, UI::Table::Header::Component, UI::Table::Row::Component, UI::Typography::Component

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 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

.audited_at(date = nil) ⇒ Object



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

def audited_at(date = nil)
  if date.present?
    @audited_at = date
  else
    @audited_at
  end
end

.component_id(&block) ⇒ Object



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

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

.component_nameObject



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

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

.generate_id(base_name: stimulus_name) ⇒ Object



61
62
63
# File 'app/components/ariadne/base_component.rb', line 61

def generate_id(base_name: stimulus_name)
  "#{base_name}-#{SecureRandom.uuid}"
end

.i18n_scopeObject



70
71
72
# File 'app/components/ariadne/base_component.rb', line 70

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

.stimulus_nameObject



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

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

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



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

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

Instance Method Details

#class_for(name) ⇒ Object



94
95
96
97
98
99
100
# File 'app/components/ariadne/base_component.rb', line 94

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



82
83
84
85
86
87
88
# File 'app/components/ariadne/base_component.rb', line 82

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



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

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

#html_attributesObject



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

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

#merge_data_attributes(html_attrs, component_data_attrs) ⇒ Object

combines incoming user-defined data attributes with the component’s data attributes



33
34
35
36
37
38
39
40
41
42
# File 'app/components/ariadne/base_component.rb', line 33

def merge_data_attributes(html_attrs, component_data_attrs)
  html_attrs[:data] ||= {}

  html_attrs[:data].tap do |_html_attrs_data|
    component_data_attrs.keys.each do |key|
      html_attrs[:data][key] ||= ""
      html_attrs[:data][key] = "#{html_attrs[:data][key]} #{component_data_attrs[key]}".strip
    end
  end
end

#merge_tailwind_classes(classes) ⇒ Object



28
29
30
# File 'app/components/ariadne/base_component.rb', line 28

def merge_tailwind_classes(classes)
  Ariadne::ViewComponents.tailwind_merger.merge(classes)
end

#optionsObject



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

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

#validate_aria_label!(html_attrs) ⇒ Object

Raises:

  • (ArgumentError)


110
111
112
113
114
# File 'app/components/ariadne/base_component.rb', line 110

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