Sha256: ccdc470dae5d4622d0be8577c22615b412faa047c67565649e2e7423b5bb3085

Contents?: true

Size: 1.64 KB

Versions: 20

Compression:

Stored size: 1.64 KB

Contents

module Coco
  class Modal < Coco::Component
    include Concerns::AcceptsOptions
    include Coco::ModalHelper
    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: ->(id: nil, **kwargs, &block) do
        @container_content = block
        @container_id = id
        @container_type = :dialog
        Coco::ModalDialog.new(dismissable: get_option_value(:dismissable), id:, **kwargs)
      end,

      lightbox: ->(id: nil, **kwargs, &block) do
        @container_type = :lightbox
        @container_id = id
        Coco::ModalLightbox.new(dismissable: get_option_value(:dismissable), id:, **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, :container_type

    def initialize(name:, show: false, **kwargs)
      @name = name
      @show = show
      @container_type = nil
      with_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

20 entries across 20 versions & 1 rubygems

Version Path
coveragebook_components-0.19.4 app/components/coco/modals/modal/modal.rb
coveragebook_components-0.19.3 app/components/coco/modals/modal/modal.rb
coveragebook_components-0.19.2 app/components/coco/modals/modal/modal.rb
coveragebook_components-0.19.1 app/components/coco/modals/modal/modal.rb
coveragebook_components-0.19.0 app/components/coco/modals/modal/modal.rb
coveragebook_components-0.18.8 app/components/coco/modals/modal/modal.rb
coveragebook_components-0.18.7 app/components/coco/modals/modal/modal.rb
coveragebook_components-0.18.0 app/components/coco/modals/modal/modal.rb
coveragebook_components-0.17.7 app/components/coco/modals/modal/modal.rb
coveragebook_components-0.17.6 app/components/coco/modals/modal/modal.rb
coveragebook_components-0.17.5 app/components/coco/modals/modal/modal.rb
coveragebook_components-0.17.4 app/components/coco/modals/modal/modal.rb
coveragebook_components-0.17.3 app/components/coco/modals/modal/modal.rb
coveragebook_components-0.17.2 app/components/coco/modals/modal/modal.rb
coveragebook_components-0.17.1 app/components/coco/modals/modal/modal.rb
coveragebook_components-0.17.0 app/components/coco/modals/modal/modal.rb
coveragebook_components-0.16.0 app/components/coco/modals/modal/modal.rb
coveragebook_components-0.15.0 app/components/coco/modals/modal/modal.rb
coveragebook_components-0.14.0 app/components/coco/modals/modal/modal.rb
coveragebook_components-0.13.1 app/components/coco/modals/modal/modal.rb