项目作者: DawChihLiou

项目描述 :
React hook for Picture in Picture
高级语言: TypeScript
项目地址: git://github.com/DawChihLiou/react-use-pip.git
创建时间: 2020-08-18T08:10:44Z
项目社区:https://github.com/DawChihLiou/react-use-pip

开源协议:MIT License

下载


react-use-pip

npm
semantic-release
Package size
License
npm downloads

react-use-pip

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.

Features

  • 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.

Support

  • react-use-pip supports React versions from 16.9.0 up. It also supports React 17 and React 18.

Install

In your project directory, run

  1. npm install --save react-use-pip

Or with Yarn

  1. yarn add react-use-pip

Quick Start

  1. import usePictureInPicture from 'react-use-pip'
  2. function VideoPlayer() {
  3. const videoRef = useRef(null)
  4. const {
  5. isPictureInPictureActive,
  6. isPictureInPictureAvailable,
  7. togglePictureInPicture,
  8. } = usePictureInPicture(videoRef)
  9. return (
  10. <div className="App">
  11. <video ref={videoRef} autoPlay muted controls loop width="100%">
  12. <source src="video-sample.mp4" ></source>
  13. </video>
  14. {isPictureInPictureAvailable && (
  15. <button
  16. onClick={() => togglePictureInPicture(!isPictureInPictureActive)}
  17. >
  18. {isPictureInPictureActive ? 'Disable' : 'Enable'} Picture in Picture
  19. </button>
  20. )}
  21. </div>
  22. )
  23. }

API

  1. const {
  2. isPictureInPictureActive,
  3. isPictureInPictureAvailable,
  4. togglePictureInPicture,
  5. } = usePictureInPicture(videoRef, options)

Parameters

  • videoRef: Ref to pass in to video element as a prop
  • options: (optional) an object that provides the hook a set of callback functions.

Return Values

  • 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

Options (Optional)

  • 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 picture

License

MIT © DawChihLiou