bulma-phlex 0.3.0 → 0.4.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: 5838ff317a395e9e57b33d45dad7f62c3a7820112a2453c35e9305e7a6ac19c1
4
- data.tar.gz: 48988fd8c7210d47b0090d9d385fa6fce278d2ad6d47d10673aabf9e46034df8
3
+ metadata.gz: 15f8b354ec9106151e857e9fa274851fd8a25c05aab5bd6206418959e5e8201c
4
+ data.tar.gz: 01302d8221c498b716b2b840948011f17227906246718233c43b252dd385fd22
5
5
  SHA512:
6
- metadata.gz: 49c89b3e7e9aa630d01ce8ae14b0c35694d811533c37512e42d2d21dbcae83546a96e5ef0262b4078d9235d6cea73f3dcad529ed91c4dd0ccfda1eee1af040d4
7
- data.tar.gz: 9840cfdb56b3af42f2a32c3717b985a29ef93d939444ceaef47545915e450e8717c543eff3f867946aacdaac45ca51dcd2845f0d53b3b61a3eaa594586964847
6
+ metadata.gz: 42078a30c124dc2fbc1ad6342034474204ceca1371b0d1a3394d47940926e5a42c7f44e83bab0cd9a54f24ad56860a5bbbd5f38ad72f4531ba809e0f71899cd5
7
+ data.tar.gz: 98dd7ea99a81ad71302ca5ca78fe2a915c083f6f7f20f520a7cdc2e004768bece733f6650ce9fe064033728877c790b8b75cbf5865d6044d0137a683c0f3ad65
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  # Bulma Components Built with Phlex
6
6
 
7
- This gem provides a set of ready-to-use components [Phlex](https://github.com/phlex-ruby/phlex) for common [Bulma](https://bulma.io/) components and elements, making it easy to build beautiful, responsive interfaces with a clean, Ruby-focused API.
7
+ This gem provides a set of ready-to-use [Phlex](https://github.com/phlex-ruby/phlex) components for common [Bulma](https://bulma.io/) components and elements, making it easy to build beautiful, responsive interfaces with a clean, Ruby-focused API.
8
8
 
9
9
  ## Installation
10
10
 
@@ -152,8 +152,6 @@ render Components::Bulma::Pagination.new(@products, ->(page) { products_path(pag
152
152
 
153
153
  The [Table](https://bulma.io/documentation/elements/table/) component provides a way to display data in rows and columns with customizable headers and formatting options.
154
154
 
155
- It requires a Hotwired Stimulus controller to manage the tabs and the content. By default, the controller name is assumed to be `bulma--tabs`, but can be overridden with the constructor keyword argument `stimulus_controller`. Stimulus targets and actions are added to the component.
156
-
157
155
  ```ruby
158
156
  users = User.all
159
157
 
@@ -176,8 +174,10 @@ end
176
174
 
177
175
  The [Tabs](https://bulma.io/documentation/components/tabs/) component provides a way to toggle between different content sections using tabbed navigation, with support for icons and active state management.
178
176
 
177
+ Behavior of the tabs can be driven by the data attributes, which are assigned by the object passed in as the `data_attributes_builder`. By default, this will use the `StimulusDataAttributes` class with the controller name `bulma--tabs`. The controller is not provided by this library, but you can create your own Stimulus controller to handle the tab switching logic. Here is [an implementation of a Stimulus controller for Bulma tabs](https://github.com/RockSolt/bulma-rails-helpers/blob/main/app/javascript/controllers/bulma/tabs_controller.js).
178
+
179
179
  ```ruby
180
- render Components::Bulma::Tabs.new do |tabs|
180
+ render Components::Bulma::Tabs.new(tabs_class: "is-boxed", contents_class: "ml-4") do |tabs|
181
181
  tabs.tab(id: "profile", title: "Profile", active: true) do
182
182
  "Profile content goes here"
183
183
  end
@@ -192,6 +192,20 @@ render Components::Bulma::Tabs.new do |tabs|
192
192
  end
193
193
  ```
194
194
 
195
+ **Constructor Keyword Arguments:**
196
+
197
+ - `tabs_class`: Classes to be added to the tabs div, such as `is-boxed`, `is-medium`, `is-centered`, or `is-toggle`.
198
+ - `contents_class`: Classes added to the div that wraps the content (no Bulma related tabs functionality here, just a hook).
199
+ - `data_attributes_builder`: Builder object that responds to `for_container`, `for_tab`, and `for_content` (with the latter two receiving the tab `id`). See the default `StimulusDataAttributes` for an example.
200
+
201
+ **Keyword Arguments for `tab` Method:**
202
+
203
+ - `id`: The id to be assigned to the content. The tab will be assigned the same id with the suffix `-tab`.
204
+ - `title`: The name on the tab.
205
+ - `active`: Adds the `is-active` class to the tab and shows the related content. Non-active content is assigned the `is-hidden` class. Defaults to `false`.
206
+ - `icon`: Specify an optional icon class.
207
+
208
+
195
209
  ## Development
196
210
 
197
211
  After checking out the repo, run `bundle install` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/lib/bulma-phlex.rb CHANGED
@@ -13,3 +13,5 @@ require "components/bulma/navigation_bar"
13
13
  require "components/bulma/pagination"
14
14
  require "components/bulma/table"
15
15
  require "components/bulma/tabs"
16
+ require "components/bulma/tab_components/content"
17
+ require "components/bulma/tab_components/tab"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BulmaPhlex
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Components
4
+ module Bulma
5
+ module TabComponents
6
+ # # Content
7
+ #
8
+ # This component represents a single content section within the Bulma Tabs component.
9
+ #
10
+ # ## Arguments:
11
+ # - `id`: Unique identifier for the content.
12
+ # - `active`: Boolean indicating if the content is currently active.
13
+ # - `data_attributes_proc`: A proc that generates data attributes for the content.
14
+ class Content < Components::Bulma::Base
15
+ def initialize(id:, active:, data_attributes_proc: nil)
16
+ @id = id
17
+ @active = active
18
+ @data_attributes = data_attributes_proc ||
19
+ Components::Bulma::Tabs::StimulusDataAttributes.new("bulma--tabs").method(:for_content)
20
+ end
21
+
22
+ def view_template(&)
23
+ div(id: @id,
24
+ class: @active ? "" : "is-hidden",
25
+ data: @data_attributes.call(@id), &)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Components
4
+ module Bulma
5
+ module TabComponents
6
+ # # Tab
7
+ #
8
+ # This component represents a single tab within the Bulma Tabs component.
9
+ #
10
+ # The component can be used if you need to create or update a tab dynamically.
11
+ #
12
+ # ## Arguments
13
+ #
14
+ # - `id`: Unique identifier for the tab.
15
+ # - `title`: The text displayed on the tab.
16
+ # - `icon`: Optional icon to display on the tab.
17
+ # - `active`: Boolean indicating if the tab is currently active.
18
+ # - `data_attributes_proc`: A proc that generates data attributes for the tab.
19
+ class Tab < Components::Bulma::Base
20
+ def initialize(id:, title:, icon:, active:,
21
+ data_attributes_proc: Components::Bulma::Tabs::StimulusDataAttributes.new("bulma--tabs").method(:for_tab))
22
+ @id = id
23
+ @title = title
24
+ @icon = icon
25
+ @active = active
26
+ @data_attributes_proc = data_attributes_proc
27
+ end
28
+
29
+ def view_template(&)
30
+ li(
31
+ id: "#{@id}-tab",
32
+ data: @data_attributes_proc.call(@id),
33
+ class: @active ? "is-active" : ""
34
+ ) do
35
+ a do
36
+ icon_span(@icon, "mr-1") if @icon
37
+ span { @title }
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -30,7 +30,7 @@ module Components
30
30
  #
31
31
  class Table < Components::Bulma::Base
32
32
  def initialize(rows, id = nil)
33
- @id = id || rows.first&.model_name&.plural
33
+ @id = id || id_from_array_or_arel(rows)
34
34
  @rows = rows
35
35
  @columns = []
36
36
  end
@@ -69,6 +69,16 @@ module Components
69
69
 
70
70
  private
71
71
 
72
+ def id_from_array_or_arel(rows)
73
+ if rows.respond_to? :model
74
+ rows.model.model_name.plural
75
+ elsif rows.empty?
76
+ "table"
77
+ else
78
+ rows.first.model_name.plural
79
+ end
80
+ end
81
+
72
82
  # this derives a th class from the column html attributes
73
83
  # perhaps a better way would be pre-defined pairs?
74
84
  def table_header(column)
@@ -8,6 +8,16 @@ module Components
8
8
  # interface, providing a way to toggle between different content sections using
9
9
  # tabbed navigation. Includes support for icons and active state management.
10
10
  #
11
+ # Classes can be assigned to either the tabs or contents wrappers. The tabs div is where Bulma
12
+ # options such as `is-boxed`, `is-centered`, or `is-small` can be added.
13
+ #
14
+ # Use method `right_content` to add content to the right of the tabs, such as a button.
15
+ #
16
+ # The tabs behavior can be managed by the data attributes provided by the `data_attributes_builder` argument. By
17
+ # default, this will use the `StimulusDataAttributes` class with the controller name `bulma--tabs`. That controller
18
+ # is not provided by this library, but you can create your own Stimulus controller to handle the tab switching
19
+ # logic. Here is [an implementation of a Stimulus controller for Bulma tabs](https://github.com/RockSolt/bulma-rails-helpers/blob/main/app/javascript/controllers/bulma/tabs_controller.js).
20
+ #
11
21
  # ## Example
12
22
  #
13
23
  # ```ruby
@@ -27,59 +37,82 @@ module Components
27
37
  # ```
28
38
  #
29
39
  class Tabs < Components::Bulma::Base
30
- Tab = Data.define(:id, :title, :icon, :active)
31
- Content = Data.define(:id, :block, :active)
40
+ StimulusDataAttributes = Data.define(:stimulus_controller) do
41
+ def for_container
42
+ { controller: stimulus_controller }
43
+ end
32
44
 
33
- def initialize(stimulus_controller: "bulma--tabs")
34
- @stimulus_controller = stimulus_controller
45
+ def for_tab(id)
46
+ {
47
+ target_key => "tab",
48
+ tab_content: id,
49
+ action: "click->#{stimulus_controller}#showTabContent"
50
+ }
51
+ end
52
+
53
+ def for_content(_id)
54
+ { target_key => "content" }
55
+ end
56
+
57
+ private
58
+
59
+ def target_key
60
+ "#{stimulus_controller}-target"
61
+ end
62
+ end
63
+
64
+ def initialize(id: nil, tabs_class: nil, contents_class: nil,
65
+ data_attributes_builder: StimulusDataAttributes.new("bulma--tabs"))
66
+ @id = id || "tabs"
67
+ @tabs_class = tabs_class
68
+ @contents_class = contents_class
69
+ @data_attributes_builder = data_attributes_builder
35
70
  @tabs = []
36
71
  @contents = []
37
72
  end
38
73
 
39
- def tab(id:, title:, icon: nil, active: false, &block)
40
- @tabs << Tab.new(id:, title:, icon:, active:)
41
- @contents << Content.new(id:, block:, active:)
74
+ def tab(id:, title:, icon: nil, active: false, &)
75
+ @tabs << TabComponents::Tab.new(id:, title:, icon:, active:,
76
+ data_attributes_proc: @data_attributes_builder.method(:for_tab))
77
+ @contents << TabComponents::Content.new(id:, active:,
78
+ data_attributes_proc: @data_attributes_builder.method(:for_content),
79
+ &)
80
+ end
81
+
82
+ def right_content(&content)
83
+ @right_content = content
42
84
  end
43
85
 
44
86
  def view_template(&)
45
87
  vanish(&)
46
88
 
47
- div(data: { controller: @stimulus_controller }) do
48
- div(class: "tabs is-boxed") do
49
- ul do
50
- @tabs.each do |tab|
51
- li(
52
- id: "#{tab.id}-tab",
53
- data: {
54
- target_key => "tab",
55
- tab_content: tab.id,
56
- action: "click->#{@stimulus_controller}#showTabContent"
57
- },
58
- class: tab.active ? "is-active" : ""
59
- ) do
60
- a do
61
- icon_span(tab.icon, "mr-1") if tab.icon
62
- span { tab.title }
63
- end
64
- end
65
- end
66
- end
67
- end
68
-
69
- @contents.each do |content|
70
- div(id: content.id,
71
- class: content.active ? "" : "is-hidden",
72
- data: { target_key => "content" }) do
73
- content.block.call
74
- end
75
- end
89
+ div(id: @id, data: @data_attributes_builder.for_container) do
90
+ build_tabs_with_optional_right_content
91
+ div(id: "#{@id}-content", class: @contents_class) { build_content }
76
92
  end
77
93
  end
78
94
 
79
95
  private
80
96
 
81
- def target_key
82
- "#{@stimulus_controller}-target"
97
+ def build_tabs_with_optional_right_content
98
+ return build_tabs if @right_content.nil?
99
+
100
+ div(class: "columns") do
101
+ div(class: "column") { build_tabs }
102
+ div(class: "column is-narrow") { @right_content.call }
103
+ end
104
+ end
105
+
106
+ def build_tabs
107
+ div(class: "tabs #{@tabs_class}".strip) do
108
+ ul(id: "#{@id}-tabs") do
109
+ @tabs.each { render it }
110
+ end
111
+ end
112
+ end
113
+
114
+ def build_content
115
+ @contents.each { render it }
83
116
  end
84
117
  end
85
118
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bulma-phlex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Todd Kummer
@@ -99,6 +99,8 @@ files:
99
99
  - lib/components/bulma/navigation_bar.rb
100
100
  - lib/components/bulma/navigation_bar_dropdown.rb
101
101
  - lib/components/bulma/pagination.rb
102
+ - lib/components/bulma/tab_components/content.rb
103
+ - lib/components/bulma/tab_components/tab.rb
102
104
  - lib/components/bulma/table.rb
103
105
  - lib/components/bulma/tabs.rb
104
106
  homepage: https://github.com/RockSolt/bulma-phlex