Urgent.News

What's breaking now, across hundreds of outlets.

Tech

How We Built Hover-to-Preview YouTube Cards in Vue & TypeScript

Video results on Recipe Finder are cards with a thumbnail, a title and a view count. Hovering a card for a moment mounts a muted YouTube player under the thumbnail, so you can see the dish being prepared before you decide to open it. Moving the pointer away removes the preview. Clicking the card opens the video on YouTube in a new tab. Three requirements shaped the design. The preview must never…

To create a preview of a YouTube video without it starting automatically, the design waited 400 milliseconds after a user rests their pointer on the card. If the pointer left before the timer expired, the preview did not load. Only devices with a real pointer, such as computers, would receive a preview; mobile devices like phones and tablets would not. Similarly, visitors who preferred reduced motion or had specific video IDs blocked from previews would also not receive a preview.

The preview was limited to a YouTube embed on the privacy-focused domain youtube-nocookie.com. This embed was built using a single list of player settings, including muted playback, no autoplay, no controls, no keyboard shortcuts, no fullscreen mode, and no annotations. The player only accepted messages from a single page origin, ensuring the preview could only be controlled by the intended site.

Communication between the web page and the embedded player was established using the postMessage API. The page sent messages to the player at regular intervals until the player responded, confirming it was ready. Once the connection was established, the page could send state change events and error messages to the player. The player, in turn, sent messages containing only the fields that had changed, such as the current video time, the total video duration, or whether the video was playing.

This allowed the web page to keep track of the video's progress and update the preview accordingly without starting the video accidentally or loading any unnecessary resources.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

More from Monday 23 November →