How to Play a Video in Picture-in-Picture with React
When you want to open another tab or a document while watching a video, you can move only the video into a small window in the corner of the screen and keep it playing. This is Picture-in-Picture (PiP). In React, you call requestPictureInPicture() on a <video> element inside a button click handler, and you track whether the video is in the small window by receiving events from the video and reflecting them in state.
Here I built a hook called usePictureInPicture that handles moving the video in and out of the small window. I also checked in a real browser what happens when the window is closed with its × button and when the video component is removed.
The test environment was Ubuntu 24.04, Node.js 22.23.4, and Chromium 153. I used React 19.3.1, TypeScript 7.0.2, esbuild 0.28.2, and Playwright 1.63.0. I clicked the small window's buttons and the video's standard menu items with a real mouse, moved by xdotool, in headed Chromium running on a virtual display (Xvfb). The video was a test clip with no audio. I did not test Firefox, Safari, mobile browsers, automatic switching to the small window when the tab changes, or the Document Picture-in-Picture API, which can put HTML other than <video> into the small window.

- 1. Which browsers support it
- 2. A hook that handles moving in and out of the small window
- 3. Opening and closing by other means keeps the display in sync
- 4. The small window stays open after the video component is removed
- 5. Call it inside a button click
- 6. Use disablePictureInPicture for videos that should not go to the small window
Which browsers support it
According to MDN's compatibility data, requestPictureInPicture() is supported in Chrome and Edge from version 69, in Chrome for Android from version 105, in Safari from 13.1, and in Firefox from 153.
To check whether it can be used, look at document.pictureInPictureEnabled. In browsers that do not support it, this property does not exist at all, so check for it with in before reading the value. On a page where Permissions Policy disables the feature, the value is false. In both cases, the component I build later does not show the button. I removed this property in Chromium to test it, and the button did not appear, while the video was still displayed.
A hook that handles moving in and out of the small window
To move a video into the small window, call video.requestPictureInPicture(). To return it, call document.exitPictureInPicture(). You can tell which video is currently in the small window from document.pictureInPictureElement.
// usePictureInPicture.ts
import { useCallback, useEffect, useState, type RefObject } from 'react';
export function isPictureInPictureSupported(): boolean {
return 'pictureInPictureEnabled' in document && document.pictureInPictureEnabled;
}
export function usePictureInPicture(videoRef: RefObject<HTMLVideoElement | null>) {
const [isActive, setIsActive] = useState(false);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
const video = videoRef.current;
if (!video) return;
const onEnter = () => setIsActive(true);
const onLeave = () => setIsActive(false);
video.addEventListener('enterpictureinpicture', onEnter);
video.addEventListener('leavepictureinpicture', onLeave);
return () => {
video.removeEventListener('enterpictureinpicture', onEnter);
video.removeEventListener('leavepictureinpicture', onLeave);
// If the component is removed while playing in the small window, the window would remain, so close it
if (document.pictureInPictureElement === video) {
document.exitPictureInPicture().catch(() => {});
}
};
}, [videoRef]);
const toggle = useCallback(async () => {
const video = videoRef.current;
if (!video) return;
setError(null);
try {
if (document.pictureInPictureElement === video) {
await document.exitPictureInPicture();
} else {
await video.requestPictureInPicture();
}
} catch (e) {
setError(e instanceof DOMException ? `${e.name}: ${e.message}` : String(e));
}
}, [videoRef]);
return { isActive, error, toggle };
}
isActive is not changed inside toggle. It switches only on the enterpictureinpicture and leavepictureinpicture events that reach the video. This is because the small window can also be opened and closed by operations other than our own button (I check this in the next section).
React's JSX has no onEnterPictureInPicture style prop. The event name does not appear anywhere in react-dom 19.3.1 or @types/react 19.3.0. For that reason, I register the listeners with addEventListener inside useEffect. The types are included in the standard TypeScript 7.0.2 type definitions (lib.dom), so tsc passed without adding any declarations of my own.
On the component side, the hook is connected to a button:
// PipVideo.tsx
import { useRef, useState } from 'react';
import { isPictureInPictureSupported, usePictureInPicture } from './usePictureInPicture';
type Props = { src: string; label: string };
export function PipVideo({ src, label }: Props) {
const videoRef = useRef<HTMLVideoElement>(null);
const { isActive, error, toggle } = usePictureInPicture(videoRef);
const [ready, setReady] = useState(false);
const supported = isPictureInPictureSupported();
return (
<section className="player" data-label={label}>
<video
ref={videoRef}
src={src}
controls
muted
playsInline
onLoadedMetadata={() => setReady(true)}
/>
{supported && (
<button type="button" onClick={toggle} disabled={!ready}>
{isActive ? '元の場所に戻す' : '小さい窓で再生'}
</button>
)}
<p className="status" role="status">
{isActive ? '小さい窓で再生しています' : 'このページの中で再生しています'}
</p>
{error && <p className="error">{error}</p>}
</section>
);
}
The button stays disabled until the video has loaded its duration and dimensions (metadata). When I called requestPictureInPicture() before loading (when readyState was 0), I got this error:
InvalidStateError: Failed to execute 'requestPictureInPicture' on 'HTMLVideoElement': Metadata for the video element are not loaded yet.
Opening and closing by other means keeps the display in sync
Chromium's small window has a × button and a "Back to tab" button. The video's standard controls also have a "⋮" menu with "Picture in picture". I pressed each of these with xdotool and checked whether the hook's state followed.
| Operation | Event received | Button and status text | Video |
|---|---|---|---|
| "Picture in picture" in the "⋮" menu | enterpictureinpicture |
Return to original place; Playing in small window | — |
| × button on the small window | leavepictureinpicture |
Play in small window; Playing in this page | Paused |
| "Back to tab" button on the small window | leavepictureinpicture |
Play in small window; Playing in this page | Kept playing |
The event arrived for every operation, so the button text and status display never got out of sync.
I also tested a page with two videos. While the first video was in the small window, I pressed the play-in-small-window button on the second. The first video received leavepictureinpicture and the second received enterpictureinpicture. In the test Chromium, only one video can be in the small window at a time, and the button labels on both videos switched correctly.
Note that MDN's Picture-in-Picture API documentation says enterpictureinpicture does not fire when the browser, rather than the page's code, moves a video into the small window. Examples given include switching tabs, and the user choosing it from the video's context menu or from the browser UI. In that case, it says you should register a handler with navigator.mediaSession.setActionHandler('enterpictureinpicture', …). However, in the test Chromium 153, as the table above shows, the event also arrived when entering from the video controls' "⋮" menu.
The small window stays open after the video component is removed
I also tested a version of the hook with the if block in the cleanup function removed (the three lines that call document.exitPictureInPicture()). While a video was playing in the small window, I removed the video with the parent component. The result was as follows.

The number of <video> elements in the page dropped to 0. However, document.pictureInPictureElement still pointed to the old <video> that had been removed from the page. leavepictureinpicture never fired. Three seconds after removal, the playback position of the small-window video kept advancing, from 0.40 seconds to 3.41 seconds.
Then I brought the component back with "show video". The old <video> was still in the small window, separate from the new <video>. The new video showed "Playing in this page", and the leftover small window was not reflected in the display.
To fix this, I made the hook's useEffect cleanup function call document.exitPictureInPicture() when its own video is in the small window. As described in React's useEffect documentation, cleanup functions also run after the component has been removed from the DOM. With this version, removing the video closed the small window, and document.pictureInPictureElement became null. All tests were run under development <StrictMode>. Even though setup and cleanup ran one extra time on mount, toggling with the button still worked correctly.
Call it inside a button click
requestPictureInPicture() must be called from a user action such as a button click. When I called it automatically after the page loaded, without any user interaction, I got this error:
NotAllowedError: Failed to execute 'requestPictureInPicture' on 'HTMLVideoElement': Must be handling a user gesture if there isn't already an element in Picture-in-Picture.
The Picture-in-Picture specification's steps for requestPictureInPicture() also specify that when nothing is yet in the small window, the call raises NotAllowedError unless it happens right after a user action (transient activation). So you cannot design the page to start playing in the small window as soon as it opens.
Use disablePictureInPicture for videos that should not go to the small window
To prevent a video from going into the small window, add disablePictureInPicture to the <video> element. You can write it directly in React's JSX.
<video src="/v.webm" controls muted disablePictureInPicture />
In Chromium, the rendered element became <video src="/v.webm" controls="" disablepictureinpicture="">, and no warning appeared in the console. Calling requestPictureInPicture() on this video threw InvalidStateError.
However, this is a request from the site to the browser. According to MDN's description of disablePictureInPicture, Firefox lets users override this setting in their preferences. The compatibility data also lists Firefox as partially supporting it: the small-window button is hidden, but users can still put the video into the small window.





