Skip to content

Commit

Permalink
Update scroll_into_view stream helper to accept options
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoroth committed Jan 7, 2024
1 parent 8ce7cf5 commit 314c792
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ import 'controllers'
### Browser Actions

* `turbo_stream.reload(**attributes)`
* `turbo_stream.scroll_into_view(targets, **attributes)`
* `turbo_stream.scroll_into_view(**attributes)`
* `turbo_stream.scroll_into_view(targets)`
* `turbo_stream.scroll_into_view(targets, align_to_top)`
* `turbo_stream.scroll_into_view(targets, behavior:, block:, inline:)`
* `turbo_stream.set_focus(targets, **attributes)`
* `turbo_stream.set_title(title, **attributes)`

Expand Down
4 changes: 2 additions & 2 deletions lib/turbo_power/stream_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ def reload(**attributes)
custom_action :reload, attributes: attributes
end

def scroll_into_view(targets = nil, **attributes)
custom_action_all :scroll_into_view, targets: targets, attributes: attributes
def scroll_into_view(targets = nil, align_to_top = nil, **attributes)
custom_action_all :scroll_into_view, targets: targets, attributes: attributes.reverse_merge(align_to_top: align_to_top).compact
end

def set_cookie(cookie = nil, **attributes)
Expand Down
54 changes: 54 additions & 0 deletions test/turbo_power/stream_helper/scroll_into_view_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,60 @@ class ScrollIntoViewTest < StreamHelperTestCase
assert_dom_equal stream, turbo_stream.scroll_into_view("#element", targets: "#better-element")
end

test "scroll_into_view with target and align-to-top as positional args" do
stream = %(<turbo-stream align-to-top="true" targets="#element" action="scroll_into_view"><template></template></turbo-stream>)

assert_dom_equal stream, turbo_stream.scroll_into_view("#element", true)
end

test "scroll_into_view with target as positional arg and align-to-top as both arg and kwarg" do
stream = %(<turbo-stream targets="#element" action="scroll_into_view" align-to-top="false"><template></template></turbo-stream>)

assert_dom_equal stream, turbo_stream.scroll_into_view("#element", true, align_to_top: false)
end

test "scroll_into_view with target and align-to-top as positional args and additionl argumenets" do
stream = %(<turbo-stream align-to-top="true" something="else" targets="#element" action="scroll_into_view"><template></template></turbo-stream>)

assert_dom_equal stream, turbo_stream.scroll_into_view("#element", true, something: "else")
end

test "scroll_into_view with target as positional arg and block as kwarg" do
stream = %(<turbo-stream targets="#element" action="scroll_into_view" block="end"><template></template></turbo-stream>)

assert_dom_equal stream, turbo_stream.scroll_into_view("#element", block: "end")
end

test "scroll_into_view with target as positional arg and behavior as kwarg" do
stream = %(<turbo-stream targets="#element" action="scroll_into_view" behavior="smooth"><template></template></turbo-stream>)

assert_dom_equal stream, turbo_stream.scroll_into_view("#element", behavior: "smooth")
end

test "scroll_into_view with target as positional arg and inline as kwarg" do
stream = %(<turbo-stream inline="nearest" action="scroll_into_view" targets="#element"><template></template></turbo-stream>)

assert_dom_equal stream, turbo_stream.scroll_into_view("#element", inline: "nearest")
end

test "scroll_into_view with target as positional arg and options as kwargs" do
stream = %(<turbo-stream block="end" behavior="smooth" inline="nearest" action="scroll_into_view" targets="#element"><template></template></turbo-stream>)

assert_dom_equal stream, turbo_stream.scroll_into_view("#element", block: "end", behavior: "smooth", inline: "nearest")
end

test "scroll_into_view with target and options as kwargs" do
stream = %(<turbo-stream block="end" behavior="smooth" inline="nearest" action="scroll_into_view" target="#element"><template></template></turbo-stream>)

assert_dom_equal stream, turbo_stream.scroll_into_view(target: "#element", block: "end", behavior: "smooth", inline: "nearest")
end

test "scroll_into_view with target, align_to_top and options as kwargs" do
stream = %(<turbo-stream block="end" behavior="smooth" inline="nearest" align-to-top="true" action="scroll_into_view" target="#element"><template></template></turbo-stream>)

assert_dom_equal stream, turbo_stream.scroll_into_view(target: "#element", align_to_top: true, block: "end", behavior: "smooth", inline: "nearest")
end

test "scroll_into_view with additional arguments" do
stream = %(<turbo-stream targets="#element" something="else" action="scroll_into_view"><template></template></turbo-stream>)

Expand Down

0 comments on commit 314c792

Please sign in to comment.