class Bootstrap4RailsComponents::Bootstrap::Components::Base

Base Component Defines conventional, shared behavior across Bootstrap components

Attributes

body[R]
options[RW]
view_context[RW]

Public Class Methods

new(component_options, view_context) click to toggle source
# File lib/bootstrap4_rails_components/bootstrap/components/base.rb, line 20
def initialize(component_options, view_context)
  self.options = defaults.merge!(component_options)
  self.view_context = view_context
  @body = options.fetch(:body, '')
  utility_initialize
  component_initialize
end

Public Instance Methods

component_family() click to toggle source

This is used to help identify where to find partials for rendering components.

Set the component family, e.g.: :breadcrumb on any sibling components.

For example: BreadcrumbItem & Breadcrumb are members of the :breadcrumb component_family

# File lib/bootstrap4_rails_components/bootstrap/components/base.rb, line 48
def component_family
  nil
end
component_initialize() click to toggle source

For components that inherit bootstrap, provide a second layer of initialization, for example: to initialize traits on design system components (which are not available on bootstrap)

# File lib/bootstrap4_rails_components/bootstrap/components/base.rb, line 80
def component_initialize; end
data() click to toggle source
# File lib/bootstrap4_rails_components/bootstrap/components/base.rb, line 52
def data
  options[:data] || {}
end
href() click to toggle source
# File lib/bootstrap4_rails_components/bootstrap/components/base.rb, line 68
def href
  options[:href]
end
html_options() click to toggle source
# File lib/bootstrap4_rails_components/bootstrap/components/base.rb, line 56
def html_options
  options.except(*non_html_attribute_options.uniq)
         .merge!(id: id,
                 class: css_classes,
                 data: data,
                 href: href,
                 style: style,
                 **assistive_html_attributes)
         .reject { |_k, v| v.blank? } # prevent empty attributes from showing up
                                      # Example: <div class>Text</div>
end
id() click to toggle source
# File lib/bootstrap4_rails_components/bootstrap/components/base.rb, line 72
def id
  options[:id]
end
render() { |: body)| ... } click to toggle source

This base render handles many of the components and can be changed to have a different base element by overriding the base_element. in some cases, the child component can also call super with a block to have this render as the wrapping element.

# File lib/bootstrap4_rails_components/bootstrap/components/base.rb, line 35
def render
  content_tag(base_element, html_options) do
    (block_given? ? yield : body)
  end
end
style() click to toggle source
# File lib/bootstrap4_rails_components/bootstrap/components/base.rb, line 83
def style
  options[:style]
end
utility_initialize() click to toggle source
# File lib/bootstrap4_rails_components/bootstrap/components/base.rb, line 81
def utility_initialize; end

Private Instance Methods

assistive_html_attributes() click to toggle source

Assigned on individual components as needed Ex: { role: 'alert' }

If aria assistive html is needed, see: Bootstrap::Utilities::AriaAssistable avoid passing aria to assistive_html_attributes directly

# File lib/bootstrap4_rails_components/bootstrap/components/base.rb, line 95
def assistive_html_attributes
  @assistive_html_attributes ||= {}
end
base_element() click to toggle source

the base_element is used in the default render for all components as the outer wrapping element. Typically, this is a div, but can be overriddent as a different static element in a child class or as a dynamic element in the child class. this allows most child components to not have to have their own render statement if their wrapping element is not a div

# File lib/bootstrap4_rails_components/bootstrap/components/base.rb, line 105
def base_element
  :div
end
component_class_name_string() click to toggle source
# File lib/bootstrap4_rails_components/bootstrap/components/base.rb, line 117
def component_class_name_string
  self.class.name.demodulize.to_s
end
component_css_class() click to toggle source

Fallback component css class name. Overwritten within individual classes for situations like Button's css class is 'btn'… Example: returns 'alert' from Bootstrap4RailsComponents::Bootstrap::Components::Alert

# File lib/bootstrap4_rails_components/bootstrap/components/base.rb, line 113
def component_css_class
  @component_css_class ||= component_class_name_string.underscore.dasherize.downcase
end
css_classes() click to toggle source

Manage or adjust the css_classes of the component by adding a new string of css classes to this method ex: super.push('new-class')

# File lib/bootstrap4_rails_components/bootstrap/components/base.rb, line 124
def css_classes
  @css_classes ||= [component_css_class, options[:class]].reject(&:nil?).uniq.join(' ').squish
end
defaults() click to toggle source
# File lib/bootstrap4_rails_components/bootstrap/components/base.rb, line 128
def defaults
  {
    # HTML Defaults
    class: '',
    id: nil,

    # Content
    # heading: (nil if heading.present?),
    body: nil,
    data: {},

    # Configuration
    # traits: ([] if traits.present?)
  }
end
non_html_attribute_options() click to toggle source

Remove attributes from html_options that shouldn't show up in the html element, ex: <div body='should not be here'>

# File lib/bootstrap4_rails_components/bootstrap/components/base.rb, line 145
def non_html_attribute_options
  @non_html_attribute_options ||= %i[body heading traits render_if render_unless]
end