Sha256: 1d53a7c82ce685a78575cc3720dbd2ec167bd40ea3926fcef8611063b9c8c381

Contents?: true

Size: 1.47 KB

Versions: 30

Compression:

Stored size: 1.47 KB

Contents

module Coco
  class Stack < Coco::Component
    include Concerns::AcceptsOptions

    accepts_option :spacing, from: Coco::Spacer::SIZES, default: Coco::Spacer::DEFAULT, private: true
    accepts_option :direction, from: %i[vertical], default: :vertical

    renders_many :items, types: {
      element: ->(tag_name = :div, **kwargs) do
        with_space if items.any? && !ends_with_spacer?
        @_last_item_type = :element
        kwargs[:class] = class_names(kwargs[:class], "stack-item")
        Coco::Tag.new(tag_name, **kwargs)
      end,

      stack: ->(size = spacing, **kwargs) do
        with_space if items.any? && !ends_with_spacer?
        @_last_item_type = :stack
        kwargs[:class] = class_names(kwargs[:class], "stack-substack")
        Coco::Stack.new(spacing: size, **kwargs)
      end,

      spacer: ->(size = spacing, **kwargs) do
        @_starts_with_spacer = true if items.none?
        @_last_item_type = :spacer
        kwargs[:class] = class_names(kwargs[:class], "stack-spacer")
        Coco::Spacer.new(size:, **kwargs)
      end
    }

    def initialize(**)
      @_items = []
      @_starts_with_spacer = false
      @_last_item_type = nil
    end

    def spacing = get_option_value(:spacing)

    def starts_with_spacer? = @_starts_with_spacer

    def ends_with_spacer? = @_last_item_type == :spacer

    alias_method :with_item, :with_item_element
    alias_method :with_substack, :with_item_stack
    alias_method :with_space, :with_item_spacer
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
coveragebook_components-0.19.9 app/components/coco/layout/stack/stack.rb
coveragebook_components-0.19.8 app/components/coco/layout/stack/stack.rb
coveragebook_components-0.19.7 app/components/coco/layout/stack/stack.rb
coveragebook_components-0.19.6 app/components/coco/layout/stack/stack.rb
coveragebook_components-0.19.5 app/components/coco/layout/stack/stack.rb
coveragebook_components-0.19.4 app/components/coco/layout/stack/stack.rb
coveragebook_components-0.19.3 app/components/coco/layout/stack/stack.rb
coveragebook_components-0.19.2 app/components/coco/layout/stack/stack.rb
coveragebook_components-0.19.1 app/components/coco/layout/stack/stack.rb
coveragebook_components-0.19.0 app/components/coco/layout/stack/stack.rb
coveragebook_components-0.18.8 app/components/coco/layout/stack/stack.rb
coveragebook_components-0.18.7 app/components/coco/layout/stack/stack.rb
coveragebook_components-0.18.0 app/components/coco/layout/stack/stack.rb
coveragebook_components-0.17.7 app/components/coco/layout/stack/stack.rb
coveragebook_components-0.17.6 app/components/coco/layout/stack/stack.rb
coveragebook_components-0.17.5 app/components/coco/layout/stack/stack.rb
coveragebook_components-0.17.4 app/components/coco/layout/stack/stack.rb
coveragebook_components-0.17.3 app/components/coco/layout/stack/stack.rb
coveragebook_components-0.17.2 app/components/coco/layout/stack/stack.rb
coveragebook_components-0.17.1 app/components/coco/layout/stack/stack.rb