Class: Railsboot::HeadingComponent

Inherits:
Component
  • Object
show all
Defined in:
app/components/railsboot/heading_component.rb

Overview

ViewComponent: renders the heading block.

Direct Known Subclasses

HeadingWithAnchorComponent

Constant Summary collapse

TAGS =

Tags.

%w[h1 h2 h3 h4 h5 h6 div span].freeze
DEFAULT_TAG =

Default tag.

"h1".freeze
SIZES =

Sizes.

%w[h1 h2 h3 h4 h5 h6].freeze
DISPLAYS =

Displays.

%w[1 2 3 4 5 6].freeze

Instance Method Summary collapse

Constructor Details

#initialize(tag: DEFAULT_TAG, size: nil, display: nil, **html_attributes) ⇒ HeadingComponent

Returns a new instance of HeadingComponent.

Parameters:

  • tag (String) (defaults to: DEFAULT_TAG)

    the tag option

  • size (Object, nil) (defaults to: nil)

    the size option

  • display (Object, nil) (defaults to: nil)

    the display option

  • html_attributes (Hash)

    additional HTML attributes applied to the rendered element

Options Hash (**html_attributes):

  • :class (String)

    additional CSS classes, merged with the component's own classes

  • :tag (String)

    HTML tag to render for the element



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/components/railsboot/heading_component.rb', line 20

def initialize(tag: DEFAULT_TAG, size: nil, display: nil, **html_attributes)
  @tag = fetch_or_raise(tag, TAGS)
  @size = size.to_s if size.present? && SIZES.include?(size.to_s)
  @display = display.to_s if display.present? && DISPLAYS.include?(display.to_s)
  @html_attributes = html_attributes

  @html_attributes[:tag] = @tag
  @html_attributes[:class] = class_names(
    { @size => @size.present? },
    { "display-#{@display}" => @display.present? },
    html_attributes.delete(:class)
  ).presence
end