Sha256: 1955e0632b35807d2290ea0b5df3186548f22b532c27c0673f29cf785414d0b0

Contents?: true

Size: 1.6 KB

Versions: 8

Compression:

Stored size: 1.6 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: ->(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
      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

8 entries across 8 versions & 1 rubygems

Version Path
coveragebook_components-0.12.2 app/components/coco/shared/modal/modal.rb
coveragebook_components-0.12.1 app/components/coco/shared/modal/modal.rb
coveragebook_components-0.12.0 app/components/coco/shared/modal/modal.rb
coveragebook_components-0.11.0 app/components/coco/shared/modal/modal.rb
coveragebook_components-0.10.1.beta.2 app/components/coco/shared/modal/modal.rb
coveragebook_components-0.10.1.beta.1 app/components/coco/shared/modal/modal.rb
coveragebook_components-0.10.1.beta.0 app/components/coco/shared/modal/modal.rb
coveragebook_components-0.10.0 app/components/coco/base/modal/modal.rb