class Bootstrap4Helper::Modal
Builds a Modal
window component.
Public Class Methods
Class constructor
@param [ActionView] template @param [Hash] opts @option opts [String] :id @option opts [String] :class @option opts [Hash] :data @option opts [Boolean] :scrollable @option opts [Boolean] :vcentered @option opts [Symbol] :size
Bootstrap4Helper::Component::new
# File lib/bootstrap4_helper/modal.rb, line 17 def initialize(template, opts = {}, &block) super(template) @id = opts.fetch(:id, uuid) @class = opts.fetch(:class, '') @data = opts.fetch(:data, {}) @scrollable = opts.fetch(:scrollable, false) @vcentered = opts.fetch(:vcentered, false) @size = opts.fetch(:size, nil) @content = block || proc { '' } end
Public Instance Methods
Builds the body component.
@param [Hash] opts @option opts [String] :id @option opts [String] :class @option opts [Hash] :data @return [String]
# File lib/bootstrap4_helper/modal.rb, line 49 def body(opts = {}, &block) build_main_component :body, opts, &block end
Build the header component for the modal.
@param [Hash] opts @option opts [String] :id @option opts [String] :class @option opts [Hash] :data @return [String]
# File lib/bootstrap4_helper/modal.rb, line 37 def header(opts = {}, &block) build_main_component :header, opts, &block end
Builds a title component.
@param [Hash] opts @option opts [String] :id @option opts [String] :class @option opts [Hash] :data @return [String]
# File lib/bootstrap4_helper/modal.rb, line 73 def title(opts = {}, &block) build_sub_component :h5, :title, opts, &block end
String representation of the object.
@return [String]
# File lib/bootstrap4_helper/modal.rb, line 101 def to_s content_tag :div, id: @id, class: "modal #{@class}", tabindex: -1, role: 'dialog', data: @data do content_tag :div, class: "modal-dialog #{size} #{scrollable} #{vcentered}", role: 'document' do content_tag(:div, class: 'modal-content') { @content.call(self) } end end end
Private Instance Methods
Used to build main components, usually divs.
@param [Symbol|String] type @param [Hash] opts @return [String]
# File lib/bootstrap4_helper/modal.rb, line 117 def build_main_component(type, opts = {}, &block) build_sub_component :div, type, opts, &block end
Used to build more specific components.
@param [Symbol] tag @param [Symbol|String] type @param [Hash] opts @option opts [String] :id @option opts [String] :class @option opts [Hash] :data @return [String]
# File lib/bootstrap4_helper/modal.rb, line 131 def build_sub_component(tag, type, opts = {}, &block) id = opts.fetch(:id, nil) klass = opts.fetch(:class, '') data = opts.fetch(:data, {}) content_tag( tag, id: id, class: "modal-#{type} #{klass}", data: data, &block ) end
Gets the scrollable CSS class.
@return [String]
# File lib/bootstrap4_helper/modal.rb, line 157 def scrollable @scrollable ? 'modal-dialog-scrollable' : '' end
Gets the size of the modal window.
@return [String]
# File lib/bootstrap4_helper/modal.rb, line 173 def size case @size when :xlarge 'modal-xl' when :large 'modal-lg' when :small 'modal-sm' else '' end end
Gets the vertical-center CSS class.
@return [String]
# File lib/bootstrap4_helper/modal.rb, line 165 def vcentered @vcentered ? 'modal-dialog-centered' : '' end