Class: Primer::BaseComponent

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

Overview

Base component used by other Primer components.

tag(symbol): HTML tag name to be passed to tag.send(tag) class_names(string): CSS class name value to be concatenated with generated Primer CSS classes args(hash): Combination of arguments for classes_from_hash and content_tag

Example usage: <%= render Primer::BaseComponent, tag: :a, href: “www.google.com”, mt: 4 do %>Link<% end %> generates: <a href=“www.google.com” class=“mt-4”>Link</a>

Constant Summary collapse

TEST_SELECTOR_TAG =
:test_selector

Constants included from FetchOrFallbackHelper

FetchOrFallbackHelper::InvalidValueError

Instance Method Summary collapse

Methods included from FetchOrFallbackHelper

#fetch_or_fallback

Methods included from ClassNameHelper

#class_names

Constructor Details

#initialize(tag:, classes: nil, **args) ⇒ BaseComponent



17
18
19
20
21
22
23
# File 'app/components/primer/base_component.rb', line 17

def initialize(tag:, classes: nil, **args)
  @tag = tag
  @result = Primer::Classify.call(**args.merge(classes: classes))

  # Filter out Primer keys so they don't get assigned as HTML attributes
  @content_tag_args = add_test_selector(args).except(*Primer::Classify::VALID_KEYS)
end

Instance Method Details

#callObject



25
26
27
28
29
# File 'app/components/primer/base_component.rb', line 25

def call
  tag.public_send(
    @tag, content, **@content_tag_args.merge(@result)
  )
end