Class: Bootstrap5Helper::Offcanvas
- 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
-
#button(text_or_options = nil, opts = {}, &block) ⇒ String
Creates a button element to act as the trigger for the offcanvas component.
-
#content(&block) ⇒ String
Used to generate the main component.
-
#initialize(template, position_or_options = nil, opts = {}, &block) ⇒ Offcanvas
constructor
Class constructor.
-
#link(text_or_options = nil, opts = {}, &block) ⇒ String
Creates a simple link to toggle the offcanvas content.
-
#to_s ⇒ String
Renders the component as a String, but only to the output bugger.
-
#trigger(tag_or_options = nil, opts = {}, &block) ⇒ String
Used to make a custom dom element for a trigger.
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
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/bootstrap5_helper/offcanvas.rb', line 19 def initialize(template, = nil, opts = {}, &block) super(template) @pos, args = (, 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.
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 ( = nil, opts = {}, &block) text, args = (, 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 ) content_tag( :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.
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 |
#link(text_or_options = nil, opts = {}, &block) ⇒ String
Creates a simple link to toggle the offcanvas content.
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/bootstrap5_helper/offcanvas.rb', line 80 def link( = nil, opts = {}, &block) text, args = (, opts) args[:data] = args.fetch(:data, {}).merge!({ 'bs-toggle': 'offcanvas' }) args[:aria] = args.fetch(:aria, {}).merge!({ 'controls': @id }) = ["##{@id}", args] .prepend(text) if text.present? @template.link_to(*, &block) end |
#to_s ⇒ String
Was updated to return an empty string opposed to nil.
Renders the component as a String, but only to the output bugger.
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.
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( = nil, opts = {}, &block) tag, args = (, 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}" ) content_tag( tag || config({ offcanvas: :trigger }, :div), id: id, class: klass, data: data, aria: aria, &block ) end |