UNPKG

react-player

Version:

A React component for playing a variety of URLs, including file paths, Mux, YouTube, Vimeo, and Wistia

97 lines (96 loc) 2.83 kB
import React, { useCallback, useEffect, useRef } from "react"; const Player = React.forwardRef((props, ref) => { const { playing, pip } = props; const Player2 = props.activePlayer; const playerRef = useRef(null); const startOnPlayRef = useRef(true); useEffect(() => { var _a, _b; if (!playerRef.current) return; if (playerRef.current.paused && playing === true) { playerRef.current.play(); } if (!playerRef.current.paused && playing === false) { playerRef.current.pause(); } playerRef.current.playbackRate = (_a = props.playbackRate) != null ? _a : 1; playerRef.current.volume = (_b = props.volume) != null ? _b : 1; }); useEffect(() => { var _a, _b, _c, _d, _e; if (!playerRef.current || !globalThis.document) return; if (pip && !document.pictureInPictureElement) { try { (_b = (_a = playerRef.current).requestPictureInPicture) == null ? void 0 : _b.call(_a); } catch (err) { } } if (!pip && document.pictureInPictureElement) { try { (_d = (_c = playerRef.current).exitPictureInPicture) == null ? void 0 : _d.call(_c); (_e = document.exitPictureInPicture) == null ? void 0 : _e.call(document); } catch (err) { } } }, [pip]); const handleLoadStart = (event) => { var _a, _b; startOnPlayRef.current = true; (_a = props.onReady) == null ? void 0 : _a.call(props); (_b = props.onLoadStart) == null ? void 0 : _b.call(props, event); }; const handlePlay = (event) => { var _a, _b; if (startOnPlayRef.current) { startOnPlayRef.current = false; (_a = props.onStart) == null ? void 0 : _a.call(props, event); } (_b = props.onPlay) == null ? void 0 : _b.call(props, event); }; if (!Player2) { return null; } const eventProps = {}; for (const key in props) { if (key.startsWith("on")) { eventProps[key] = props[key]; } } return /* @__PURE__ */ React.createElement( Player2, { ...eventProps, style: props.style, className: props.className, slot: props.slot, ref: useCallback( (node) => { playerRef.current = node; if (typeof ref === "function") { ref(node); } else if (ref !== null) { ref.current = node; } }, [ref] ), src: props.src, crossOrigin: props.crossOrigin, preload: props.preload, controls: props.controls, muted: props.muted, autoPlay: props.autoPlay, loop: props.loop, playsInline: props.playsInline, config: props.config, onLoadStart: handleLoadStart, onPlay: handlePlay }, props.children ); }); Player.displayName = "Player"; var Player_default = Player; export { Player_default as default };