bulma-phlex 0.7.0 → 0.8.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.
@@ -1,44 +0,0 @@
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
@@ -1,150 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Components
4
- module Bulma
5
- # Table component for data display
6
- #
7
- # This component implements the [Bulma table](https://bulma.io/documentation/elements/table/)
8
- # interface, providing a way to display data in rows and columns with customizable
9
- # headers and formatting options.
10
- #
11
- # ## Example
12
- #
13
- # ```ruby
14
- # users = User.all
15
- #
16
- # Bulma::Table(users) do |table|
17
- # table.column "Name" do |user|
18
- # user.full_name
19
- # end
20
- #
21
- # table.column "Email" do |user|
22
- # user.email
23
- # end
24
- #
25
- # table.column "Actions" do |user|
26
- # link_to "Edit", edit_user_path(user), class: "button is-small"
27
- # end
28
- # end
29
- # ```
30
- #
31
- class Table < Components::Bulma::Base
32
- def initialize(rows, id_or_options = nil, **options)
33
- @rows = rows
34
- @id, @table_class = parse_id_and_options(id_or_options, options, rows)
35
- @columns = []
36
- end
37
-
38
- def view_template(&)
39
- vanish(&)
40
-
41
- table(id: @id, class: @table_class) do
42
- thead do
43
- @columns.each do |column|
44
- table_header(column)
45
- end
46
- end
47
-
48
- tbody do
49
- @rows.each do |row|
50
- tr do
51
- @columns.each do |column|
52
- td(**column[:html_attributes]) { column[:content].call(row) }
53
- end
54
- end
55
- end
56
- end
57
-
58
- pagination
59
- end
60
- end
61
-
62
- def column(header, **html_attributes, &content)
63
- @columns << { header:, html_attributes:, content: }
64
- end
65
-
66
- def date_column(header, format: "%Y-%m-%d", **html_attributes, &content)
67
- column(header, **html_attributes) do |row|
68
- content.call(row)&.strftime(format)
69
- end
70
- end
71
-
72
- def conditional_icon(header, icon_class: "fas fa-check", **html_attributes, &content)
73
- html_attributes[:class] = [html_attributes[:class], "has-text-centered"].compact.join(" ")
74
-
75
- column(header, **html_attributes) do |row|
76
- icon_span(icon_class) if content.call(row)
77
- end
78
- end
79
-
80
- def paginate(&path_builder)
81
- @path_builder = path_builder
82
- end
83
-
84
- private
85
-
86
- def parse_id_and_options(id_or_options, options, rows)
87
- if id_or_options.is_a?(String)
88
- id = id_or_options
89
- opts = options
90
- else
91
- opts = (id_or_options || {}).merge(options)
92
- id = opts.delete(:id) || id_from_array_or_arel(rows)
93
- end
94
- table_class = "table #{parse_table_classes(opts)}"
95
- [id, table_class]
96
- end
97
-
98
- def id_from_array_or_arel(rows)
99
- if rows.respond_to? :model
100
- rows.model.model_name.plural
101
- elsif rows.empty?
102
- "table"
103
- else
104
- rows.first.model_name.plural
105
- end
106
- rescue StandardError
107
- "table"
108
- end
109
-
110
- def parse_table_classes(options)
111
- options.slice(*%i[bordered striped narrow hoverable fullwidth])
112
- .transform_keys { |key| "is-#{key}" }
113
- .select { |_, value| value }
114
- .keys
115
- .join(" ")
116
- end
117
-
118
- # this derives a th class from the column html attributes
119
- # perhaps a better way would be pre-defined pairs?
120
- def table_header(column)
121
- attributes = {}
122
- attributes[:class] = header_alignment(column[:html_attributes])
123
- th(**attributes) { column[:header] }
124
- end
125
-
126
- def header_alignment(html_attributes)
127
- classes = html_attributes[:class]
128
- return if classes.nil?
129
-
130
- if classes&.include?("has-text-right") || classes&.include?("amount-display")
131
- "has-text-right"
132
- elsif classes&.include?("has-text-centered")
133
- "has-text-centered"
134
- end
135
- end
136
-
137
- def pagination
138
- return unless @path_builder
139
-
140
- tfoot do
141
- tr do
142
- td(class: "py-0", colspan: @columns.size) do
143
- Pagination(@rows, @path_builder)
144
- end
145
- end
146
- end
147
- end
148
- end
149
- end
150
- end
@@ -1,119 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Components
4
- module Bulma
5
- # Tabs component for toggling between different content sections
6
- #
7
- # This component implements the [Bulma tabs](https://bulma.io/documentation/components/tabs/)
8
- # interface, providing a way to toggle between different content sections using
9
- # tabbed navigation. Includes support for icons and active state management.
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
- #
21
- # ## Example
22
- #
23
- # ```ruby
24
- # Bulma::Tabs() do |tabs|
25
- # tabs.tab(id: "profile", title: "Profile", active: true) do
26
- # "Profile content goes here"
27
- # end
28
- #
29
- # tabs.tab(id: "settings", title: "Settings", icon: "fas fa-cog") do
30
- # "Settings content goes here"
31
- # end
32
- #
33
- # tabs.tab(id: "notifications", title: "Notifications", icon: "fas fa-bell") do
34
- # "Notifications content goes here"
35
- # end
36
- # end
37
- # ```
38
- #
39
- class Tabs < Components::Bulma::Base
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
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
70
- @tabs = []
71
- @contents = []
72
- end
73
-
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
84
- end
85
-
86
- def view_template(&)
87
- vanish(&)
88
-
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
96
-
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 }
116
- end
117
- end
118
- end
119
- end