sdr_view_components 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a5c75fc70087db7f22de6555353858ad37e5e1eca8b22fb3bf0ef97fa875cb50
4
- data.tar.gz: 4d08732919f119eb589ba79c4e194ff98868c6dcd7c7b105e6b008232100c00b
3
+ metadata.gz: e251b4177b159e8978b41116e020d3d58d5f95071c6dbccd9b212d3751c9a118
4
+ data.tar.gz: be45e43ececfc02ed410b67844418c80371a28a1b89596a56630f3d81ce2df22
5
5
  SHA512:
6
- metadata.gz: a37dc109660769c562ff43176deaeb796396fa5039f472e4bbbdd3ef1983028913567dc9d4ccd21b6e1ff122a3d57dac4e158369df119565c3ca02c8e0aa8b0d
7
- data.tar.gz: 858f08586f7682571637115a94ae7aaf93d263de9675155f2fbcdfda262c6e83fe4a26d41d5448ccf4cda2c2a2be4159bbdbb641f6169381c7e96df62a255306
6
+ metadata.gz: 638147ae953c62036f9553375bbac77530a7b6dfdc4dcd9243954b4bf702250a70a5953fb9b4444f2d1c5c249f45a797816a0def51f323c57d6fad91a57d566c
7
+ data.tar.gz: c7a3458363dfe0bc1000fb4b24ce6415fece60c20f3d06722c5f626204eff3582badefe1a9ab0696000aa949777484dbd3c6a50938762c1e600a8782d08907c8
data/README.md CHANGED
@@ -65,6 +65,19 @@ application.register("sdr-tab-link", TabLinkController)
65
65
  application.register("sdr-tab-nav", TabNavController)
66
66
  ```
67
67
 
68
+ `SdrViewComponents::Elements::Tabs::TabListComponent`'s `collapse_below` option (see [General usage](#general-usage) below) ships `sdr_view_components/tab_select_controller`, registered as `sdr-tab-select`:
69
+
70
+ ```ruby
71
+ pin "sdr_view_components/tab_select_controller", to: "sdr_view_components/tab_select_controller.js"
72
+ ```
73
+
74
+ ```javascript
75
+ import { application } from "controllers/application"
76
+ import TabSelectController from "sdr_view_components/tab_select_controller"
77
+
78
+ application.register("sdr-tab-select", TabSelectController)
79
+ ```
80
+
68
81
  ## Usage
69
82
 
70
83
  ### Form components
@@ -151,6 +164,23 @@ See the Lookbook preview for `SdrViewComponents::TabForm::TabListComponent` for
151
164
 
152
165
  ### General usage:
153
166
 
167
+ `SdrViewComponents::Elements::Tabs::TabListComponent` accepts a `collapse_below:` option (one of `:sm`, `:md`, `:lg`, `:xl`, `:xxl`). When given, the tabs are replaced with a `<select>` below that Bootstrap breakpoint instead of wrapping or scrolling -- the `<select>` drives the same Bootstrap tab JavaScript as the tabs, so panes switch the same way regardless of which control is visible. Leave it unset (the default) to always render the tabs. This requires the `sdr-tab-select` controller -- see [JavaScript](#javascript) above.
168
+
169
+ ```erb
170
+ <%= render SdrViewComponents::Elements::Tabs::TabListComponent.new(variant: :underline, collapse_below: :xl) do |tab_list| %>
171
+ <% tab_list.with_tab(label: 'Details', id: 'details-tab', pane_id: 'details-pane', active: true) %>
172
+ <% tab_list.with_tab(label: 'History', id: 'history-tab', pane_id: 'history-pane') %>
173
+
174
+ <% tab_list.with_pane(id: 'details-pane', tab_id: 'details-tab', active: true) do %>
175
+ <p>Details pane content.</p>
176
+ <% end %>
177
+
178
+ <% tab_list.with_pane(id: 'history-pane', tab_id: 'history-tab') do %>
179
+ <p>History pane content.</p>
180
+ <% end %>
181
+ <% end %>
182
+ ```
183
+
154
184
  ```
155
185
  <% render SdrViewComponent::....>
156
186
  ```
@@ -4,13 +4,14 @@ module SdrViewComponents
4
4
  module Elements
5
5
  # Applies an h# component with the expected styling.
6
6
  class HeadingComponent < BaseComponent
7
- def initialize(level:, text: nil, variant: nil, classes: [])
7
+ def initialize(level:, text: nil, variant: nil, classes: [], **options)
8
8
  raise ArgumentError, 'Invalid level' unless %i[h1 h2 h3 h4 h5 h6].include?(level.to_sym)
9
9
 
10
10
  @level = level
11
11
  @variant = variant
12
12
  @classes = classes
13
13
  @text = text # Provide text or content
14
+ @options = options
14
15
  super()
15
16
  end
16
17
 
@@ -22,7 +23,7 @@ module SdrViewComponents
22
23
  def call
23
24
  return unless tag.respond_to?(@level)
24
25
 
25
- tag.public_send(@level, class: classes) do
26
+ tag.public_send(@level, class: classes, **@options) do
26
27
  @text || content
27
28
  end
28
29
  end
@@ -1,9 +1,21 @@
1
- <%= tag.ul class: classes, role: 'tablist' do %>
2
- <% tabs.each do |tab| %><%= tab %><% end %>
1
+ <% if collapse_below? %>
2
+ <div data-controller="sdr-tab-select" data-action="shown.bs.tab->sdr-tab-select#sync">
3
+ <%= tag.select class: select_classes, 'aria-label': 'Select a tab', data: { sdr_tab_select_target: 'select', action: 'sdr-tab-select#change' } do %>
4
+ <% tabs.each do |tab| %><%= tag.option(tab.label, value: tab.id, selected: tab.active?) %><% end %>
5
+ <% end %>
6
+
7
+ <%= tag.ul class: classes, role: 'tablist' do %>
8
+ <% tabs.each do |tab| %><%= tab %><% end %>
9
+ <% end %>
10
+ </div>
11
+ <% else %>
12
+ <%= tag.ul class: classes, role: 'tablist' do %>
13
+ <% tabs.each do |tab| %><%= tab %><% end %>
14
+ <% end %>
3
15
  <% end %>
4
16
 
5
17
  <%= header %>
6
18
 
7
- <div class="tab-content">
19
+ <%= tag.div class: content_classes do %>
8
20
  <% panes.each do |pane| %><%= pane %><% end %>
9
- </div>
21
+ <% end %>
@@ -5,21 +5,48 @@ module SdrViewComponents
5
5
  module Tabs
6
6
  # Component for rendering a list of tabs in a tabbed interface.
7
7
  class TabListComponent < BaseComponent
8
+ BREAKPOINTS = %i[sm md lg xl xxl].freeze
9
+
8
10
  renders_one :header # optional
9
11
  renders_many :tabs, Elements::Tabs::TabComponent
10
12
  renders_many :panes, Elements::Tabs::PaneComponent
11
13
 
12
- def initialize(classes: [], variant: :default)
14
+ # @param classes [Array<String>, String] additional classes for the tab list.
15
+ # @param content_classes [Array<String>, String] additional classes for the tab content container.
16
+ # @param variant [Symbol] the visual style of the tabs (:default or :underline).
17
+ # @param collapse_below [Symbol, nil] if given, the tabs are replaced with a `<select>`
18
+ # below this Bootstrap breakpoint (one of :sm, :md, :lg, :xl, :xxl). The tabs remain
19
+ # fully functional (the `<select>` drives the same Bootstrap tab JavaScript) -- this
20
+ # only changes how they're presented at narrow viewports. Leave nil (the default) to
21
+ # always render the tabs.
22
+ def initialize(classes: [], content_classes: [], variant: :default, collapse_below: nil)
13
23
  @classes = classes
24
+ @content_classes = content_classes
14
25
  @variant = variant
26
+ @collapse_below = collapse_below
15
27
 
16
28
  raise ArgumentError, "Invalid variant: #{variant}" unless %i[underline default].include?(variant)
29
+ if collapse_below && BREAKPOINTS.exclude?(collapse_below)
30
+ raise ArgumentError, "Invalid collapse_below: #{collapse_below}"
31
+ end
17
32
 
18
33
  super()
19
34
  end
20
35
 
36
+ def collapse_below?
37
+ @collapse_below.present?
38
+ end
39
+
21
40
  def classes
22
- merge_classes('nav', @classes, variant_classes)
41
+ merge_classes('nav', @classes, variant_classes, collapse_below? ? ['d-none', "d-#{@collapse_below}-flex"] : nil)
42
+ end
43
+
44
+ def select_classes
45
+ merge_classes('form-select', "d-#{@collapse_below}-none", @classes)
46
+ end
47
+
48
+ def content_classes
49
+ merge_classes('tab-content', @content_classes)
23
50
  end
24
51
 
25
52
  private
@@ -0,0 +1,22 @@
1
+ import { Controller } from '@hotwired/stimulus'
2
+
3
+ // Drives a <select> that stands in for a tab list on narrow viewports (see
4
+ // TabListComponent's `collapse_below` option). Selecting an option shows the
5
+ // corresponding tab; activating a tab some other way (e.g. clicking the tab
6
+ // list above the collapse breakpoint) keeps the select's value in sync.
7
+ //
8
+ // Uses window.bootstrap rather than `import * as bootstrap from 'bootstrap'`:
9
+ // the UMD bootstrap.bundle.min.js has no ES module exports, so an ESM import
10
+ // of it resolves to an empty namespace object even though the script itself
11
+ // still assigns the real API to window.bootstrap as a side effect.
12
+ export default class extends Controller {
13
+ static targets = ['select']
14
+
15
+ change (event) {
16
+ window.bootstrap.Tab.getOrCreateInstance(document.getElementById(event.target.value)).show()
17
+ }
18
+
19
+ sync (event) {
20
+ this.selectTarget.value = event.target.id
21
+ }
22
+ }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SdrViewComponents
4
- VERSION = '0.5.0'
4
+ VERSION = '0.6.0'
5
5
  end
@@ -33,6 +33,12 @@ module SdrViewComponents
33
33
  render SdrViewComponents::Elements::ButtonLinkComponent.new(link: '/example', label: 'Info Link Button',
34
34
  variant: :info)
35
35
  end
36
+
37
+ def nil
38
+ render SdrViewComponents::Elements::ButtonLinkComponent.new(link: '/example', label: 'Nil Variant Link Button',
39
+ variant: nil)
40
+ end
41
+
36
42
  # @!endgroup
37
43
 
38
44
  # @!group Button Sizes
@@ -0,0 +1,14 @@
1
+ <%# Resize the preview frame below the sm breakpoint to see the tabs collapse into a select. %>
2
+ <%# The sdr-tab-select controller isn't registered in this dummy app, so the select won't switch panes here -- see the consuming app for a working example. %>
3
+ <%= render SdrViewComponents::Elements::Tabs::TabListComponent.new(variant: :underline, collapse_below: :sm) do |tab_list| %>
4
+ <% tab_list.with_tab(label: 'Details', id: 'details-tab', pane_id: 'details-pane', active: true) %>
5
+ <% tab_list.with_tab(label: 'History', id: 'history-tab', pane_id: 'history-pane') %>
6
+
7
+ <% tab_list.with_pane(id: 'details-pane', tab_id: 'details-tab', active: true) do %>
8
+ <p>Details pane content.</p>
9
+ <% end %>
10
+
11
+ <% tab_list.with_pane(id: 'history-pane', tab_id: 'history-tab') do %>
12
+ <p>History pane content.</p>
13
+ <% end %>
14
+ <% end %>
@@ -10,6 +10,8 @@ module SdrViewComponents
10
10
  # @!endgroup
11
11
 
12
12
  def with_header; end
13
+
14
+ def collapsible; end
13
15
  end
14
16
  end
15
17
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sdr_view_components
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Collier
@@ -170,6 +170,7 @@ files:
170
170
  - app/javascript/sdr_view_components/tab_error_controller.js
171
171
  - app/javascript/sdr_view_components/tab_link_controller.js
172
172
  - app/javascript/sdr_view_components/tab_nav_controller.js
173
+ - app/javascript/sdr_view_components/tab_select_controller.js
173
174
  - app/javascript/sdr_view_components/toast_controller.js
174
175
  - app/views/layouts/lookbook.html.erb
175
176
  - config/routes.rb
@@ -199,6 +200,7 @@ files:
199
200
  - spec/components/previews/sdr_view_components/elements/progress_bar_component_preview.rb
200
201
  - spec/components/previews/sdr_view_components/elements/spinner_component_preview.rb
201
202
  - spec/components/previews/sdr_view_components/elements/tabs/tab_list_component_preview.rb
203
+ - spec/components/previews/sdr_view_components/elements/tabs/tab_list_component_preview/collapsible.html.erb
202
204
  - spec/components/previews/sdr_view_components/elements/tabs/tab_list_component_preview/default_variant.html.erb
203
205
  - spec/components/previews/sdr_view_components/elements/tabs/tab_list_component_preview/underline_variant.html.erb
204
206
  - spec/components/previews/sdr_view_components/elements/tabs/tab_list_component_preview/with_header.html.erb