Sha256: 064503e9eff0aa23beba0363b0afffd27a73c25422f3ec7347070bafcf401ff7

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module Optics
  module Button
    class Component < ApplicationViewComponent
      SIZES = %w[small medium large].freeze
      STYLES = %w[default primary secondary delete].freeze

      renders_one :leading_icon, lambda { |name:, size: 'normal'|
        Optics::Icon::Component.new(name:, size:)
      }

      accepts :label
      accepts :border, default: true
      accepts :icon, default: false
      accepts :size, default: 'medium'
      accepts :variant, default: 'default'
      accepts :url

      def call
        build_button do
          leading_icon
          label
        end
      end

      def build_button(&)
        return link_to(url, class: classes, **@attributes, &) if url

        tag.button(class: classes, **@attributes, &)
      end

      def button_class
        return 'btn' if variant == 'default'

        "btn-#{variant}"
      end

      def classes
        class_names(
          button_class,
          size_class,
          'btn--icon': icon,
          'btn--no-border': !border
        ).join(' ')
      end

      def size_class
        return if size == 'medium'

        "btn--#{size}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
optics_view_components-0.1.1 app/components/optics/button/component.rb