Class: Ariadne::BaseComponent

Inherits:
Component
  • Object
show all
Defined in:
app/components/ariadne/base_component.rb

Overview

All Ariadne ViewComponents accept a standard set of options: classes, which match Tailwind CSS classes, and attributes, which match HTML attributes..

Under the hood, any-non class attributes is passed to Rails’ [tag](api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-tag) (for self-closing tags) or [content_tag](api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-content_tag).

Constant Summary collapse

SELF_CLOSING_TAGS =
[:area, :base, :br, :col, :embed, :hr, :img, :input, :link, :meta, :param, :source, :track, :wbr].freeze

Constants inherited from Component

Component::BASE_HIDDEN_CLASS, Component::BASE_MAIN_CLASSES, Component::BASE_WRAPPER_CLASSES, Component::INVALID_ARIA_LABEL_TAGS

Constants included from ActionViewExtensions::FormHelper

ActionViewExtensions::FormHelper::DEFAULT_FORM_CLASSES

Constants included from Status::Dsl

Status::Dsl::STATUSES

Constants included from ViewHelper

ViewHelper::HELPERS

Constants included from FetchOrFallbackHelper

FetchOrFallbackHelper::INTEGER_TYPES, FetchOrFallbackHelper::InvalidValueError, FetchOrFallbackHelper::TRUE_OR_FALSE

Instance Method Summary collapse

Methods included from ActionViewExtensions::FormHelper

#ariadne_form_with

Methods included from ClassNameHelper

#class_names

Methods included from LoggerHelper

#logger, #silence_deprecations?, #silence_warnings?

Methods included from FetchOrFallbackHelper

#check_incoming_attribute, #check_incoming_tag, #check_incoming_value, #fetch_or_raise, #fetch_or_raise_boolean, #fetch_or_raise_integer

Constructor Details

#initialize(tag:, classes: "", attributes: {}) ⇒ BaseComponent

Returns a new instance of BaseComponent.



11
12
13
14
15
16
17
18
19
20
# File 'app/components/ariadne/base_component.rb', line 11

def initialize(tag:, classes: "", attributes: {})
  @tag = tag

  @attributes = validate_attributes(tag: tag, attributes: attributes) || {}
  @attributes[:"data-ariadne-view-component"] = true

  classes = @attributes.fetch(:classes, "") if classes.blank?
  @classes = validate_class_names(classes) || {}
  @content_tag_args = {}
end

Instance Method Details

#callObject



22
23
24
25
26
27
28
29
# File 'app/components/ariadne/base_component.rb', line 22

def call
  options = @attributes.merge(@classes)
  if SELF_CLOSING_TAGS.include?(@tag)
    tag(@tag, options)
  else
    (@tag, content, options)
  end
end