bs5 0.0.10 → 0.0.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/app/components/bs5/accordion_component.html.erb +4 -11
  4. data/app/components/bs5/accordion_component.rb +1 -0
  5. data/app/components/bs5/alert_component.html.erb +1 -1
  6. data/app/components/bs5/button_tag_component.rb +1 -5
  7. data/app/components/bs5/button_to_component.rb +109 -0
  8. data/app/components/bs5/list_group_component.html.erb +17 -0
  9. data/app/components/bs5/list_group_component.rb +143 -0
  10. data/app/controllers/bs5/pages_controller.rb +11 -0
  11. data/app/helpers/bs5/components_helper.rb +14 -1
  12. data/app/service/bs5/collapse_service.rb +47 -0
  13. data/app/views/bs5/examples/accordion/default/_example.html.erb +1 -1
  14. data/app/views/bs5/examples/accordion/flush/_example.html.erb +1 -1
  15. data/app/views/bs5/examples/alert/additional_content/_example.html.erb +1 -1
  16. data/app/views/bs5/examples/alert/default/_example.html.erb +1 -1
  17. data/app/views/bs5/examples/alert/dismissable/_example.html.erb +1 -1
  18. data/app/views/bs5/examples/alert/style/_example.html.erb +1 -1
  19. data/app/views/bs5/examples/badge/default/_example.html.erb +1 -1
  20. data/app/views/bs5/examples/badge/pill/_example.html.erb +1 -1
  21. data/app/views/bs5/examples/badge/style/_example.html.erb +1 -1
  22. data/app/views/bs5/examples/breadcrumb/default/_example.html.erb +1 -1
  23. data/app/views/bs5/examples/buttons/button_tag/block_buttons/_example.html.erb +5 -0
  24. data/app/views/bs5/examples/buttons/{block_buttons → button_tag/block_buttons}/block_buttons.html.erb +0 -0
  25. data/app/views/bs5/examples/buttons/{block_buttons → button_tag/block_buttons}/responsive_block_buttons_1.html.erb +0 -0
  26. data/app/views/bs5/examples/buttons/{block_buttons → button_tag/block_buttons}/responsive_block_buttons_2.html.erb +0 -0
  27. data/app/views/bs5/examples/buttons/{block_buttons → button_tag/block_buttons}/responsive_block_buttons_3.html.erb +0 -0
  28. data/app/views/bs5/examples/buttons/button_tag/default/_example.html.erb +2 -0
  29. data/app/views/bs5/examples/buttons/button_tag/default/snippet.html.erb +15 -0
  30. data/app/views/bs5/examples/buttons/button_tag/outline/_example.html.erb +2 -0
  31. data/app/views/bs5/examples/buttons/button_tag/outline/snippet.html.erb +19 -0
  32. data/app/views/bs5/examples/buttons/button_tag/size/_example.html.erb +3 -0
  33. data/app/views/bs5/examples/buttons/button_tag/size/large.html.erb +3 -0
  34. data/app/views/bs5/examples/buttons/button_tag/size/small.html.erb +3 -0
  35. data/app/views/bs5/examples/buttons/button_tag/style/_example.html.erb +2 -0
  36. data/app/views/bs5/examples/buttons/button_tag/style/snippet.html.erb +19 -0
  37. data/app/views/bs5/examples/buttons/button_tag/toggle_states/_example.html.erb +2 -0
  38. data/app/views/bs5/examples/buttons/{toggle_states → button_tag/toggle_states}/snippet.html.erb +0 -0
  39. data/app/views/bs5/examples/buttons/button_to/default/_example.html.erb +2 -0
  40. data/app/views/bs5/examples/buttons/button_to/default/snippet.html.erb +11 -0
  41. data/app/views/bs5/examples/close_button/default/_example.html.erb +1 -1
  42. data/app/views/bs5/examples/close_button/disabled/_example.html.erb +1 -1
  43. data/app/views/bs5/examples/close_button/white/_example.html.erb +1 -1
  44. data/app/views/bs5/examples/collapse/default/_example.html.erb +4 -0
  45. data/app/views/bs5/examples/collapse/default/multiple_targets.html.erb +21 -0
  46. data/app/views/bs5/examples/collapse/default/snippet.html.erb +9 -0
  47. data/app/views/bs5/examples/list_group/actionable/_example.html.erb +3 -0
  48. data/app/views/bs5/examples/list_group/actionable/button.html.erb +7 -0
  49. data/app/views/bs5/examples/list_group/actionable/snippet.html.erb +7 -0
  50. data/app/views/bs5/examples/list_group/active/_example.html.erb +2 -0
  51. data/app/views/bs5/examples/list_group/active/snippet.html.erb +7 -0
  52. data/app/views/bs5/examples/list_group/checkboxes_and_radios/_example.html.erb +3 -0
  53. data/app/views/bs5/examples/list_group/checkboxes_and_radios/default.html.erb +22 -0
  54. data/app/views/bs5/examples/list_group/checkboxes_and_radios/with_labels.html.erb +32 -0
  55. data/app/views/bs5/examples/list_group/custom_content/_example.html.erb +2 -0
  56. data/app/views/bs5/examples/list_group/custom_content/default.html.erb +32 -0
  57. data/app/views/bs5/examples/list_group/default/_example.html.erb +2 -0
  58. data/app/views/bs5/examples/list_group/default/snippet.html.erb +7 -0
  59. data/app/views/bs5/examples/list_group/disabled/_example.html.erb +2 -0
  60. data/app/views/bs5/examples/list_group/disabled/snippet.html.erb +7 -0
  61. data/app/views/bs5/examples/list_group/flush/_example.html.erb +2 -0
  62. data/app/views/bs5/examples/list_group/flush/snippet.html.erb +7 -0
  63. data/app/views/bs5/examples/list_group/horizontal/_example.html.erb +2 -0
  64. data/app/views/bs5/examples/list_group/horizontal/snippet.html.erb +30 -0
  65. data/app/views/bs5/examples/list_group/style/_example.html.erb +3 -0
  66. data/app/views/bs5/examples/list_group/style/actionable.html.erb +11 -0
  67. data/app/views/bs5/examples/list_group/style/default.html.erb +11 -0
  68. data/app/views/bs5/examples/list_group/with_badges/_example.html.erb +2 -0
  69. data/app/views/bs5/examples/list_group/with_badges/default.html.erb +14 -0
  70. data/app/views/bs5/examples/tooltips/default/_example.html.erb +4 -0
  71. data/app/views/bs5/examples/tooltips/default/buttons.html.erb +9 -0
  72. data/app/views/bs5/examples/tooltips/default/disabled_elements.html.erb +3 -0
  73. data/app/views/bs5/examples/tooltips/default/snippet.html.erb +7 -0
  74. data/app/views/bs5/pages/accordion.html.erb +3 -0
  75. data/app/views/bs5/{examples/_alert.html.erb → pages/alert.html.erb} +1 -1
  76. data/app/views/bs5/{examples/_badge.html.erb → pages/badge.html.erb} +1 -1
  77. data/app/views/bs5/pages/breadcrumb.html.erb +2 -0
  78. data/app/views/bs5/pages/buttons.html.erb +11 -0
  79. data/app/views/bs5/{examples/_close_button.html.erb → pages/close_button.html.erb} +1 -1
  80. data/app/views/bs5/pages/collapse.html.erb +2 -0
  81. data/app/views/bs5/pages/list_group.html.erb +11 -0
  82. data/app/views/bs5/pages/tooltips.html.erb +2 -0
  83. data/app/views/layouts/bs5/application.html.erb +1 -0
  84. data/app/views/layouts/bs5/pages.html.erb +34 -0
  85. data/config/locales/en.yml +6 -0
  86. data/config/routes.rb +2 -2
  87. data/lib/bs5/version.rb +1 -1
  88. data/lib/generators/bs5/install/install_generator.rb +14 -5
  89. data/lib/generators/bs5/install/templates/bs5.js +14 -0
  90. metadata +80 -26
  91. data/app/controllers/bs5/examples_controller.rb +0 -9
  92. data/app/views/bs5/examples/_accordion.html.erb +0 -3
  93. data/app/views/bs5/examples/_breadcrumb.html.erb +0 -2
  94. data/app/views/bs5/examples/_buttons.html.erb +0 -7
  95. data/app/views/bs5/examples/buttons/block_buttons/_example.html.erb +0 -5
  96. data/app/views/bs5/examples/buttons/default/_example.html.erb +0 -2
  97. data/app/views/bs5/examples/buttons/default/snippet.html.erb +0 -15
  98. data/app/views/bs5/examples/buttons/outline/_example.html.erb +0 -2
  99. data/app/views/bs5/examples/buttons/outline/snippet.html.erb +0 -19
  100. data/app/views/bs5/examples/buttons/size/_example.html.erb +0 -3
  101. data/app/views/bs5/examples/buttons/size/large.html.erb +0 -3
  102. data/app/views/bs5/examples/buttons/size/small.html.erb +0 -3
  103. data/app/views/bs5/examples/buttons/style/_example.html.erb +0 -2
  104. data/app/views/bs5/examples/buttons/style/snippet.html.erb +0 -19
  105. data/app/views/bs5/examples/buttons/toggle_states/_example.html.erb +0 -2
  106. data/app/views/bs5/examples/index.html.erb +0 -23
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ecb9af880ad8e465272d743689c0c470af8c322affb3e77be49bdb360f8b0022
4
- data.tar.gz: 3b1517ef64015fa58510b9123cd625735d79e2e7fbbccda4eb161f1f4671e038
3
+ metadata.gz: 1f868555ef8de05ff2a59b42efd77e1e43a4322d896ba468075131178f0dc1a7
4
+ data.tar.gz: adbcd1e63ac16714e3b4ee5c5d72266fe687685449b08dbf87aa3b0ea359a8a7
5
5
  SHA512:
6
- metadata.gz: b638dd6760619921caaa399a585a53040501cdb796712c5bf118cc281f5ba65f16637dde70df784adff66839c2b71b91488c64bcd46a0045cc949e82cf8ba995
7
- data.tar.gz: f5b36b7a285aad9a6eb2e803d182d5e4126398ae410c9cb2a6dd66674de9474da84ba1db4f6c2202730a6f0b255a3df71762074fb8315c74711f457d3a9331a2
6
+ metadata.gz: 46c4e2e6bcb0d65824c993daea510e230c891e3bf7808ed98bbbf9d57997c36b5d01b83864cda9e4bcfa26b6aaff2460ab7d3c153510f852e8c526b58855481b
7
+ data.tar.gz: 43a3f21f7527666fd7499a1e05d14f442976e9a1465f8a4f91d1e3b967f5eb3cff1a0bb8a42c9e4efd8486710039e723c03369df33a28236b83d36bdb26a6a00
data/README.md CHANGED
@@ -32,7 +32,7 @@ $ gem install bs5
32
32
 
33
33
  Run the following command to setup your project to use Bootstrap 5.
34
34
 
35
- $ rails generate bs5:install
35
+ $ bin/rails generate bs5:install
36
36
 
37
37
  ## Contributing
38
38
 
@@ -6,22 +6,15 @@
6
6
  id="<%= item.header_id %>"
7
7
  class="accordion-header"
8
8
  >
9
- <button
10
- class="<%= item.button_class %>"
11
- type="button"
12
- data-toggle="collapse"
13
- data-target="#<%= item.collapse_id %>"
14
- aria-expanded="true"
15
- aria-controls="<%= item.collapse_id %>"
16
- >
17
- <%= item.title %>
18
- </button>
9
+ <%= button_tag item.title,
10
+ bs5_collapse(target: "##{item.collapse_id}", expanded: !item.collapsed?)
11
+ .merge(type: :button, class: item.button_class) %>
19
12
  </h2>
20
13
  <div
21
14
  id="<%= item.collapse_id %>"
22
15
  class="<%= item.collapse_class %>"
23
16
  aria-labelledby="<%= item.header_id %>"
24
- data-parent="#<%= id %>"
17
+ data-bs-parent="#<%= id %>"
25
18
  >
26
19
  <div class="accordion-body">
27
20
  <%= item.content %>
@@ -2,6 +2,7 @@
2
2
 
3
3
  module Bs5
4
4
  class AccordionComponent < ViewComponent::Base
5
+ include ComponentsHelper
5
6
  include ViewComponent::Slotable
6
7
 
7
8
  with_slot :item, collection: true, class_name: 'Item'
@@ -1,6 +1,6 @@
1
1
  <div class="<%= component_class %>" role="alert">
2
2
  <%= content %>
3
3
  <%- if is_dismissable %>
4
- <%= render Bs5::CloseButtonComponent.new(data: { dismiss: :alert }) %>
4
+ <%= render Bs5::CloseButtonComponent.new(data: { 'bs-dismiss': :alert }) %>
5
5
  <%- end %>
6
6
  </div>
@@ -30,11 +30,7 @@ module Bs5
30
30
  end
31
31
 
32
32
  def call
33
- if content
34
- button_tag(content_or_options, options) { content }
35
- else
36
- button_tag(content_or_options, options)
37
- end
33
+ button_tag(content || content_or_options, options)
38
34
  end
39
35
 
40
36
  private
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bs5
4
+ class ButtonToComponent < ViewComponent::Base
5
+ STYLES = %i[primary secondary success danger warning info light dark link].freeze
6
+ DEFAULT_STYLE = :primary
7
+ SIZES = { small: :sm, large: :lg }.freeze
8
+ CLASS_PREFIX = 'btn'
9
+
10
+ attr_reader :size
11
+
12
+ include ActiveModel::Validations
13
+ validates :style, style: true
14
+ validates :size, inclusion: { in: SIZES.keys, valid_sizes: SIZES.keys.to_sentence, allow_nil: true }
15
+
16
+ def initialize(name = nil, options = nil, html_options = nil)
17
+ @name = name
18
+ @options = options
19
+ @html_options = html_options
20
+
21
+ set_button_to_options
22
+ extract_custom_options
23
+ merge_default_options
24
+ end
25
+
26
+ def before_render
27
+ raise errors.full_messages.to_sentence if invalid?
28
+ end
29
+
30
+ def call
31
+ if content
32
+ button_to(@name, @options, @html_options) { content }
33
+ else
34
+ button_to(@name, @options, @html_options)
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def set_button_to_options
41
+ if @name.is_a? Hash
42
+ self.button_to_options = @name
43
+ elsif @options.is_a? Hash
44
+ self.button_to_options = @options
45
+ else
46
+ @html_options ||= {}
47
+ self.button_to_options = @html_options
48
+ end
49
+ end
50
+
51
+ def button_to_options=(hash)
52
+ @button_to_options = hash.symbolize_keys!
53
+ end
54
+
55
+ def extract_custom_options
56
+ extract_style
57
+ extract_outline
58
+ extract_size
59
+ end
60
+
61
+ def extract_style
62
+ @style = @button_to_options.delete(:style)
63
+ end
64
+
65
+ def extract_outline
66
+ @outline = @button_to_options.delete(:outline)
67
+ end
68
+
69
+ def extract_size
70
+ @size = @button_to_options.delete(:size)
71
+ end
72
+
73
+ def merge_default_options
74
+ @button_to_options.deep_merge!(default_options) do |_key, this_val, other_val|
75
+ [this_val, other_val].join(' ').strip
76
+ end
77
+ end
78
+
79
+ def default_options
80
+ { class: button_class }
81
+ end
82
+
83
+ def button_class
84
+ [CLASS_PREFIX, contextual_class, size_class].compact.join(' ')
85
+ end
86
+
87
+ def contextual_class
88
+ [CLASS_PREFIX, outline? ? 'outline' : nil, style].compact.join('-')
89
+ end
90
+
91
+ def size_class
92
+ return unless size?
93
+
94
+ [CLASS_PREFIX, SIZES[size]].join('-')
95
+ end
96
+
97
+ def style
98
+ (@style || DEFAULT_STYLE).to_sym
99
+ end
100
+
101
+ def outline?
102
+ !!@outline
103
+ end
104
+
105
+ def size?
106
+ !!size
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,17 @@
1
+ <% if items.any? %>
2
+ <% if actionables? %>
3
+ <div class="<%= component_class %>">
4
+ <% items.each do |item| %>
5
+ <%= item.decorated_content %>
6
+ <% end %>
7
+ </div>
8
+ <% else %>
9
+ <ul class="<%= component_class %>">
10
+ <% items.each do |item| %>
11
+ <%= tag.li(**item.options) do %>
12
+ <%= item.content %>
13
+ <% end %>
14
+ <% end %>
15
+ </ul>
16
+ <% end %>
17
+ <% end %>
@@ -0,0 +1,143 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bs5
4
+ class ListGroupComponent < ViewComponent::Base
5
+ CLASS_NAME_BASE = 'list-group'
6
+ CLASS_NAME_FLUSH = "#{CLASS_NAME_BASE}-flush"
7
+ CLASS_NAME_HORIZONTAL = "#{CLASS_NAME_BASE}-horizontal"
8
+
9
+ include ViewComponent::Slotable
10
+
11
+ with_slot :item, collection: true, class_name: 'Item'
12
+
13
+ def initialize(flush: false, horizontal: false)
14
+ @flush = flush
15
+ @horizontal = horizontal
16
+ end
17
+
18
+ private
19
+
20
+ def flush?
21
+ !!@flush
22
+ end
23
+
24
+ def actionables?
25
+ items.any?(&:actionable?)
26
+ end
27
+
28
+ def horizontal?
29
+ !!@horizontal
30
+ end
31
+
32
+ def component_class
33
+ class_names = [CLASS_NAME_BASE]
34
+ class_names << flush_class
35
+ class_names << horizontal_class
36
+ class_names.join(' ')
37
+ end
38
+
39
+ def flush_class
40
+ CLASS_NAME_FLUSH if flush?
41
+ end
42
+
43
+ def horizontal_class
44
+ return unless horizontal?
45
+
46
+ class_names = [CLASS_NAME_HORIZONTAL]
47
+ class_names << @horizontal if @horizontal.in?(%i[sm md lg xl xxl])
48
+ class_names.join('-')
49
+ end
50
+
51
+ class Item < ViewComponent::Slot
52
+ CLASS_NAME_BASE = 'list-group-item'
53
+ CLASS_NAME_ACTION = "#{CLASS_NAME_BASE}-action"
54
+
55
+ attr_reader :options
56
+
57
+ def initialize(options = {})
58
+ @options = options.symbolize_keys
59
+
60
+ @active = @options.delete(:active) || false
61
+ @disabled = @options.delete(:disabled) || false
62
+ @style = @options.delete(:style)
63
+
64
+ set_attributes
65
+ end
66
+
67
+ def actionable?
68
+ !!actionable_element
69
+ end
70
+
71
+ def actionable_element
72
+ @actionable_element ||= begin
73
+ if (elements = Nokogiri::HTML::DocumentFragment.parse(content).elements).one? &&
74
+ (element = elements.first).name.in?(%w[a button label])
75
+ element
76
+ end
77
+ end
78
+ end
79
+
80
+ def decorated_content
81
+ set_actionable_element_attributes
82
+ actionable_element.to_html.html_safe # rubocop:disable Rails/OutputSafety
83
+ end
84
+
85
+ private
86
+
87
+ def set_actionable_element_class_names
88
+ class_names = Array(actionable_element[:class])
89
+ class_names << item_classes
90
+ class_names << CLASS_NAME_ACTION if actionable_element.name.in?(%w[a button])
91
+ actionable_element[:class] = class_names.join(' ')
92
+ end
93
+
94
+ def set_actionable_element_attributes
95
+ set_actionable_element_class_names
96
+ actionable_element['aria-current'] = true if active?
97
+
98
+ return unless disabled?
99
+
100
+ case actionable_element.name
101
+ when 'a'
102
+ actionable_element['aria-disabled'] = true
103
+ actionable_element['tabindex'] = -1
104
+ when 'button'
105
+ actionable_element['disabled'] = ''
106
+ end
107
+ end
108
+
109
+ def set_attributes
110
+ @options[:class] = Array(@options[:class])
111
+ @options[:class] << item_classes
112
+ @options[:aria] ||= {}
113
+ @options[:aria][:current] = true if active?
114
+ @options[:aria][:disabled] = true if disabled?
115
+ end
116
+
117
+ def item_classes
118
+ class_names = [CLASS_NAME_BASE]
119
+ class_names << 'active' if active?
120
+ class_names << 'disabled' if disabled?
121
+ class_names << contextual_class if style?
122
+
123
+ class_names
124
+ end
125
+
126
+ def active?
127
+ !!@active
128
+ end
129
+
130
+ def disabled?
131
+ !!@disabled
132
+ end
133
+
134
+ def style?
135
+ !!@style
136
+ end
137
+
138
+ def contextual_class
139
+ [CLASS_NAME_BASE, @style].join('-')
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_dependency 'bs5/application_controller'
4
+
5
+ module Bs5
6
+ class PagesController < ApplicationController
7
+ def show
8
+ render template: "bs5/pages/#{params[:page]}"
9
+ end
10
+ end
11
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Bs5
4
4
  module ComponentsHelper
5
- COMPONENTS = %w[accordion alert badge close_button breadcrumb button_tag].freeze
5
+ COMPONENTS = %w[accordion alert badge close_button breadcrumb button_tag button_to list_group].freeze
6
6
 
7
7
  COMPONENTS.each do |name|
8
8
  define_method("bs5_#{name}") do |*args, &block|
@@ -11,6 +11,19 @@ module Bs5
11
11
  end
12
12
  end
13
13
 
14
+ def bs5_tooltip(title, options = nil)
15
+ default_options = { toggle: :tooltip }
16
+ options = Hash(options).symbolize_keys.merge(default_options)
17
+ {
18
+ title: title,
19
+ data: options
20
+ }
21
+ end
22
+
23
+ def bs5_collapse(*args)
24
+ CollapseService.new(*args).to_hash
25
+ end
26
+
14
27
  private
15
28
 
16
29
  def render_component(component_clazz, *args)
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bs5
4
+ class CollapseService
5
+ CONTROLS_ERR_MSG = 'Please provide either a `controls` option' \
6
+ ' containing the id of the collapsible element' \
7
+ ' or an ID selector as `target` options.'
8
+
9
+ attr_reader :expanded, :target, :controls
10
+
11
+ def initialize(expanded: false, target: nil, controls: nil)
12
+ @expanded = expanded
13
+ @target = target
14
+ @controls = controls
15
+ end
16
+
17
+ def to_hash
18
+ {
19
+ data: data_options,
20
+ aria: aria_options
21
+ }
22
+ end
23
+
24
+ private
25
+
26
+ def data_options
27
+ options = { 'bs-toggle': :collapse }
28
+ options[:'bs-target'] = target if target
29
+
30
+ options
31
+ end
32
+
33
+ def aria_options
34
+ options = { expanded: expanded }
35
+
36
+ if controls
37
+ options[:controls] = controls
38
+ elsif target&.start_with?('#')
39
+ options[:controls] = target.delete_prefix('#')
40
+ else
41
+ raise CONTROLS_ERR_MSG
42
+ end
43
+
44
+ options
45
+ end
46
+ end
47
+ end
@@ -1,2 +1,2 @@
1
- <h3>Default</h3>
1
+ <h2>Example</h2>
2
2
  <%= bs5_example(snippet: 'accordion/default/snippet') %>