Class: Card::Bootstrap::Component::Carousel
- Inherits:
-
Component
- Object
- Component
- Card::Bootstrap::Component::Carousel
- Defined in:
- lib/card/bootstrap/component/carousel.rb
Overview
support class for bootstrap carousels
Instance Method Summary collapse
- #carousel(id, active_index) ⇒ Object
- #carousel_item(item, html_opts) ⇒ Object
- #carousel_item_opts(index) ⇒ Object
- #control_button(direction, description) ⇒ Object
- #control_next ⇒ Object
- #control_prev ⇒ Object
- #indicator(index) ⇒ Object
- #indicators ⇒ Object
- #item(content = nil, &block) ⇒ Object
- #items ⇒ Object
- #render_content ⇒ Object
Instance Method Details
#carousel(id, active_index) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/card/bootstrap/component/carousel.rb', line 10 def carousel(id, active_index, &) @id = id @active_item_index = active_index @items = [] instance_exec(&) @html.div class: "carousel slide", id: id, "data-bs-ride" => "true" do indicators items control_prev control_next end end |
#carousel_item(item, html_opts) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/card/bootstrap/component/carousel.rb', line 42 def carousel_item item, html_opts @html.div html_opts do item = item.call if item.respond_to?(:call) @html << item if item.is_a?(String) end end |
#carousel_item_opts(index) ⇒ Object
36 37 38 39 40 |
# File 'lib/card/bootstrap/component/carousel.rb', line 36 def carousel_item_opts index { class: "carousel-item" }.tap do |opts| add_class opts, "active" if index == @active_item_index end end |
#control_button(direction, description) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/card/bootstrap/component/carousel.rb', line 57 def direction, description @html. class: "carousel-control-#{direction}", "data-bs-target": "##{@id}", type: "button" do @html.span class: "carousel-control-#{direction}-icon", "aria-hidden": "true" do "" end @html.span description, class: "visually-hidden" end end |
#control_next ⇒ Object
53 54 55 |
# File 'lib/card/bootstrap/component/carousel.rb', line 53 def control_next :next, "Next" end |
#control_prev ⇒ Object
49 50 51 |
# File 'lib/card/bootstrap/component/carousel.rb', line 49 def control_prev :prev, "Previous" end |
#indicator(index) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/card/bootstrap/component/carousel.rb', line 74 def indicator index html_opts = { "data-bs-slide-to": index, "data-bs-target": "##{@id}", type: "button", "aria-label": "Slide #{index + 1}" } add_class html_opts, "active" if index == @active_item_index @html. html_opts end |
#indicators ⇒ Object
68 69 70 71 72 |
# File 'lib/card/bootstrap/component/carousel.rb', line 68 def indicators @html.div class: "carousel-indicators" do @items.size.times { |i| indicator i } end end |
#item(content = nil, &block) ⇒ Object
24 25 26 |
# File 'lib/card/bootstrap/component/carousel.rb', line 24 def item content=nil, &block @items << (content || block) end |
#items ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/card/bootstrap/component/carousel.rb', line 28 def items @html.div class: "carousel-inner" do @items.each_with_index do |item, index| carousel_item item, carousel_item_opts(index) end end end |
#render_content ⇒ Object
6 7 8 |
# File 'lib/card/bootstrap/component/carousel.rb', line 6 def render_content carousel(*@args, &@build_block) end |