react-player
Version:
A React component for playing a variety of URLs, including file paths, Mux, YouTube, Vimeo, and Wistia
112 lines (111 loc) • 3.66 kB
JavaScript
import React, { lazy, Suspense, useEffect, useState } from "react";
import merge from "deepmerge";
import { defaultProps } from "./props.js";
import Player from "./Player.js";
const Preview = lazy(() => import(
/* webpackChunkName: 'reactPlayerPreview' */
"./Preview.js"
));
const customPlayers = [];
const createReactPlayer = (players, playerFallback) => {
const getActivePlayer = (src) => {
for (const player of [...customPlayers, ...players]) {
if (src && player.canPlay(src)) {
return player;
}
}
if (playerFallback) {
return playerFallback;
}
return null;
};
const ReactPlayer = React.forwardRef(({ children, ..._props }, ref) => {
const props = merge(defaultProps, _props);
const { src, slot, className, style, width, height, fallback, wrapper } = props;
const [showPreview, setShowPreview] = useState(!!props.light);
useEffect(() => {
if (props.light) {
setShowPreview(true);
} else {
setShowPreview(false);
}
}, [props.light]);
const handleClickPreview = (e) => {
var _a;
setShowPreview(false);
(_a = props.onClickPreview) == null ? void 0 : _a.call(props, e);
};
const renderPreview = (src2) => {
if (!src2) return null;
const { light, playIcon, previewTabIndex, oEmbedUrl, previewAriaLabel } = props;
return /* @__PURE__ */ React.createElement(
Preview,
{
src: src2,
light,
playIcon,
previewTabIndex,
previewAriaLabel,
oEmbedUrl,
onClickPreview: handleClickPreview
}
);
};
const renderActivePlayer = (src2) => {
var _a, _b;
const player = getActivePlayer(src2);
if (!player) return null;
const { style: style2, width: width2, height: height2, wrapper: wrapper2 } = props;
const config = (_a = props.config) == null ? void 0 : _a[player.key];
return /* @__PURE__ */ React.createElement(
Player,
{
...props,
ref,
activePlayer: (_b = player.player) != null ? _b : player,
slot: wrapper2 ? void 0 : slot,
className: wrapper2 ? void 0 : className,
style: wrapper2 ? { display: "block", width: "100%", height: "100%" } : { display: "block", width: width2, height: height2, ...style2 },
config
},
children
);
};
const Wrapper = wrapper == null ? ForwardChildren : wrapper;
const UniversalSuspense = fallback === false ? ForwardChildren : Suspense;
return /* @__PURE__ */ React.createElement(Wrapper, { slot, className, style: { width, height, ...style } }, /* @__PURE__ */ React.createElement(UniversalSuspense, { fallback }, showPreview ? renderPreview(src) : renderActivePlayer(src)));
});
ReactPlayer.displayName = "ReactPlayer";
ReactPlayer.addCustomPlayer = (player) => {
customPlayers.push(player);
};
ReactPlayer.removeCustomPlayers = () => {
customPlayers.length = 0;
};
ReactPlayer.canPlay = (src) => {
if (src) {
for (const Player2 of [...customPlayers, ...players]) {
if (Player2.canPlay(src)) {
return true;
}
}
}
return false;
};
ReactPlayer.canEnablePIP = (src) => {
var _a;
if (src) {
for (const Player2 of [...customPlayers, ...players]) {
if (Player2.canPlay(src) && ((_a = Player2.canEnablePIP) == null ? void 0 : _a.call(Player2))) {
return true;
}
}
}
return false;
};
return ReactPlayer;
};
const ForwardChildren = ({ children }) => children;
export {
createReactPlayer
};