React hook for Picture in Picture
npm install react-use-pip




!npm downloads
React Hook for Picture in Picture.
Picture in Picture is a on going W3C draft so that websites to create a floating window to continue playing media while users interact with other content or sites. Before shipping your application with the feature, please be aware that the feature API is not widely supported yet across browsers and the implementation status can be found here. Please also see Can I Use for more information.
- Lightweight and easy to use.
- Picture in picture browser support detection.
- TypeScript friendly. react-use-pip hook is written in TypeScript.
- Picture in Picture mode control with your own custom Event handlers.
- react-use-pip supports React versions from 16.9.0 up. It also supports React 17 and React 18.
In your project directory, run
``bash`
npm install --save react-use-pip
Or with Yarn
`bash`
yarn add react-use-pip
`tsx
import usePictureInPicture from 'react-use-pip'
function VideoPlayer() {
const videoRef = useRef(null)
const {
isPictureInPictureActive,
isPictureInPictureAvailable,
togglePictureInPicture,
} = usePictureInPicture(videoRef)
return (
API
`ts
const {
isPictureInPictureActive,
isPictureInPictureAvailable,
togglePictureInPicture,
} = usePictureInPicture(videoRef, options)
`$3
-
videoRef: Ref to pass in to video element as a prop
- options: (optional) an object that provides the hook a set of callback functions.$3
-
isPictureInPictureActive = false: boolean that signals whether picture in picture mode is active or inactive
- isPictureInPictureAvailable: boolean that signals whether picture in picture mode is supported and enabled
- togglePictureInPicture(isActive: boolean) => void: function that provide you toggle picture in picture mode$3
-
onEnterPictureInPicture(event: Event) => void: (optional) callback function when entering picture in picture mode
- onLeavePictureInPicture(event: Event) => void: (optional) callback function when leaving picture in picture mode
- onRequestPictureInPictureError(error: any) => void: (optional) callback function when there is an error requesting picture in picture
- onExitPictureInPictureError(error: any) => void`: (optional) callback function when there is an error exiting picture in pictureMIT © DawChihLiou