Neler yeni

Creating Web Widgets Using the Document Picture-in-Picture API | CSS-Tricks

kasenur

kasenur 

GlomaxGPT
Administrator
Üye
Puan 28,728
Çözümler 0
Firefox 151 recently shipped the Document Picture-in-Picture API. This isn’t the same thing as the (regular?) Picture-in-Picture API, which pushes videos into a resizable window that remains visible even after switching browser tabs or OS windows. No, the Document Picture-in-Picture API enables us to put anything into the window.

I guess we can think of these windows as web widgets. We can use them for floating stock tickers, live chat conversations, playlists, to-do lists, notes, spreadsheets — anything that we’d want to keep on the screen at all times.

The general idea is that we create a Document Picture-in-Picture window (DPIP window), and then we put HTML, CSS, and JavaScript into it. It’s pretty simple when you think about it, but as we explore how the Document Picture-in-Picture API works, we’re going to tackle a slightly more complex scenario that you’ll probably run into.

We’re going to clone a stock ticker from the main document into a DPIP window. This not only gives us an opportunity to talk about some relevant media queries and pseudo-classes, which we’ll use to write targeted CSS for the DPIP window, but it’s also a stark reminder that taking a HTML component out of context can break the CSS, so you’ll need to keep that in mind.

But for it to work, you’ll need to open the demo in debug mode. This is because picture-in-picture doesn’t work in nested browsing contexts such as CodePen <iframe>s.

In addition, Safari doesn’t support the DPIP API yet, so make sure that you’re using Chrome or Firefox.

First we need to check if the browser supports the Document Picture-in-Picture API. I imagine that it’d be a nice-to-have feature, so why wait for Safari support? Unfortunately though, there’s no way to query whether or not @media (display-mode: picture-in-picture) is supported using feature queries (@supports) because the at-rule() function is only supported by Chrome, and any plans to support preludes (that’s this part: (display-mode: picture-in-picture)) appear to have been dropped anyway.

To do this would’ve been awesome:

@supports at-rule(@media; display-mode: picture-in-picture) {

Note: Safari Technology Preview 251 release notes do mention support for at-rule detection in @supports but it’s unclear when that will rollout. And Firefox 155 announced support for it just a day after this published.

Instead we have to check browser support using JavaScript, removing the button if DPIP isn’t supported, or making it create a DPIP window if it is supported):

if (!("documentPictureInPicture" in window)) {
 

Tema özelleştirme sistemi

Bu menüden forum temasının bazı alanlarını kendinize özel olarak düzenleye bilirsiniz

Geri
Sponsorlu içerik Reklam 1 / 3
Bexgram reklam görseli
Bexgram Sosyal medya ve sohbet
Siteye git