Class: Bootstrap5Helper::Offcanvas

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

Overview

Builds a Offcanvas component.

Defined Under Namespace

Classes: Content

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, position_or_options = nil, opts = {}, &block) ⇒ Offcanvas

Class constructor

Parameters:

  • (ActionView)
  • position_or_options (NilClass|Symbol|Hash) (defaults to: nil)
    • :start, :end, :top, :bottom

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

Options Hash (opts):

  • :id (String)
  • :class (String)
  • :data (Hash)
  • :aria (Hash)
  • :scrollable (Boolean)
  • :backdrop (Boolean|String)
    • true, false, static



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bootstrap5_helper/offcanvas.rb', line 19

def initialize(template, position_or_options = nil, opts = {}, &block)
  super(template)
  @pos, args  = parse_position_or_options(position_or_options, opts)
  @id         = args.fetch(:id,         uuid)
  @class      = args.fetch(:class,      '')
  @data       = args.fetch(:data,       {})
  @aria       = args.fetch(:aria,       {})
  @scrollable = args.fetch(:scrollable, false)
  @backdrop   = args.fetch(:backdrop,   true)
  @content    = block || proc { '' }
end

Instance Method Details

#button(text_or_options = nil, opts = {}, &block) ⇒ String

Creates a button element to act as the trigger for the offcanvas component.

Parameters:

  • text_or_options (NilClass|String|Hash) (defaults to: nil)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

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

Returns:

  • (String)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bootstrap5_helper/offcanvas.rb', line 42

def button(text_or_options = nil, opts = {}, &block)
  text, args = parse_text_or_options(text_or_options, opts)

  id    = args.fetch(:id,    nil)
  klass = args.fetch(:class, '')

  data = args.fetch(:data, {}).merge!(
    'bs-toggle' => 'offcanvas',
    'bs-target' => "##{@id}"
  )

  aria = args.fetch(:aria, {}).merge!(
    'controls' => @id
  )

  (
    :button,
    text,
    type:  :button,
    id:    id,
    class: klass,
    data:  data,
    aria:  aria,
    &block
  )
end

#content(&block) ⇒ String

Used to generate the main component. This class serves as a wrapper, so that buttons and links have reference to the content component.

Returns:

  • (String)


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/bootstrap5_helper/offcanvas.rb', line 131

def content(&block)
  Content.new(
    @template,
    {
      id:         @id,
      class:      @class,
      data:       @data,
      aria:       @aria,
      scrollable: @scrollable,
      backdrop:   @backdrop,
      position:   @pos
    },
    &block
  )
end

Creates a simple link to toggle the offcanvas content.

Parameters:

  • text_or_options (String|Hash|NilClass) (defaults to: nil)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

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

Returns:

  • (String)


80
81
82
83
84
85
86
87
88
89
# File 'lib/bootstrap5_helper/offcanvas.rb', line 80

def link(text_or_options = nil, opts = {}, &block)
  text, args  = parse_text_or_options(text_or_options, opts)
  args[:data] = args.fetch(:data,  {}).merge!({ 'bs-toggle': 'offcanvas' })
  args[:aria] = args.fetch(:aria,  {}).merge!({ 'controls': @id })

  options = ["##{@id}", args]
  options.prepend(text) if text.present?

  @template.link_to(*options, &block)
end

#to_sString

Note:

Was updated to return an empty string opposed to nil.

Renders the component as a String, but only to the output bugger.

Returns:

  • (String)

See Also:

  • Bootstrap5Helper::Offcanvas.changelogchangelog.md


154
155
156
157
158
# File 'lib/bootstrap5_helper/offcanvas.rb', line 154

def to_s
  @content.call(self)

  ''
end

#trigger(tag_or_options = nil, opts = {}, &block) ⇒ String

Used to make a custom dom element for a trigger. Use this when the trigger isnt a link or button.

Parameters:

  • tag_or_options (Symbol|Hash|NilClass) (defaults to: nil)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

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

Returns:

  • (String)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/bootstrap5_helper/offcanvas.rb', line 104

def trigger(tag_or_options = nil, opts = {}, &block)
  tag, args = parse_tag_or_options(tag_or_options, opts)

  id    = args.fetch(:id,    nil)
  klass = args.fetch(:class, '')
  aria  = args.fetch(:aria,  {}).merge!({ 'controls': @id })
  data  = args.fetch(:data,  {}).merge!(
    'bs-toggle': 'offcanvas',
    'bs-target': "##{@id}"
  )

  (
    tag || config({ offcanvas: :trigger }, :div),
    id:    id,
    class: klass,
    data:  data,
    aria:  aria,
    &block
  )
end