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