Skip to content

useDocumentPIP

See: (Document-Picture-in-Picture API).

Demo

INFO

The component uses useDocumentPIP hook to show in a separate window the counter showed renderes by the component.

Loading demo…
Show source code
tsx
import { useCallback, useState } from "react";
import { useDocumentPIP } from "../../../..";

export const UseDocumentPIP = () => {
	const [show, setShow] = useState(true);
	const [c, setC] = useState(0);
	const { isSupported, openPIP, PipWindow, closePIP } = useDocumentPIP({
		onOpened: useCallback(() => setShow(false), []),
		onClose: useCallback(() => setShow(true), [])
	});

	return <div>
		<p>Window Counter</p>
		<button onClick={() => setC(c => c + 1)}>{c}</button>
		<p>Supported: {isSupported ? 'Yes' : 'No'}</p>
		{
			show &&
			<>
				<div>
					<button
						onClick={() => openPIP({
							inheritCSS: true
						})}
					>
						Open PIP
					</button>
				</div>
			</>
		}
		<PipWindow>
			<div style={{display: 'flex', flexDirection: "column", alignItems: "center", width: "100%", gap: 20}}>
				<h4 style={{textAlign: "center"}}>PIP Window Counter</h4>
				<button onClick={() => setC(c => c + 1)}>{c}</button>
				<button onClick={closePIP}>Close</button>
			</div>
		</PipWindow>
	</div>
}

Types

DocumentPictureInPictureEvent

Event fired when the Document Picture-in-Picture window is successfully opened.

PropertyTypeRequiredDescription
windowWindow

DocumentPIPOptions

Configuration options for the Document Picture-in-Picture window.

PropertyTypeRequiredDescription
window{ width: number; height: number; }Initial dimensions for the PiP window.
inheritCSSbooleanIf true, clones the parent document's stylesheets into the PiP window. Useful for maintaining consistent UI styling.

UseDocumentPIPProps

Props accepted by useDocumentPIP.

PropertyTypeRequiredDescription
optionsDocumentPIPOptionsDefault options to apply when opening the PiP window.
onOpen() => voidCallback triggered immediately when the opening process starts.
onOpened(evt: DocumentPictureInPictureEvent) => voidCallback triggered once the PiP window is open and the document is available.
onClose(evt: PageTransitionEvent) => voidCallback triggered when the PiP window is closed.
onError(err: unknown) => voidCallback triggered if the PiP request fails (e.g., user denial or unsupported environment).

UseDocumentPIPResult

Result returned by useDocumentPIP.

PropertyTypeRequiredDescription
isSupportedbooleanIndicates if the Document Picture-in-Picture API is supported by the current browser.
openPIP(opts?: DocumentPIPOptions) => Promise<void>Opens the PiP window.
closePIP() => voidManually closes the active PiP window.
windowWindowThe current PiP window instance, or undefined if no window is active.
PipWindow(props: PropsWithChildren) => JSX.Element \| nullA React component that renders its children inside the PiP window using a Portal. Returns null if the PiP window is not currently open.

Released under the MIT License.