Skip to content

Commit

Permalink
Responsive modifiers should be passed via a responsive: hash
Browse files Browse the repository at this point in the history
In preparation for conditional modifiers.
  • Loading branch information
davidalejandroaguilar committed Sep 24, 2024
1 parent c9717eb commit 6797879
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 37 deletions.
4 changes: 3 additions & 1 deletion lib/phlexy_ui/class_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ def html_classes_for_modifiers(modifiers, responsive_prefix: nil)
end

def add_responsive_modifiers_classes(classes)
return unless (responsive_options = options.delete(:responsive))

RESPONSIVE_PREFIXES.each do |responsive_prefix|
if (values = options.delete(responsive_prefix))
if (values = responsive_options[responsive_prefix])
classes.concat(
html_classes_for_modifiers(
Array(values),
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/phlexy_ui/badge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let(:component) do
Class.new(Phlex::HTML) do
def view_template(&)
render PhlexyUI::Badge.new(:neutral) do
render PhlexyUI::Badge.new(:neutral, primary: false, secondary: true) do
div do
"+"
end
Expand All @@ -24,7 +24,7 @@ def view_template(&)

it "is expected to match the formatted HTML" do
expected_html = html <<~HTML
<span class="badge badge-neutral"><div>+</div>Badge</span>
<span class="badge badge-neutral badge-secondary"><div>+</div>Badge</span>
HTML

is_expected.to eq(expected_html)
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/phlexy_ui/button_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
%i[sm md lg].each do |viewport|
context "when given an :#{viewport} responsive option as a single argument" do
subject(:output) do
render described_class.new(:neutral, viewport => :primary)
render described_class.new(:neutral, responsive: {viewport => :primary})
end

it "renders it separately with a responsive prefix" do
Expand All @@ -132,7 +132,7 @@

context "when given multiple responsive options as an array" do
subject(:output) do
render described_class.new(:neutral, viewport => [:primary, :active])
render described_class.new(:neutral, responsive: {viewport => [:primary, :active]})
end

it "renders it separately with a responsive prefix" do
Expand Down Expand Up @@ -165,7 +165,7 @@
end

subject(:output) do
render described_class.new(:neutral, viewport => [:primary, :active])
render described_class.new(:neutral, responsive: {viewport => [:primary, :active]})
end

it "renders it separately with a responsive prefix" do
Expand Down
8 changes: 4 additions & 4 deletions spec/lib/phlexy_ui/card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
%i[sm md lg].each do |viewport|
context "when given an :#{viewport} responsive option as a single argument" do
subject(:output) do
render described_class.new(:compact, viewport => :primary)
render described_class.new(:compact, responsive: {viewport => :primary})
end

it "renders it separately with a responsive prefix" do
Expand All @@ -205,7 +205,7 @@

context "when given multiple responsive options as an array" do
subject(:output) do
render described_class.new(:compact, viewport => [:normal, :primary])
render described_class.new(:compact, responsive: {viewport => [:normal, :primary]})
end

it "renders it separately with a responsive prefix" do
Expand Down Expand Up @@ -239,7 +239,7 @@
end

subject(:output) do
render described_class.new(:compact, viewport => [:normal, :primary])
render described_class.new(:compact, responsive: {viewport => [:normal, :primary]})
end

it "renders it separately with a responsive prefix" do
Expand Down Expand Up @@ -283,7 +283,7 @@
subject(:output) do
render described_class.new(
:my_modifier,
viewport => :my_other_modifier
responsive: {viewport => :my_other_modifier}
)
end

Expand Down
6 changes: 3 additions & 3 deletions spec/lib/phlexy_ui/drawer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
%i[sm md lg].each do |viewport|
context "when given an :#{viewport} responsive option as a single argument" do
subject(:output) do
render described_class.new(:open, viewport => :end, :id => :my_drawer)
render described_class.new(:open, responsive: {viewport => :end}, id: :my_drawer)
end

it "renders it separately with a responsive prefix" do
Expand All @@ -102,7 +102,7 @@

context "when given multiple responsive options as an array" do
subject(:output) do
render described_class.new(:open, viewport => [:open, :end], :id => :my_drawer)
render described_class.new(:open, responsive: {viewport => [:open, :end]}, id: :my_drawer)
end

it "renders it separately with a responsive prefix" do
Expand Down Expand Up @@ -135,7 +135,7 @@
end

subject(:output) do
render described_class.new(:open, viewport => [:open, :end], :id => :my_drawer)
render described_class.new(:open, responsive: {viewport => [:open, :end]}, id: :my_drawer)
end

it "renders it separately with a responsive prefix" do
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/phlexy_ui/menu_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
%i[sm md lg].each do |viewport|
context "when given an :#{viewport} responsive option as a single argument" do
subject(:output) do
render described_class.new(:xs, viewport => :vertical)
render described_class.new(:xs, responsive: {viewport => :vertical})
end

it "renders it separately with a responsive prefix" do
Expand All @@ -137,7 +137,7 @@

context "when given multiple responsive options as an array" do
subject(:output) do
render described_class.new(:xs, viewport => [:horizontal, :vertical])
render described_class.new(:xs, responsive: {viewport => [:horizontal, :vertical]})
end

it "renders it separately with a responsive prefix" do
Expand Down Expand Up @@ -170,7 +170,7 @@
end

subject(:output) do
render described_class.new(:xs, viewport => [:horizontal, :vertical])
render described_class.new(:xs, responsive: {viewport => [:horizontal, :vertical]})
end

it "renders it separately with a responsive prefix" do
Expand Down
34 changes: 28 additions & 6 deletions spec/lib/phlexy_ui/table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
%i[sm md lg].each do |viewport|
context "when given an :#{viewport} responsive option as a single argument" do
subject(:output) do
render described_class.new(:zebra, viewport => :pin_cols)
render described_class.new(:zebra, responsive: {viewport => :pin_cols})
end

it "renders it separately with a responsive prefix" do
Expand All @@ -185,7 +185,7 @@

context "when given multiple responsive options as an array" do
subject(:output) do
render described_class.new(:zebra, viewport => [:pin_cols, :xs])
render described_class.new(:zebra, responsive: {viewport => [:pin_cols, :xs]})
end

it "renders it separately with a responsive prefix" do
Expand Down Expand Up @@ -218,7 +218,7 @@
end

subject(:output) do
render described_class.new(:zebra, viewport => [:pin_cols, :xs])
render described_class.new(:zebra, responsive: {viewport => [:pin_cols, :xs]})
end

it "renders it separately with a responsive prefix" do
Expand Down Expand Up @@ -261,7 +261,7 @@
subject(:output) do
render described_class.new(
:my_modifier,
viewport => :my_other_modifier
responsive: {viewport => :my_other_modifier}
)
end

Expand Down Expand Up @@ -295,11 +295,33 @@
end
end

describe "conditional modifiers" do
subject(:output) do
render PhlexyUI::Table.new(
:zebra,
xs: false,
sm: true,
pin_cols: true,
responsive: {sm: :pin_cols}
)
end

it "renders it correctly" do
expected_html = html <<~HTML
<table
class="table table-zebra table-pin-cols table-sm sm:table-pin-cols">
</table>
HTML

expect(output).to eq(expected_html)
end
end

describe "rendering a full table" do
let(:component) do
Class.new(Phlex::HTML) do
def view_template(&)
render PhlexyUI::Table.new(:zebra) do |table|
render PhlexyUI::Table.new(:zebra, xs: false, sm: true, pin_cols: true) do |table|
table.header do |header|
header.row do |row|
row.head { "" }
Expand Down Expand Up @@ -345,7 +367,7 @@ def view_template(&)

it "is expected to match the formatted HTML" do
expected_html = html <<~HTML
<table class="table table-zebra">
<table class="table table-zebra table-pin-cols table-sm">
<thead>
<tr>
<th></th>
Expand Down
8 changes: 4 additions & 4 deletions spec/lib/phlexy_ui/tabs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
%i[sm md lg].each do |viewport|
context "when given an :#{viewport} responsive option as a single argument" do
subject(:output) do
render described_class.new(:boxed, viewport => :bordered)
render described_class.new(:boxed, responsive: {viewport => :bordered})
end

it "renders it separately with a responsive prefix" do
Expand All @@ -130,7 +130,7 @@

context "when given multiple responsive options as an array" do
subject(:output) do
render described_class.new(:boxed, viewport => [:boxed, :bordered])
render described_class.new(:boxed, responsive: {viewport => [:boxed, :bordered]})
end

it "renders it separately with a responsive prefix" do
Expand Down Expand Up @@ -163,7 +163,7 @@
end

subject(:output) do
render described_class.new(:boxed, viewport => [:boxed, :bordered])
render described_class.new(:boxed, responsive: {viewport => [:boxed, :bordered]})
end

it "renders it separately with a responsive prefix" do
Expand Down Expand Up @@ -212,7 +212,7 @@
let(:component) do
Class.new(Phlex::HTML) do
def view_template(&)
render PhlexyUI::Tabs.new :lifted, id: "my_tabs_2" do |tabs|
render PhlexyUI::Tabs.new :lifted, boxed: false, id: "my_tabs_2" do |tabs|
tabs.tab "Tab 1", :active, :closed, data: {my: :tabs} do |tab|
tab.content class: "bg-base-100 border-base-300 rounded-box p-6" do
"Tab content 1"
Expand Down
22 changes: 11 additions & 11 deletions spec/lib/phlexy_ui/tooltip_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
subject(:output) { render described_class.new }

describe "rendering via Kit" do
subject(:output) do
Tooltip tip: "A tooltip"
end

it "renders it correctly" do
expected_html = html <<~HTML
<div class="tooltip" data-tip="A tooltip"></div>
HTML

expect(output).to eq(expected_html)
end
subject(:output) do
Tooltip tip: "A tooltip"
end

it "renders it correctly" do
expected_html = html <<~HTML
<div class="tooltip" data-tip="A tooltip"></div>
HTML

expect(output).to eq(expected_html)
end
end

describe "passing :as option" do
subject(:output) { render described_class.new(as: :a, tip: "A tooltip") }
Expand Down

0 comments on commit 6797879

Please sign in to comment.