bulma-phlex 0.2.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: 9c9baf01284c1c12d06e389eb2871a7c9ada348d3611c26e74ce7d7f24136c83
4
- data.tar.gz: baaee6a523f95e49507bb6a289b28301fb1d218284aee8cef960dc6b53baf559
3
+ metadata.gz: 15f8b354ec9106151e857e9fa274851fd8a25c05aab5bd6206418959e5e8201c
4
+ data.tar.gz: 01302d8221c498b716b2b840948011f17227906246718233c43b252dd385fd22
5
5
  SHA512:
6
- metadata.gz: 24302ffbdc624f9b1041ff786d0a57aaf2d3fed7abc4d32096a9c50f861a48049bc9c6ffdf27dfcbfbd94e50d3569debc5898617b77fe9d00be34bc2b702f5bd
7
- data.tar.gz: aaaa147076dfd55030ea1fd94adc9d4d71e0e05bc6bf8841eabe776d6633426142e06119635e6db68f9972df12369595c933e6fcba03845d7e0d326920ff271b
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
 
@@ -117,12 +117,12 @@ The [NavigationBar](https://bulma.io/documentation/components/navbar/) component
117
117
  ```ruby
118
118
  render Components::Bulma::NavigationBar.new do |navbar|
119
119
  navbar.brand_item "My App", "/"
120
-
120
+
121
121
  navbar.left do |menu|
122
122
  menu.item "Home", "/"
123
123
  menu.item "Products", "/products"
124
124
  end
125
-
125
+
126
126
  navbar.right do |menu|
127
127
  menu.item "About", "/about"
128
128
  menu.dropdown "Account" do |dropdown|
@@ -159,11 +159,11 @@ render Components::Bulma::Table.new(users) do |table|
159
159
  table.column "Name" do |user|
160
160
  user.full_name
161
161
  end
162
-
162
+
163
163
  table.column "Email" do |user|
164
164
  user.email
165
165
  end
166
-
166
+
167
167
  table.column "Actions" do |user|
168
168
  link_to "Edit", edit_user_path(user), class: "button is-small"
169
169
  end
@@ -174,22 +174,38 @@ end
174
174
 
175
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.
176
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
+
177
179
  ```ruby
178
- render Components::Bulma::Tabs.new do |tabs|
180
+ render Components::Bulma::Tabs.new(tabs_class: "is-boxed", contents_class: "ml-4") do |tabs|
179
181
  tabs.tab(id: "profile", title: "Profile", active: true) do
180
182
  "Profile content goes here"
181
183
  end
182
-
184
+
183
185
  tabs.tab(id: "settings", title: "Settings", icon: "fas fa-cog") do
184
186
  "Settings content goes here"
185
187
  end
186
-
188
+
187
189
  tabs.tab(id: "notifications", title: "Notifications", icon: "fas fa-bell") do
188
190
  "Notifications content goes here"
189
191
  end
190
192
  end
191
193
  ```
192
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
+
193
209
  ## Development
194
210
 
195
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.2.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,52 +37,83 @@ 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
44
+
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
32
63
 
33
- def initialize
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
34
70
  @tabs = []
35
71
  @contents = []
36
72
  end
37
73
 
38
- def tab(id:, title:, icon: nil, active: false, &block)
39
- @tabs << Tab.new(id:, title:, icon:, active:)
40
- @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
41
84
  end
42
85
 
43
86
  def view_template(&)
44
87
  vanish(&)
45
88
 
46
- div(data: { controller: "tabs" }) do
47
- div(class: "tabs is-boxed") do
48
- ul do
49
- @tabs.each do |tab|
50
- li(
51
- data: {
52
- tabs_target: "tab",
53
- tab_content: tab.id,
54
- action: "click->tabs#showTabContent"
55
- },
56
- class: tab.active ? "is-active" : ""
57
- ) do
58
- a do
59
- icon_span(tab.icon, "mr-1") if tab.icon
60
- span { tab.title }
61
- end
62
- end
63
- end
64
- end
65
- 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 }
92
+ end
93
+ end
94
+
95
+ private
66
96
 
67
- @contents.each do |content|
68
- div(id: content.id,
69
- class: content.active ? "" : "hidden",
70
- data: { tabs_target: "content" }) do
71
- content.block.call
72
- end
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 }
73
110
  end
74
111
  end
75
112
  end
113
+
114
+ def build_content
115
+ @contents.each { render it }
116
+ end
76
117
  end
77
118
  end
78
119
  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.2.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