Sha256: e758faf187671c51909c7d621db765c4a554c78fbe89bf0badf437447a358f2e

Contents?: true

Size: 1.93 KB

Versions: 7

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

module PandaCms
  module Admin
    class ButtonComponent < ViewComponent::Base
      attr_accessor :text, :action, :link, :icon, :size, :data

      def initialize(text: "Button", action: nil, data: {}, link: "#", icon: nil, size: :regular)
        @text = text
        @action = action
        @data = data
        @link = link
        @icon = icon
        @size = size
      end

      def call
        @icon = set_icon_from_action(@action) if @action && @icon.nil?
        icon = content_tag(:i, "", class: "mr-2 fa-regular fa-#{@icon}") if @icon
        @text = "#{icon} #{@text.titleize}".html_safe

        classes = "inline-flex items-center rounded-md font-medium shadow-sm focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 "

        case @size
        when :small, :sm
          classes += "gap-x-1.5 px-2.5 py-1.5 text-sm "
        when :medium, :regular, :md
          classes += "gap-x-1.5 px-3 py-2 text-base "
        when :large, :lg
          classes += "gap-x-2 px-3.5 py-2.5 text-lg "
        end

        classes += case @action
        when :save, :create
          "text-white bg-active"
        when :secondary
          "text-highlight border border-highlight bg-white hover:bg-sky-100 focus-visible:outline-highlight "
        when :delete, :destroy, :danger
          "text-error border border-error bg-red-100 hover:bg-red-200 hover:text-error focus-visible:outline-red-300 "
        else
          "text-dark border-2 border-dark bg-transparent hover:bg-light transition-all "
        end

        content_tag :a, href: @link, class: classes, data: @data do
          @text
        end
      end

      private

      def set_icon_from_action(action)
        case action
        when :add, :new, :create
          "plus"
        when :save
          "check"
        when :edit, :update
          "pencil"
        when :delete, :destroy
          "trash"
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
panda_cms-0.5.10 app/components/panda_cms/admin/button_component.rb
panda_cms-0.5.9 app/components/panda_cms/admin/button_component.rb
panda_cms-0.5.8 app/components/panda_cms/admin/button_component.rb
panda_cms-0.5.7 app/components/panda_cms/admin/button_component.rb
panda_cms-0.5.6 app/components/panda_cms/admin/button_component.rb
panda_cms-0.5.5 app/components/panda_cms/admin/button_component.rb
panda_cms-0.5.4 app/components/panda_cms/admin/button_component.rb