Move your custom video player into Document Picture-in-Picture (controls included)
TL;DR video.requestPictureInPicture() only takes the <video> , so your custom controls vanish. documentPictureInPicture.requestWindow() opens an always-on-top window you fill with your own DOM. We'll move a live player (hls.js included) into it without interrupting playback, copy styles, fix keyboard shortcuts, move it back on close, and fall back to classic PiP where Document PiP doesn't exist.…
In the world of custom video players, a new method has emerged to enhance user experience through Document Picture-in-Picture (PiP) support. Unlike the traditional Picture-in-Picture feature limited to the built-in player controls, this innovative approach allows custom controls to remain active even when the video is in PiP mode.
The process begins with the standard custom player setup, consisting of a container, video, and control bar. The source code for this player is provided, highlighting the use of a video element, control elements, and the HLs.js library for live streaming.
The core of the solution lies in the `openDocPip` function. This function employs the `requestWindow()` method provided by the `documentPictureInPicture` API to create a new document window for the PiP view. By specifying the desired width and height, the new window is generated, ready to host the custom player.
To ensure a seamless transition, the custom player is moved into the newly created PiP window using the `append()` method. This action does not duplicate the player but rather relocates it directly into the new document, preserving all its functionality. Additionally, the styles of the original player are copied over to maintain visual consistency, even in the PiP window.
A crucial step in this process is the addition of event listeners to handle the closing of the PiP window. When the user triggers the close action, either by pressing the designated key or navigating away from the PiP view, the player is smoothly returned to its original position within the main document. This ensures that the video playback resumes without any interruptions, providing a consistent experience across both the PiP and main document modes.
Despite the apparent simplicity of these steps, there are important considerations to keep in mind. The `requestWindow()` method must be invoked from a user gesture, such as a click event, to avoid rejection. Additionally, the PiP window supports keyboard shortcuts and can seamlessly transition between PiP and main document modes without losing playback or control.
It's worth noting that while this method works flawlessly in Chromium 158 with the HLs.js library, compatibility may vary across different browsers and versions. As of November 2026, Chrome and Edge desktop versions above 116 support PiP, with specific features like `disallowReturnToOpener` and `preferInitialWindowPlacement` enhancing user control. Firefox desktop, starting from version 151, has also adopted PiP support, marking a significant step towards cross-browser compatibility.
In conclusion, by leveraging the power of `documentPictureInPicture`, developers can elevate custom video players by integrating PiP functionality without compromising on user experience. This approach not only enhances accessibility but also allows for a more dynamic and interactive viewing experience, setting a new standard in web video playback.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to