Class: Bootstrap5Helper::Modal

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

Overview

Builds a Modal window component.

Instance Method Summary collapse

Methods inherited from Component

#capture, #concat, #config, #content_tag, #parse_arguments, #parse_context_or_options, #parse_tag_or_options, #parse_text_or_options, #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)
  • :static (Boolean)
  • :fullscreen (Boolean|Symbol)
  • :size (Symbol)


21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bootstrap5_helper/modal.rb', line 21

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)
  @static     = opts.fetch(:static,     false)
  @fullscreen = opts.fetch(:fullscreen, 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)


55
56
57
# File 'lib/bootstrap5_helper/modal.rb', line 55

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)


89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/bootstrap5_helper/modal.rb', line 89

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

  (
    :button,
    type:  'button',
    class: block_given? ? klass : 'btn-close',
    data:  { 'bs-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)


67
68
69
# File 'lib/bootstrap5_helper/modal.rb', line 67

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)


43
44
45
# File 'lib/bootstrap5_helper/modal.rb', line 43

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)


79
80
81
# File 'lib/bootstrap5_helper/modal.rb', line 79

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

#to_sString

String representation of the object.

Returns:

  • (String)


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/bootstrap5_helper/modal.rb', line 109

def to_s
  @data.merge!('bs-backdrop' => 'static', 'bs-keyboard' => false) if @static

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