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
34
# File 'lib/bootstrap5_helper/modal.rb', line 21

def initialize(template, opts = {}, &block)
  super(template)

  @attrs      = opts
  @id         = @attrs.delete(:id)         { uuid }
  @class      = @attrs.delete(:class)      { '' }
  @data       = @attrs.delete(:data)       { {} }
  @scrollable = @attrs.delete(:scrollable) { false }
  @vcentered  = @attrs.delete(:vcentered)  { false }
  @static     = @attrs.delete(:static)     { false }
  @fullscreen = @attrs.delete(:fullscreen) { false }
  @size       = @attrs.delete(: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)


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

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)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/bootstrap5_helper/modal.rb', line 90

def close_button(opts = {})
  attrs = opts
  klass = attrs.delete(:class) { '' }
  data  = attrs.delete(:data)  { {} }
  aria  = attrs.delete(:aria)  { {} }

  (
    :button,
    {
      type:  'button',
      class: block_given? ? klass : 'btn-close',
      data:  data.merge('bs-dismiss' => 'modal'),
      aria:  aria.merge(label: 'Close')
    }.merge(attrs)
  ) 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)


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

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)


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

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)


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

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

#to_sString

String representation of the object.

Returns:

  • (String)


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/bootstrap5_helper/modal.rb', line 115

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
    }.merge(@attrs)
  ) do
    (
      :div,
      class: "modal-dialog #{size} #{scrollable} #{vcentered} #{fullscreen}",
      role:  'document'
    ) do
      (:div, class: 'modal-content') { @content.call(self) }
    end
  end
end