forked from cookpete/react-player
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactPlayer.tsx
More file actions
143 lines (119 loc) · 4.02 KB
/
Copy pathReactPlayer.tsx
File metadata and controls
143 lines (119 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import React, { lazy, Suspense, useEffect, useState } from 'react';
import { defaultProps } from './props.js';
import Player from './Player.js';
import type { ReactPlayerProps } from './types.js';
import type { PlayerEntry } from './players.js';
const Preview = lazy(() => import(/* webpackChunkName: 'reactPlayerPreview' */ './Preview.js'));
const customPlayers: PlayerEntry[] = [];
type ReactPlayer = React.ForwardRefExoticComponent<
Omit<ReactPlayerProps, 'ref'> & React.RefAttributes<HTMLVideoElement>
> &
Partial<{
addCustomPlayer: (player: PlayerEntry) => void;
removeCustomPlayers: () => void;
canPlay: (src: string) => boolean;
canEnablePIP: (src: string) => boolean;
}>;
export const createReactPlayer = (players: PlayerEntry[], playerFallback: PlayerEntry) => {
const getActivePlayer = (src?: string) => {
for (const player of [...customPlayers, ...players]) {
if (src && player.canPlay(src)) {
return player;
}
}
if (playerFallback) {
return playerFallback;
}
return null;
};
const ReactPlayer: ReactPlayer = React.forwardRef((_props, ref) => {
const props = { ...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: React.SyntheticEvent) => {
setShowPreview(false);
props.onClickPreview?.(e);
};
const renderPreview = (src?: string) => {
if (!src) return null;
const { light, playIcon, previewTabIndex, oEmbedUrl, previewAriaLabel } = props;
return (
<Preview
src={src}
light={light}
playIcon={playIcon}
previewTabIndex={previewTabIndex}
previewAriaLabel={previewAriaLabel}
oEmbedUrl={oEmbedUrl}
onClickPreview={handleClickPreview}
/>
);
};
const renderActivePlayer = (src?: string) => {
const player = getActivePlayer(src);
if (!player) return null;
const { style, width, height, wrapper } = props;
const config = props.config?.[player.key as keyof ReactPlayerProps['config']];
return (
<Player
{...props}
ref={ref}
activePlayer={player.player ?? (player as unknown as PlayerEntry['player'])}
slot={wrapper ? undefined : slot}
className={wrapper ? undefined : className}
style={
wrapper
? { display: 'block', width: '100%', height: '100%' }
: { display: 'block', width, height, ...style }
}
config={config}
/>
);
};
const Wrapper: ReactPlayerProps['wrapper'] = wrapper == null ? ForwardChildren : wrapper;
const UniversalSuspense = fallback === false ? ForwardChildren : Suspense;
return (
<Wrapper slot={slot} className={className} style={{ width, height, ...style }}>
<UniversalSuspense fallback={fallback}>
{showPreview ? renderPreview(src) : renderActivePlayer(src)}
</UniversalSuspense>
</Wrapper>
);
});
ReactPlayer.displayName = 'ReactPlayer';
ReactPlayer.addCustomPlayer = (player: PlayerEntry) => {
customPlayers.push(player);
};
ReactPlayer.removeCustomPlayers = () => {
customPlayers.length = 0;
};
ReactPlayer.canPlay = (src?: string) => {
if (src) {
for (const Player of [...customPlayers, ...players]) {
if (Player.canPlay(src)) {
return true;
}
}
}
return false;
};
ReactPlayer.canEnablePIP = (src?: string) => {
if (src) {
for (const Player of [...customPlayers, ...players]) {
if (Player.canPlay(src) && Player.canEnablePIP?.()) {
return true;
}
}
}
return false;
};
return ReactPlayer;
};
const ForwardChildren = ({ children }: { children?: React.ReactNode }) => children;