class Bootstrap4Helper::Modal

Builds a Modal window component.

Public Class Methods

new(template, opts = {}, &block) click to toggle source

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

Calls superclass method 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

body(opts = {}, &block) click to toggle source

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
close_button(opts = {}) { |: xbutton| ... } click to toggle source

Builds a close button component.

@param [Hash] opts @option opts [String] :class @return [String]

# File lib/bootstrap4_helper/modal.rb, line 83
def close_button(opts = {})
  klass = opts.fetch(:class, '')

  content_tag(
    :button,
    type: 'button',
    class: block_given? ? klass : 'close',
    data: { dismiss: 'modal' },
    aria: { label: 'Close' }
  ) do
    block_given? ? yield : xbutton
  end
end
header(opts = {}, &block) click to toggle source

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
title(opts = {}, &block) click to toggle source

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
to_s() click to toggle source

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

build_main_component(type, opts = {}, &block) click to toggle source

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
build_sub_component(tag, type, opts = {}, &block) click to toggle source

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
scrollable() click to toggle source

Gets the scrollable CSS class.

@return [String]

# File lib/bootstrap4_helper/modal.rb, line 157
def scrollable
  @scrollable ? 'modal-dialog-scrollable' : ''
end
size() click to toggle source

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
vcentered() click to toggle source

Gets the vertical-center CSS class.

@return [String]

# File lib/bootstrap4_helper/modal.rb, line 165
def vcentered
  @vcentered ? 'modal-dialog-centered' : ''
end
xbutton() click to toggle source

Builds the `x` button normally used in the header.

@return [String]

# File lib/bootstrap4_helper/modal.rb, line 149
def xbutton
  content_tag :span, '×'.html_safe, aria: { hidden: true }
end