Scroll into view after append using a custom Turbo Stream action

10070
0

When appending new content (like a new message), I sometimes want to scroll it into view automatically. Rather than adding JS in the controller, I use a custom turbo stream action scroll_into_view that finds the target element and calls scrollIntoView. The server emits the action as part of the stream response right after the append. This keeps the behavior declarative and local to the response that needs it. It’s also easy to adjust: smooth scrolling, alignment, or conditional logic. The client action is a handful of lines and lives alongside other Turbo stream actions. I’m careful not to hijack scroll unexpectedly on long pages; usually I only auto-scroll if the user is already near the bottom.