Premise
as weāve already observed, Turbo Frames are ideal for exchanging content on the fly using their src
attribute. In this challenge, weāll look at how we can do that in a time-sensitive manner, for example as the time code of a video changes.
Starting Point
Our starting point contains a YouTube video in a <lite-youtube>
container element:
https://stackblitz.com/edit/turbo-frames-scrolling-lyrics?file=index.html%3AL63
(yes youāve just been RickRolled š)
It also contains a Turbo Frame with a placeholder for the lyrics:
https://stackblitz.com/edit/turbo-frames-scrolling-lyrics?file=index.html%3AL75
The server is equipped with an endpoint that responds to the /lyrics
route with the lyrics for a given timecode in seconds when passed as the t
param (e.g. /lyrics?t=10
):
https://stackblitz.com/edit/turbo-frames-scrolling-lyrics?file=index.js%3AL20
If thereās a match for the timecode in the lyrics
object, it responds with an appropriate Turbo Frame, else with a 404 error.
Challenge
Your challenge is to implement a āScrolling Lyricsā algorithm using the time updates sent by the player.
A word of warning: Because Iām using a YouTube video which is embedded in an <iframe>
, I have to do a little dance to listen for time updates from the YT Iframe Player API (hereās a Codepen for context). For a regular <video>
element, youād just listen for the timeupdate
event. But I thought it was funnier this way š
.
Now go ahead and
- implement the swapping of the Turbo Frameās
src
with the appropriate timecode in this callback: https://stackblitz.com/edit/turbo-frames-scrolling-lyrics?file=app.js%3AL31 - also, set the
time
elementās innerHTML to the current timecode - optimize your implementation to only fetch a new Turbo Frame from the server once per second.
Hereās a preview of the result:
Teaser
- How would you add view transitions to the swapping of Turbo Frames for a smoother experience?