Skip to content

Commit 5f90294

Browse files
authored
Update scroll_into_view stream helper to accept options (#40)
1 parent 8ce7cf5 commit 5f90294

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,10 @@ import 'controllers'
138138
### Browser Actions
139139

140140
* `turbo_stream.reload(**attributes)`
141-
* `turbo_stream.scroll_into_view(targets, **attributes)`
141+
* `turbo_stream.scroll_into_view(**attributes)`
142+
* `turbo_stream.scroll_into_view(targets)`
143+
* `turbo_stream.scroll_into_view(targets, align_to_top)`
144+
* `turbo_stream.scroll_into_view(targets, behavior:, block:, inline:)`
142145
* `turbo_stream.set_focus(targets, **attributes)`
143146
* `turbo_stream.set_title(title, **attributes)`
144147

lib/turbo_power/stream_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ def reload(**attributes)
176176
custom_action :reload, attributes: attributes
177177
end
178178

179-
def scroll_into_view(targets = nil, **attributes)
180-
custom_action_all :scroll_into_view, targets: targets, attributes: attributes
179+
def scroll_into_view(targets = nil, align_to_top = nil, **attributes)
180+
custom_action_all :scroll_into_view, targets: targets, attributes: attributes.reverse_merge(align_to_top: align_to_top).compact
181181
end
182182

183183
def set_cookie(cookie = nil, **attributes)

test/turbo_power/stream_helper/scroll_into_view_test.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,60 @@ class ScrollIntoViewTest < StreamHelperTestCase
2929
assert_dom_equal stream, turbo_stream.scroll_into_view("#element", targets: "#better-element")
3030
end
3131

32+
test "scroll_into_view with target and align-to-top as positional args" do
33+
stream = %(<turbo-stream align-to-top="true" targets="#element" action="scroll_into_view"><template></template></turbo-stream>)
34+
35+
assert_dom_equal stream, turbo_stream.scroll_into_view("#element", true)
36+
end
37+
38+
test "scroll_into_view with target as positional arg and align-to-top as both arg and kwarg" do
39+
stream = %(<turbo-stream targets="#element" action="scroll_into_view" align-to-top="false"><template></template></turbo-stream>)
40+
41+
assert_dom_equal stream, turbo_stream.scroll_into_view("#element", true, align_to_top: false)
42+
end
43+
44+
test "scroll_into_view with target and align-to-top as positional args and additionl argumenets" do
45+
stream = %(<turbo-stream align-to-top="true" something="else" targets="#element" action="scroll_into_view"><template></template></turbo-stream>)
46+
47+
assert_dom_equal stream, turbo_stream.scroll_into_view("#element", true, something: "else")
48+
end
49+
50+
test "scroll_into_view with target as positional arg and block as kwarg" do
51+
stream = %(<turbo-stream targets="#element" action="scroll_into_view" block="end"><template></template></turbo-stream>)
52+
53+
assert_dom_equal stream, turbo_stream.scroll_into_view("#element", block: "end")
54+
end
55+
56+
test "scroll_into_view with target as positional arg and behavior as kwarg" do
57+
stream = %(<turbo-stream targets="#element" action="scroll_into_view" behavior="smooth"><template></template></turbo-stream>)
58+
59+
assert_dom_equal stream, turbo_stream.scroll_into_view("#element", behavior: "smooth")
60+
end
61+
62+
test "scroll_into_view with target as positional arg and inline as kwarg" do
63+
stream = %(<turbo-stream inline="nearest" action="scroll_into_view" targets="#element"><template></template></turbo-stream>)
64+
65+
assert_dom_equal stream, turbo_stream.scroll_into_view("#element", inline: "nearest")
66+
end
67+
68+
test "scroll_into_view with target as positional arg and options as kwargs" do
69+
stream = %(<turbo-stream block="end" behavior="smooth" inline="nearest" action="scroll_into_view" targets="#element"><template></template></turbo-stream>)
70+
71+
assert_dom_equal stream, turbo_stream.scroll_into_view("#element", block: "end", behavior: "smooth", inline: "nearest")
72+
end
73+
74+
test "scroll_into_view with target and options as kwargs" do
75+
stream = %(<turbo-stream block="end" behavior="smooth" inline="nearest" action="scroll_into_view" target="#element"><template></template></turbo-stream>)
76+
77+
assert_dom_equal stream, turbo_stream.scroll_into_view(target: "#element", block: "end", behavior: "smooth", inline: "nearest")
78+
end
79+
80+
test "scroll_into_view with target, align_to_top and options as kwargs" do
81+
stream = %(<turbo-stream block="end" behavior="smooth" inline="nearest" align-to-top="true" action="scroll_into_view" target="#element"><template></template></turbo-stream>)
82+
83+
assert_dom_equal stream, turbo_stream.scroll_into_view(target: "#element", align_to_top: true, block: "end", behavior: "smooth", inline: "nearest")
84+
end
85+
3286
test "scroll_into_view with additional arguments" do
3387
stream = %(<turbo-stream targets="#element" something="else" action="scroll_into_view"><template></template></turbo-stream>)
3488

0 commit comments

Comments
 (0)