Class: Booties::Modal

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Utils
Defined in:
lib/booties/modal.rb

Overview

:nodoc:

Constant Summary collapse

DEFAULT_DISMISSAL =
'×'.html_safe
{ small: 'modal-sm', large: 'modal-lg' }.freeze

Instance Method Summary collapse

Methods included from Utils

merge_classes

Constructor Details

#initialize(template, id:, fade: true, size: nil) ⇒ Modal

Instantiates a new Modal. Several helper methods like #content_tag will be delegated to template. The required keyword id will be used as the DOM ID of the modal element. By default, the modal will exhibit fading behavior, but this can be disabled by setting fade to a falsey value.



19
20
21
22
23
24
# File 'lib/booties/modal.rb', line 19

def initialize(template, id:, fade: true, size: nil)
  @template = template
  @id = id
  @fade = fade ? 'fade' : nil
  @size = MODAL_SIZE[size]
end

Instance Method Details

#body(&block) ⇒ Object

Renders the main body of the modal. The content of the section is captured from block.



85
86
87
# File 'lib/booties/modal.rb', line 85

def body(&block)
   :div, capture(&block), class: 'modal-body'
end

#content(&block) ⇒ Object

Renders the content section of the modal. The content of the dialog is captured from block. The Modal object will be passed as a paramter to block.



52
53
54
55
56
# File 'lib/booties/modal.rb', line 52

def content(&block)
   :div, class: 'modal-content' do
    capture self, &block
  end
end

#dialog(&block) ⇒ Object

Renders the dialog section of the modal. block is passed to #content to fill in the content of the dialog.



42
43
44
45
46
# File 'lib/booties/modal.rb', line 42

def dialog(&block)
   :div, class: ['modal-dialog', @size] do
    content(&block)
  end
end

#dismiss(content = nil, **options, &block) ⇒ Object

Renders a button for dismissing the modal. The label can be passed in through the content parameter. Otherwise, it will be captured from block. Additional HTML attributes for the button can be passed in through options.



111
112
113
114
115
116
# File 'lib/booties/modal.rb', line 111

def dismiss(content = nil, **options, &block)
  content ||= capture(&block)
  options[:class] ||= 'close'
  options.update data: { dismiss: 'modal' }, type: 'button'
  button_tag content, options
end

Renders the footer of the modal. The content of the section is captured from block.



92
93
94
# File 'lib/booties/modal.rb', line 92

def footer(&block)
   :div, capture(&block), class: 'modal-footer'
end

#header(close: true, &block) ⇒ Object

TODO: rename close option to dismissible?



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/booties/modal.rb', line 70

def header(close: true, &block)
   :div, class: 'modal-header' do
    if close
      dismissal = t :'booties.modal.dismiss_html',
                    default: [:'booties.modal.dismiss', DEFAULT_DISMISSAL]
      dismiss(dismissal) + title(&block)
    else
      title(&block)
    end
  end
end

#render(**options, &block) ⇒ Object

Renders the top-level div for the modal dialog. block is passed to #dialog to fill in the content of the modal. @id is used as the DOM ID of the modal. @fade is used to include or exclude fading behavior.



32
33
34
35
36
37
# File 'lib/booties/modal.rb', line 32

def render(**options, &block)
  classes = merge_classes ['modal', @fade], options.delete(:class)
   :div, class: classes, id: @id, **options do
    dialog(&block)
  end
end

#title(content = nil, &block) ⇒ Object

Renders the title of the modal. The content can be passed through the content parameter. Otherwise it will be captured from block.

#title is called automatically from #header.



101
102
103
104
# File 'lib/booties/modal.rb', line 101

def title(content = nil, &block)
  content ||= capture(&block)
   :h4, content, class: 'modal-title'
end