Document Picture-in-Picture API example
Document Picture-in-Picture API example. documentPictureInPicture.requestWindow() floats a document, not a video.
The problem
HTMLVideoElement.requestPictureInPicture() floats a <video>. That is all it can
float. A clock, a chat, or custom controls cannot go in that window.
The method
documentPictureInPicture.requestWindow() opens a real Window. You
append() any HTML. documentPictureInPicture.window is the handle.
enter fires when it opens. The floating window fires pagehide when it closes. There is
no leave event on DocumentPictureInPicture.
What to do
- Run
documentPictureInPicture. Compare it withpictureInPictureEnabled. - Run
requestWindow(). The panel leaves the stage. - Run
window, then Close.
documentPictureInPicture
The Document PiP entry. document.pictureInPictureEnabled is the older video API. A true there does
not mean requestWindow() exists.
window.documentPictureInPicture
document.pictureInPictureEnabled
requestWindow()
Needs a user gesture. Returns a Window. Browsers may clamp width and
height. Opening again closes the one that is already out.
const pipWindow = await documentPictureInPicture.requestWindow({
width: 320,
height: 200,
})
pipWindow.document.body.append(panel)
documentPictureInPicture.window
The last opened PiP window, or null. closed is a Window property, not an
event.
const pipWindow = documentPictureInPicture.window
pipWindow?.closed
pipWindow?.innerWidth
enter
Fires on documentPictureInPicture after the window opens. event.window is that
Window. Listen, then Run requestWindow().
documentPictureInPicture.addEventListener("enter", (event) => {
event.window
})
close()
Window.close(). The floating document fires pagehide. That is the close signal. Move
the nodes back on that event.
pipWindow.addEventListener("pagehide", () => {
slot.append(panel)
})
pipWindow.close()