Class: Bootstrap5Helper::Offcanvas::Content

Inherits:
Component
  • Object
show all
Defined in:
lib/bootstrap5_helper/offcanvas/content.rb

Overview

Builds a Content component for use in offcanvas.

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) ⇒ ClassName

Parameters:

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


10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bootstrap5_helper/offcanvas/content.rb', line 10

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

  @attrs      = opts
  @id         = @attrs.delete(:id)         { uuid }
  @class      = @attrs.delete(:class)      { '' }
  @data       = @attrs.delete(:data)       { {} }
  @aria       = @attrs.delete(:aria)       { {} }
  @scrollable = @attrs.delete(:scrollable) { false }
  @position   = @attrs.delete(:position)   { 'start' }
  @backdrop   = @attrs.delete(:backdrop)   { true }
  @content    = block || proc { '' }
end

Instance Method Details

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

TODO:


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/bootstrap5_helper/offcanvas/content.rb', line 80

def body(opts = {}, &block)
  attrs = opts
  id    = attrs.delete(:id)    { nil }
  klass = attrs.delete(:class) { '' }
  data  = attrs.delete(:data)  { {} }
  aria  = attrs.delete(:aria)  { {} }

  (
    :div,
    {
      id:    id,
      class: "offcanvas-body #{klass}",
      data:  data,
      aria:  aria
    }.merge(attrs),
    &block
  )
end

#close_button(opts = {}) ⇒ Object

TODO:


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/bootstrap5_helper/offcanvas/content.rb', line 63

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

  (
    config({ offcanvas: :close }, :button),
    class: block_given? ? klass : 'btn-close',
    data:  data.merge('bs-dismiss' => 'offcanvas'),
    aria:  aria.merge(label: 'Close')
  ) do
    block_given? ? yield : xbutton
  end
end

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

TODO:


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bootstrap5_helper/offcanvas/content.rb', line 25

def header(opts = {}, &block)
  attrs = opts
  id    = attrs.delete(:id)    { nil }
  klass = attrs.delete(:class) { '' }
  data  = attrs.delete(:data)  { {} }

  (
    config({ offcanvas: :header }, :div),
    {
      id:    id,
      class: "offcanvas-header #{klass}",
      data:  data
    }.merge(attrs),
    &block
  )
end

#title(text_or_options = nil, opts = {}, &block) ⇒ Object

TODO:


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bootstrap5_helper/offcanvas/content.rb', line 43

def title(text_or_options = nil, opts = {}, &block)
  text, attrs = parse_text_or_options(text_or_options, opts)

  id    = attrs.delete(:id)    { nil }
  klass = attrs.delete(:class) { '' }
  data  = attrs.delete(:data)  { {} }

  (
    config({ offcanvas: :title }, :h6),
    text,
    {
      id:    id,
      class: "offcanvas-title #{klass}",
      data:  data
    }.merge(attrs),
    &block
  )
end

#to_sObject

TODO:


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/bootstrap5_helper/offcanvas/content.rb', line 100

def to_s
  (
    :div,
    {
      id:       @id,
      class:    "offcanvas offcanvas-#{@position} #{@class}",
      tabindex: -1,
      data:     @data.merge!(
        'bs-scroll'   => @scrollable,
        'bs-backdrop' => @backdrop
      ),
      aria:     @aria.merge!(labelledby: 'offcanvasExampleLabel')
    }.merge(@attrs)
  ) do
    @content.call(self)
  end
end