Turbo Streams - Custom Stream Actions

Use custom Turbo Stream actions to orchestrate complex UI animations.

View Solution on Patreon
Turbo Streams - Custom Stream Actions

Premise

Turbo Streams come with 7 default actions that under the usual circumstances provide enough functionality to power reactivity in your app. The downside is that it can become quite verbose, up to the point of Turbo Spaghetti code. To ameliorate this, Turbo provides an interface to implement custom stream actions for your app.

Challenge

This time, our example code contains a few Shoelace elements:

Your challenge is - using the techniques from Challenge 8 - upon click on the open-button button, to create a custom Turbo Stream action (or a sequence thereof) that will

An Action Bar Animation

Note: you have to wrap each button in an <sl-animation> component first.

Hint: use the dialog’s native events.

Caveats

There are two ways to accomplish the sequential animation:

  • Promises (async/await),
  • Recursive setTimeouts

Teaser

Orchestrating UI animations like this from the server side is one of the superpowers of Turbo. Can you think of a spot in an app of yours where this could be useful? And more high-level - what about making custom stream actions composable?

More from

Hotwire Combobox with Real Time Data
12 March 2024

Update combobox options using Websockets and Stimulus outlets

Turbo Streams - Custom Stream Actions - LocalStorage
30 January 2024

Store ephemeral state changes locally using localStorage and custom Turbo Stream Actions.

Turbo Streams - Custom Stream Actions - Video Playlist Management
10 October 2023

Managing a video playlist using custom Turbo Stream Actions.

Cookies
essential