Class: Bootstrap4Helper::Modal

Inherits:
Component show all
Defined in:
lib/bootstrap4_helper/modal.rb

Overview

Builds a Modal window component.

Instance Method Summary collapse

Methods inherited from Component

#capture, #concat, #config, #content_tag, #parse_arguments, #uuid

Constructor Details

#initialize(template, opts = {}, &block) ⇒ Modal

Class constructor

Parameters:

  • template (ActionView)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :id (String)
  • :class (String)
  • :data (Hash)
  • :scrollable (Boolean)
  • :vcentered (Boolean)
  • :size (Symbol)


17
18
19
20
21
22
23
24
25
26
27
# 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

Instance Method Details

#body(opts = {}, &block) ⇒ String

Builds the body component.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :id (String)
  • :class (String)
  • :data (Hash)

Returns:

  • (String)


49
50
51
# File 'lib/bootstrap4_helper/modal.rb', line 49

def body(opts = {}, &block)
  build_main_component :body, opts, &block
end

#close_button(opts = {}) ⇒ String

Builds a close button component.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :class (String)

Returns:

  • (String)


83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/bootstrap4_helper/modal.rb', line 83

def close_button(opts = {})
  klass = opts.fetch(:class, '')

  (
    :button,
    type: 'button',
    class: block_given? ? klass : 'close',
    data: { dismiss: 'modal' },
    aria: { label: 'Close' }
  ) do
    block_given? ? yield : xbutton
  end
end

Builds the footer component.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :id (String)
  • :class (String)
  • :data (Hash)

Returns:

  • (String)


61
62
63
# File 'lib/bootstrap4_helper/modal.rb', line 61

def footer(opts = {}, &block)
  build_main_component :footer, opts, &block
end

#header(opts = {}, &block) ⇒ String

Build the header component for the modal.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :id (String)
  • :class (String)
  • :data (Hash)

Returns:

  • (String)


37
38
39
# File 'lib/bootstrap4_helper/modal.rb', line 37

def header(opts = {}, &block)
  build_main_component :header, opts, &block
end

#title(opts = {}, &block) ⇒ String

Builds a title component.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :id (String)
  • :class (String)
  • :data (Hash)

Returns:

  • (String)


73
74
75
# File 'lib/bootstrap4_helper/modal.rb', line 73

def title(opts = {}, &block)
  build_sub_component :h5, :title, opts, &block
end

#to_sString

String representation of the object.

Returns:

  • (String)


101
102
103
104
105
106
107
# File 'lib/bootstrap4_helper/modal.rb', line 101

def to_s
   :div, id: @id, class: "modal #{@class}", tabindex: -1, role: 'dialog', data: @data do
     :div, class: "modal-dialog #{size} #{scrollable} #{vcentered}", role: 'document' do
      (:div, class: 'modal-content') { @content.call(self) }
    end
  end
end