Sha256: b0045c20f23161e4788fa67562dde7013af20e13b6670399fb877498d6b3ce90

Contents?: true

Size: 877 Bytes

Versions: 12

Compression:

Stored size: 877 Bytes

Contents

# frozen_string_literal: true

module Bs5
  class BadgeComponent < ViewComponent::Base
    STYLES = %i[primary secondary success danger warning info light dark].freeze

    attr_reader :text, :color

    include ActiveModel::Validations
    validates :color, style: true

    def initialize(text:, color: :secondary, pill: false)
      @text = text
      @color = color.to_sym
      @pill = pill
    end

    def before_render
      raise errors.full_messages.to_sentence if invalid?
    end

    private

    def component_class
      class_names = ['badge', contextual_class]
      class_names << %w[rounded-pill] if pill?
      class_names.join(' ')
    end

    def pill?
      @pill
    end

    def contextual_class
      class_names = ["bg-#{@color}"]
      class_names << %w[text-dark] if color.in?(%i[warning info light])
      class_names.join(' ')
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
bs5-0.0.31 app/components/bs5/badge_component.rb
bs5-0.0.30 app/components/bs5/badge_component.rb
bs5-0.0.29 app/components/bs5/badge_component.rb
bs5-0.0.28 app/components/bs5/badge_component.rb
bs5-0.0.27 app/components/bs5/badge_component.rb
bs5-0.0.26 app/components/bs5/badge_component.rb
bs5-0.0.25 app/components/bs5/badge_component.rb
bs5-0.0.24 app/components/bs5/badge_component.rb
bs5-0.0.23 app/components/bs5/badge_component.rb
bs5-0.0.22 app/components/bs5/badge_component.rb
bs5-0.0.21 app/components/bs5/badge_component.rb
bs5-0.0.20 app/components/bs5/badge_component.rb