-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Description 📖 This pull request adds support for passing `Hash` in `class` and `style` attributes in a way that feels idiomatic in Ruby and achieves composability. ### Background 📜 When writing Phlex components, things usually start simple: ```ruby div(id: "hero", class: "section") ``` but when you need a conditional class, things get a bit awkward: ```ruby div(id: "hero", **classes("section", ("active" if active))) ``` This proposal is meant to provide an alternative which is easier to understand, and easier to write: ```ruby div(id: "hero", class: ["section", active:]) ``` ### Examples 🧪 #### `class` A component such as: ```ruby def active = true def inactive = false def view_template div(class: ["dropdown", inactive:, active:]) end ``` would render: ```erb <div class="dropdown active"></div> ``` #### `style` A component such as: ```ruby def view_template div(style: {font_size: "16px", opacity: 0}) end ``` would render: ```erb <div style="font-size:16px;opacity:0;"></div> ``` ### Precedents This feels like the Ruby equivalent of what frameworks like [Vue](https://vuejs.org/guide/essentials/class-and-style.html) provide in their template system: - [Class and Style Bindings](https://vuejs.org/guide/essentials/class-and-style.html) As noted by @Spone, this behavior is consistent with the [`class_names`](https://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-token_list) helper in Rails.
- Loading branch information
Showing
2 changed files
with
149 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
{div: "div"}.each do |method_name, tag| | ||
describe "<#{tag}> with class array attribute" do | ||
example = Class.new(Phlex::HTML) do | ||
define_method :view_template do | ||
send(method_name, class: ["class", nil, inactive: false, truthy: 1]) { "content" } | ||
end | ||
end | ||
|
||
test "produces the correct output" do | ||
expect(example.call) == %(<#{tag} class="class truthy">content</#{tag}>) | ||
end | ||
end | ||
|
||
describe "<#{tag}> with class hash attribute" do | ||
example = Class.new(Phlex::HTML) do | ||
define_method :view_template do | ||
send(method_name, class: {class: true, inactive: false, truthy: 1}) { "content" } | ||
end | ||
end | ||
|
||
test "produces the correct output" do | ||
expect(example.call) == %(<#{tag} class="class truthy">content</#{tag}>) | ||
end | ||
end | ||
|
||
describe "<#{tag}> with style array attribute" do | ||
example = Class.new(Phlex::HTML) do | ||
define_method :view_template do | ||
send(method_name, style: ["color: red", nil, font_weight: "bold", opacity: 0]) { "content" } | ||
end | ||
end | ||
|
||
test "produces the correct output" do | ||
expect(example.call) == %(<#{tag} style="color: red;font-weight:bold;opacity:0;">content</#{tag}>) | ||
end | ||
end | ||
|
||
describe "<#{tag}> with style hash attribute" do | ||
example = Class.new(Phlex::HTML) do | ||
define_method :view_template do | ||
send(method_name, style: {color: "red", word_break: nil, font_weight: "bold"}) { "content" } | ||
end | ||
end | ||
|
||
test "produces the correct output" do | ||
expect(example.call) == %(<#{tag} style="color:red;font-weight:bold;">content</#{tag}>) | ||
end | ||
end | ||
end |