@sybilion/uilib 1.4.30 → 1.4.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -18,6 +18,10 @@ function VimeoEmbed({ videoId, videoHash, gifMode = false, playButtonColor, clas
|
|
|
18
18
|
const iframeRef = useRef(null);
|
|
19
19
|
const containerRef = useRef(null);
|
|
20
20
|
const playerRef = useRef(null);
|
|
21
|
+
// Generation token: incremented on every [id, hash] change so that stale
|
|
22
|
+
// async destroys / ready callbacks from a previous video can't clobber the
|
|
23
|
+
// current player ref.
|
|
24
|
+
const generationRef = useRef(0);
|
|
21
25
|
const [isPlaying, setIsPlaying] = useState(false);
|
|
22
26
|
const [isPlayerReady, setIsPlayerReady] = useState(false);
|
|
23
27
|
// Intrinsic video aspect ratio (width / height) reported by the Vimeo SDK.
|
|
@@ -35,14 +39,23 @@ function VimeoEmbed({ videoId, videoHash, gifMode = false, playButtonColor, clas
|
|
|
35
39
|
// Cleanup player when id/hash changes
|
|
36
40
|
useEffect(() => {
|
|
37
41
|
setAspectRatio(null);
|
|
38
|
-
|
|
39
|
-
|
|
42
|
+
generationRef.current += 1;
|
|
43
|
+
const token = generationRef.current;
|
|
44
|
+
const previousPlayer = playerRef.current;
|
|
45
|
+
playerRef.current = null;
|
|
46
|
+
setIsPlayerReady(false);
|
|
47
|
+
setIsPlaying(false);
|
|
48
|
+
if (previousPlayer) {
|
|
49
|
+
previousPlayer
|
|
40
50
|
.destroy()
|
|
41
51
|
.catch(() => {
|
|
42
52
|
// Ignore destroy errors
|
|
43
53
|
})
|
|
44
54
|
.finally(() => {
|
|
45
|
-
|
|
55
|
+
if (generationRef.current !== token)
|
|
56
|
+
return;
|
|
57
|
+
if (playerRef.current)
|
|
58
|
+
return;
|
|
46
59
|
setIsPlayerReady(false);
|
|
47
60
|
setIsPlaying(false);
|
|
48
61
|
});
|
|
@@ -52,46 +65,60 @@ function VimeoEmbed({ videoId, videoHash, gifMode = false, playButtonColor, clas
|
|
|
52
65
|
const handleIframeLoad = useCallback(() => {
|
|
53
66
|
if (!iframeRef.current)
|
|
54
67
|
return;
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
// Ignore destroy errors
|
|
61
|
-
})
|
|
62
|
-
.finally(() => {
|
|
63
|
-
playerRef.current = null;
|
|
64
|
-
});
|
|
68
|
+
const token = generationRef.current;
|
|
69
|
+
const previousPlayer = playerRef.current;
|
|
70
|
+
playerRef.current = null;
|
|
71
|
+
if (previousPlayer) {
|
|
72
|
+
previousPlayer.destroy().catch(() => { });
|
|
65
73
|
}
|
|
66
74
|
try {
|
|
67
75
|
const player = new Player(iframeRef.current);
|
|
76
|
+
if (generationRef.current !== token) {
|
|
77
|
+
player.destroy().catch(() => { });
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
68
80
|
playerRef.current = player;
|
|
69
|
-
// Wait for player to be ready before setting up events
|
|
70
81
|
player
|
|
71
82
|
.ready()
|
|
72
83
|
.then(() => {
|
|
84
|
+
if (generationRef.current !== token) {
|
|
85
|
+
if (playerRef.current === player)
|
|
86
|
+
playerRef.current = null;
|
|
87
|
+
player.destroy().catch(() => { });
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
73
90
|
setIsPlayerReady(true);
|
|
74
|
-
// Read the video's intrinsic dimensions so the container can adopt
|
|
75
|
-
// the exact aspect ratio — eliminates letterbox stripes from the
|
|
76
|
-
// hardcoded box ratio mismatching the source video.
|
|
77
91
|
Promise.all([player.getVideoWidth(), player.getVideoHeight()])
|
|
78
92
|
.then(([w, h]) => {
|
|
93
|
+
if (generationRef.current !== token)
|
|
94
|
+
return;
|
|
79
95
|
if (w > 0 && h > 0)
|
|
80
96
|
setAspectRatio(w / h);
|
|
81
97
|
})
|
|
82
98
|
.catch(() => {
|
|
83
99
|
// Ignore dimension errors; CSS fallback ratio stays in effect
|
|
84
100
|
});
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
101
|
+
player.on('play', () => {
|
|
102
|
+
if (generationRef.current !== token)
|
|
103
|
+
return;
|
|
104
|
+
setIsPlaying(true);
|
|
105
|
+
});
|
|
106
|
+
player.on('pause', () => {
|
|
107
|
+
if (generationRef.current !== token)
|
|
108
|
+
return;
|
|
109
|
+
setIsPlaying(false);
|
|
110
|
+
});
|
|
111
|
+
player.on('ended', () => {
|
|
112
|
+
if (generationRef.current !== token)
|
|
113
|
+
return;
|
|
114
|
+
setIsPlaying(false);
|
|
115
|
+
});
|
|
89
116
|
})
|
|
90
117
|
.catch(() => {
|
|
91
118
|
// Ignore ready errors
|
|
92
119
|
});
|
|
93
120
|
}
|
|
94
|
-
catch
|
|
121
|
+
catch {
|
|
95
122
|
// Ignore initialization errors
|
|
96
123
|
}
|
|
97
124
|
}, []);
|
|
@@ -146,7 +173,7 @@ function VimeoEmbed({ videoId, videoHash, gifMode = false, playButtonColor, clas
|
|
|
146
173
|
observer.disconnect();
|
|
147
174
|
};
|
|
148
175
|
}, [play, pause]);
|
|
149
|
-
return (jsxs("div", { ref: containerRef, className: cn(S.root, className, isPlaying && S.isPlaying), style: aspectRatio ? { aspectRatio: String(aspectRatio) } : undefined, ...props, children: [jsx("iframe", { ref: iframeRef, src: embedUrl, className: S.iframe, allow: "autoplay; fullscreen; picture-in-picture; encrypted-media", allowFullScreen: true, title: "Vimeo video player", onLoad: handleIframeLoad }), !gifMode && (jsx("button", { type: "button", className: S.playButton, onClick: play, "aria-label": "Play video", children: jsx("svg", { className: S.playIcon, viewBox: "0 0 24 24", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: jsx("path", { d: "M8 5v14l11-7z", fill: "currentColor" }) }) }))] }));
|
|
176
|
+
return (jsxs("div", { ref: containerRef, className: cn(S.root, className, isPlaying && S.isPlaying), style: aspectRatio ? { aspectRatio: String(aspectRatio) } : undefined, ...props, children: [jsx("iframe", { ref: iframeRef, src: embedUrl, className: S.iframe, allow: "autoplay; fullscreen; picture-in-picture; encrypted-media", allowFullScreen: true, title: "Vimeo video player", onLoad: handleIframeLoad }, embedUrl), !gifMode && (jsx("button", { type: "button", className: S.playButton, onClick: play, "aria-label": "Play video", children: jsx("svg", { className: S.playIcon, viewBox: "0 0 24 24", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: jsx("path", { d: "M8 5v14l11-7z", fill: "currentColor" }) }) }))] }));
|
|
150
177
|
}
|
|
151
178
|
|
|
152
179
|
export { VimeoEmbed };
|
package/package.json
CHANGED
|
@@ -30,6 +30,10 @@ function VimeoEmbed({
|
|
|
30
30
|
const iframeRef = useRef<HTMLIFrameElement>(null);
|
|
31
31
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
32
32
|
const playerRef = useRef<Player | null>(null);
|
|
33
|
+
// Generation token: incremented on every [id, hash] change so that stale
|
|
34
|
+
// async destroys / ready callbacks from a previous video can't clobber the
|
|
35
|
+
// current player ref.
|
|
36
|
+
const generationRef = useRef(0);
|
|
33
37
|
const [isPlaying, setIsPlaying] = useState(false);
|
|
34
38
|
const [isPlayerReady, setIsPlayerReady] = useState(false);
|
|
35
39
|
// Intrinsic video aspect ratio (width / height) reported by the Vimeo SDK.
|
|
@@ -50,14 +54,23 @@ function VimeoEmbed({
|
|
|
50
54
|
// Cleanup player when id/hash changes
|
|
51
55
|
useEffect(() => {
|
|
52
56
|
setAspectRatio(null);
|
|
53
|
-
|
|
54
|
-
|
|
57
|
+
generationRef.current += 1;
|
|
58
|
+
const token = generationRef.current;
|
|
59
|
+
|
|
60
|
+
const previousPlayer = playerRef.current;
|
|
61
|
+
playerRef.current = null;
|
|
62
|
+
setIsPlayerReady(false);
|
|
63
|
+
setIsPlaying(false);
|
|
64
|
+
|
|
65
|
+
if (previousPlayer) {
|
|
66
|
+
previousPlayer
|
|
55
67
|
.destroy()
|
|
56
68
|
.catch(() => {
|
|
57
69
|
// Ignore destroy errors
|
|
58
70
|
})
|
|
59
71
|
.finally(() => {
|
|
60
|
-
|
|
72
|
+
if (generationRef.current !== token) return;
|
|
73
|
+
if (playerRef.current) return;
|
|
61
74
|
setIsPlayerReady(false);
|
|
62
75
|
setIsPlaying(false);
|
|
63
76
|
});
|
|
@@ -68,49 +81,61 @@ function VimeoEmbed({
|
|
|
68
81
|
const handleIframeLoad = useCallback(() => {
|
|
69
82
|
if (!iframeRef.current) return;
|
|
70
83
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
})
|
|
78
|
-
.finally(() => {
|
|
79
|
-
playerRef.current = null;
|
|
80
|
-
});
|
|
84
|
+
const token = generationRef.current;
|
|
85
|
+
|
|
86
|
+
const previousPlayer = playerRef.current;
|
|
87
|
+
playerRef.current = null;
|
|
88
|
+
if (previousPlayer) {
|
|
89
|
+
previousPlayer.destroy().catch(() => {});
|
|
81
90
|
}
|
|
82
91
|
|
|
83
92
|
try {
|
|
84
93
|
const player = new Player(iframeRef.current);
|
|
85
94
|
|
|
95
|
+
if (generationRef.current !== token) {
|
|
96
|
+
player.destroy().catch(() => {});
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
86
100
|
playerRef.current = player;
|
|
87
101
|
|
|
88
|
-
// Wait for player to be ready before setting up events
|
|
89
102
|
player
|
|
90
103
|
.ready()
|
|
91
104
|
.then(() => {
|
|
105
|
+
if (generationRef.current !== token) {
|
|
106
|
+
if (playerRef.current === player) playerRef.current = null;
|
|
107
|
+
player.destroy().catch(() => {});
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
92
111
|
setIsPlayerReady(true);
|
|
93
112
|
|
|
94
|
-
// Read the video's intrinsic dimensions so the container can adopt
|
|
95
|
-
// the exact aspect ratio — eliminates letterbox stripes from the
|
|
96
|
-
// hardcoded box ratio mismatching the source video.
|
|
97
113
|
Promise.all([player.getVideoWidth(), player.getVideoHeight()])
|
|
98
114
|
.then(([w, h]) => {
|
|
115
|
+
if (generationRef.current !== token) return;
|
|
99
116
|
if (w > 0 && h > 0) setAspectRatio(w / h);
|
|
100
117
|
})
|
|
101
118
|
.catch(() => {
|
|
102
119
|
// Ignore dimension errors; CSS fallback ratio stays in effect
|
|
103
120
|
});
|
|
104
121
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
122
|
+
player.on('play', () => {
|
|
123
|
+
if (generationRef.current !== token) return;
|
|
124
|
+
setIsPlaying(true);
|
|
125
|
+
});
|
|
126
|
+
player.on('pause', () => {
|
|
127
|
+
if (generationRef.current !== token) return;
|
|
128
|
+
setIsPlaying(false);
|
|
129
|
+
});
|
|
130
|
+
player.on('ended', () => {
|
|
131
|
+
if (generationRef.current !== token) return;
|
|
132
|
+
setIsPlaying(false);
|
|
133
|
+
});
|
|
109
134
|
})
|
|
110
135
|
.catch(() => {
|
|
111
136
|
// Ignore ready errors
|
|
112
137
|
});
|
|
113
|
-
} catch
|
|
138
|
+
} catch {
|
|
114
139
|
// Ignore initialization errors
|
|
115
140
|
}
|
|
116
141
|
}, []);
|
|
@@ -175,6 +200,7 @@ function VimeoEmbed({
|
|
|
175
200
|
{...props}
|
|
176
201
|
>
|
|
177
202
|
<iframe
|
|
203
|
+
key={embedUrl}
|
|
178
204
|
ref={iframeRef}
|
|
179
205
|
src={embedUrl}
|
|
180
206
|
className={S.iframe}
|