Skip to content

Commit 754ae6b

Browse files
committed
Replace CarouselContext with Tailwind group
1 parent 002845b commit 754ae6b

File tree

8 files changed

+36
-97
lines changed

8 files changed

+36
-97
lines changed

lib/ruby_ui/carousel/carousel.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,14 @@ def initialize(orientation: :horizontal, options: {}, **user_attrs)
1010
end
1111

1212
def view_template(&)
13-
CarouselContext.with_context(orientation: @orientation) do
14-
div(**attrs, &)
15-
end
13+
div(**attrs, &)
1614
end
1715

1816
private
1917

2018
def default_attrs
2119
{
22-
class: "relative",
20+
class: ["relative group", orientation_classes],
2321
role: "region",
2422
aria_roledescription: "carousel",
2523
data: {
@@ -34,5 +32,9 @@ def default_options
3432
axis: (@orientation == :horizontal) ? "x" : "y"
3533
}
3634
end
35+
36+
def orientation_classes
37+
(@orientation == :horizontal) ? "is-horizontal" : "is-vertical"
38+
end
3739
end
3840
end

lib/ruby_ui/carousel/carousel_content.rb

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,6 @@
22

33
module RubyUI
44
class CarouselContent < Base
5-
ORIENTATION_CLASSES = {
6-
horizontal: "-ml-4",
7-
vertical: "-mt-4 flex-col"
8-
}
9-
10-
def initialize(**attrs)
11-
@orientation = CarouselContext.orientation || :horizontal
12-
13-
super
14-
end
15-
165
def view_template(&)
176
div(class: "overflow-hidden", data: {ruby_ui__carousel_target: "viewport"}) do
187
div(**attrs, &)
@@ -23,7 +12,11 @@ def view_template(&)
2312

2413
def default_attrs
2514
{
26-
class: ["flex", ORIENTATION_CLASSES[@orientation]]
15+
class: [
16+
"flex",
17+
"group-[.is-horizontal]:-ml-4",
18+
"group-[.is-vertical]:-mt-4 group-[.is-vertical]:flex-col"
19+
]
2720
}
2821
end
2922
end

lib/ruby_ui/carousel/carousel_context.rb

Lines changed: 0 additions & 20 deletions
This file was deleted.

lib/ruby_ui/carousel/carousel_controller.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default class extends Controller {
2727

2828
this.carousel.on("init", this.#updateControls.bind(this))
2929
this.carousel.on("reInit", this.#updateControls.bind(this))
30-
this.carousel.on("select", this.#handleSelect.bind(this))
30+
this.carousel.on("select", this.#updateControls.bind(this))
3131
}
3232

3333
destroyCarousel() {
@@ -42,11 +42,6 @@ export default class extends Controller {
4242
this.carousel.scrollPrev()
4343
}
4444

45-
#handleSelect() {
46-
this.#updateControls()
47-
this.dispatch("select", { detail: { carousel: this.carousel } })
48-
}
49-
5045
#updateControls() {
5146
this.#toggleButtonsDisabledState(this.nextButtonTargets, !this.carousel.canScrollNext())
5247
this.#toggleButtonsDisabledState(this.prevButtonTargets, !this.carousel.canScrollPrev())

lib/ruby_ui/carousel/carousel_item.rb

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,6 @@
22

33
module RubyUI
44
class CarouselItem < Base
5-
ORIENTATION_CLASSES = {
6-
horizontal: "pl-4",
7-
vertical: "pt-4"
8-
}
9-
10-
def initialize(**attrs)
11-
@orientation = CarouselContext.orientation || :horizontal
12-
13-
super
14-
end
15-
165
def view_template(&)
176
div(**attrs, &)
187
end
@@ -23,7 +12,11 @@ def default_attrs
2312
{
2413
role: "group",
2514
aria_roledescription: "slide",
26-
class: ["min-w-0 shrink-0 grow-0 basis-full", ORIENTATION_CLASSES[@orientation]]
15+
class: [
16+
"min-w-0 shrink-0 grow-0 basis-full",
17+
"group-[.is-horizontal]:pl-4",
18+
"group-[.is-vertical]:pt-4"
19+
]
2720
}
2821
end
2922
end

lib/ruby_ui/carousel/carousel_next.rb

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,6 @@
22

33
module RubyUI
44
class CarouselNext < Base
5-
ORIENTATION_CLASSES = {
6-
horizontal: "-right-12 top-1/2 -translate-y-1/2",
7-
vertical: "-bottom-12 left-1/2 -translate-x-1/2 rotate-90"
8-
}
9-
10-
def initialize(**attrs)
11-
@orientation = CarouselContext.orientation || :horizontal
12-
13-
super
14-
end
15-
165
def view_template(&)
176
Button(**attrs) do
187
icon
@@ -25,7 +14,11 @@ def default_attrs
2514
{
2615
variant: :outline,
2716
icon: true,
28-
class: ["absolute h-8 w-8 rounded-full", ORIENTATION_CLASSES[@orientation]],
17+
class: [
18+
"absolute h-8 w-8 rounded-full",
19+
"group-[.is-horizontal]:-right-12 group-[.is-horizontal]:top-1/2 group-[.is-horizontal]:-translate-y-1/2",
20+
"group-[.is-vertical]:-bottom-12 group-[.is-vertical]:left-1/2 group-[.is-vertical]:-translate-x-1/2 group-[.is-vertical]:rotate-90"
21+
],
2922
disabled: true,
3023
data: {
3124
action: "click->ruby-ui--carousel#scrollNext",

lib/ruby_ui/carousel/carousel_previous.rb

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,6 @@
22

33
module RubyUI
44
class CarouselPrevious < Base
5-
ORIENTATION_CLASSES = {
6-
horizontal: "-left-12 top-1/2 -translate-y-1/2",
7-
vertical: "-top-12 left-1/2 -translate-x-1/2 rotate-90"
8-
}
9-
10-
def initialize(**attrs)
11-
@orientation = CarouselContext.orientation || :horizontal
12-
13-
super
14-
end
15-
165
def view_template(&)
176
Button(**attrs) do
187
icon
@@ -26,7 +15,11 @@ def default_attrs
2615
{
2716
variant: :outline,
2817
icon: true,
29-
class: ["absolute h-8 w-8 rounded-full", ORIENTATION_CLASSES[@orientation]],
18+
class: [
19+
"absolute h-8 w-8 rounded-full",
20+
"group-[.is-horizontal]:-left-12 group-[.is-horizontal]:top-1/2 group-[.is-horizontal]:-translate-y-1/2",
21+
"group-[.is-vertical]:-top-12 group-[.is-vertical]:left-1/2 group-[.is-vertical]:-translate-x-1/2 group-[.is-vertical]:rotate-90"
22+
],
3023
disabled: true,
3124
data: {
3225
action: "click->ruby-ui--carousel#scrollPrev",

test/ruby_ui/carousel_test.rb

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ def test_render_with_all_items
1616

1717
assert_match(/Item/, output)
1818
assert_match(/button/, output)
19+
assert_match(/ is-horizontal/, output)
1920
end
2021

21-
def test_render_with_vertical_orientation
22+
def test_render_with_horizontal_orientation
2223
output = phlex do
23-
RubyUI.Carousel(orientation: :vertical) do
24+
RubyUI.Carousel(orientation: :horizontal) do
2425
RubyUI.CarouselContent() do
2526
RubyUI.CarouselItem() { "Item" }
2627
end
@@ -29,31 +30,20 @@ def test_render_with_vertical_orientation
2930
end
3031
end
3132

32-
assert_match(/-mt-4 flex-col/, output)
33-
assert_match(/pt-4/, output)
34-
assert_match(/-top-12/, output)
35-
assert_match(/-bottom-12/, output)
36-
end
37-
38-
def test_sets_context_while_rendering
39-
phlex do
40-
RubyUI.Carousel(orientation: :test) do
41-
assert_equal ({orientation: :test}), Thread.current[:ruby_ui__carousel_state]
42-
end
43-
end
33+
assert_match(/ is-horizontal/, output)
4434
end
4535

46-
def test_clears_context_after_render
47-
phlex do
36+
def test_render_with_vertical_orientation
37+
output = phlex do
4838
RubyUI.Carousel(orientation: :vertical) do
49-
RubyUI.CarouselContent do
50-
RubyUI.CarouselItem { "Item" }
39+
RubyUI.CarouselContent() do
40+
RubyUI.CarouselItem() { "Item" }
5141
end
5242
RubyUI.CarouselPrevious()
5343
RubyUI.CarouselNext()
5444
end
5545
end
5646

57-
assert_nil Thread.current[:ruby_ui__carousel_state]
47+
assert_match(/ is-vertical/, output)
5848
end
5949
end

0 commit comments

Comments
 (0)