Sha256: 6cd1e1e22b934b8f6911ec9b831ff4aabadbb24fc3fe06c3a4b666bde17f3fdb

Contents?: true

Size: 1.41 KB

Versions: 15

Compression:

Stored size: 1.41 KB

Contents

module Coco
  class Modal < Coco::Component
    include Concerns::AcceptsOptions
    include Turbo::FramesHelper

    accepts_option :dismissable, from: [true, false], default: true
    accepts_option :close_on_submit, from: [true, false], default: true

    renders_one :title

    renders_one :container, types: {
      dialog: ->(**kwargs, &block) do
        @container_content = block
        Coco::ModalDialog.new(dismissable: get_option_value(:dismissable), **kwargs)
      end,

      lightbox: ->(**kwargs, &block) do
        Coco::ModalLightbox.new(dismissable: get_option_value(:dismissable), **kwargs)
      end
    }

    before_render do
      if container? && title? && container.respond_to?(:with_title)
        container.with_title { title.to_s }
      end
    end

    attr_reader :name, :show

    def initialize(name:, show: false, **kwargs)
      @name = name
      @show = show
      title { kwargs[:title] } if kwargs[:title]
    end

    def render_as_modal?
      request.headers["Turbo-Frame"].present? || show
    end

    def id
      "coco-modal-#{name.to_s.dasherize}"
    end

    def modal_content
      container? ? container : content
    end

    def content
      @container_content.present? ? @container_content.call : super
    end

    def render_flash_messages
      if helpers.respond_to?(:render_turbo_stream_flash_messages)
        helpers.render_turbo_stream_flash_messages
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
coveragebook_components-0.8.8 app/components/coco/base/modal/modal.rb
coveragebook_components-0.8.7 app/components/coco/base/modal/modal.rb
coveragebook_components-0.8.6 app/components/coco/base/modal/modal.rb
coveragebook_components-0.8.5 app/components/coco/base/modal/modal.rb
coveragebook_components-0.8.4 app/components/coco/base/modal/modal.rb
coveragebook_components-0.8.3 app/components/coco/base/modal/modal.rb
coveragebook_components-0.8.2 app/components/coco/base/modal/modal.rb
coveragebook_components-0.8.1 app/components/coco/base/modal/modal.rb
coveragebook_components-0.8.0 app/components/coco/base/modal/modal.rb
coveragebook_components-0.8.0.beta.3 app/components/coco/base/modal/modal.rb
coveragebook_components-0.8.0.beta.2 app/components/coco/base/modal/modal.rb
coveragebook_components-0.8.0.beta.1 app/components/coco/base/modal/modal.rb
coveragebook_components-0.7.10 app/components/coco/base/modal/modal.rb
coveragebook_components-0.7.9 app/components/coco/base/modal/modal.rb
coveragebook_components-0.7.8 app/components/coco/base/modal/modal.rb