@sybilion/uilib 1.4.29 → 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,8 +18,15 @@ 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);
|
|
27
|
+
// Intrinsic video aspect ratio (width / height) reported by the Vimeo SDK.
|
|
28
|
+
// Null until the player reports it; falls back to the CSS default meanwhile.
|
|
29
|
+
const [aspectRatio, setAspectRatio] = useState(null);
|
|
23
30
|
const parsed = parseVimeoUrl(videoId);
|
|
24
31
|
const id = parsed.id;
|
|
25
32
|
const hash = videoHash ?? parsed.hash;
|
|
@@ -31,14 +38,24 @@ function VimeoEmbed({ videoId, videoHash, gifMode = false, playButtonColor, clas
|
|
|
31
38
|
const embedUrl = `https://player.vimeo.com/video/${id}?${baseParams}&${gifParams}${hashParam}`;
|
|
32
39
|
// Cleanup player when id/hash changes
|
|
33
40
|
useEffect(() => {
|
|
34
|
-
|
|
35
|
-
|
|
41
|
+
setAspectRatio(null);
|
|
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
|
|
36
50
|
.destroy()
|
|
37
51
|
.catch(() => {
|
|
38
52
|
// Ignore destroy errors
|
|
39
53
|
})
|
|
40
54
|
.finally(() => {
|
|
41
|
-
|
|
55
|
+
if (generationRef.current !== token)
|
|
56
|
+
return;
|
|
57
|
+
if (playerRef.current)
|
|
58
|
+
return;
|
|
42
59
|
setIsPlayerReady(false);
|
|
43
60
|
setIsPlaying(false);
|
|
44
61
|
});
|
|
@@ -48,35 +65,60 @@ function VimeoEmbed({ videoId, videoHash, gifMode = false, playButtonColor, clas
|
|
|
48
65
|
const handleIframeLoad = useCallback(() => {
|
|
49
66
|
if (!iframeRef.current)
|
|
50
67
|
return;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
// Ignore destroy errors
|
|
57
|
-
})
|
|
58
|
-
.finally(() => {
|
|
59
|
-
playerRef.current = null;
|
|
60
|
-
});
|
|
68
|
+
const token = generationRef.current;
|
|
69
|
+
const previousPlayer = playerRef.current;
|
|
70
|
+
playerRef.current = null;
|
|
71
|
+
if (previousPlayer) {
|
|
72
|
+
previousPlayer.destroy().catch(() => { });
|
|
61
73
|
}
|
|
62
74
|
try {
|
|
63
75
|
const player = new Player(iframeRef.current);
|
|
76
|
+
if (generationRef.current !== token) {
|
|
77
|
+
player.destroy().catch(() => { });
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
64
80
|
playerRef.current = player;
|
|
65
|
-
// Wait for player to be ready before setting up events
|
|
66
81
|
player
|
|
67
82
|
.ready()
|
|
68
83
|
.then(() => {
|
|
84
|
+
if (generationRef.current !== token) {
|
|
85
|
+
if (playerRef.current === player)
|
|
86
|
+
playerRef.current = null;
|
|
87
|
+
player.destroy().catch(() => { });
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
69
90
|
setIsPlayerReady(true);
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
91
|
+
Promise.all([player.getVideoWidth(), player.getVideoHeight()])
|
|
92
|
+
.then(([w, h]) => {
|
|
93
|
+
if (generationRef.current !== token)
|
|
94
|
+
return;
|
|
95
|
+
if (w > 0 && h > 0)
|
|
96
|
+
setAspectRatio(w / h);
|
|
97
|
+
})
|
|
98
|
+
.catch(() => {
|
|
99
|
+
// Ignore dimension errors; CSS fallback ratio stays in effect
|
|
100
|
+
});
|
|
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
|
+
});
|
|
74
116
|
})
|
|
75
117
|
.catch(() => {
|
|
76
118
|
// Ignore ready errors
|
|
77
119
|
});
|
|
78
120
|
}
|
|
79
|
-
catch
|
|
121
|
+
catch {
|
|
80
122
|
// Ignore initialization errors
|
|
81
123
|
}
|
|
82
124
|
}, []);
|
|
@@ -131,7 +173,7 @@ function VimeoEmbed({ videoId, videoHash, gifMode = false, playButtonColor, clas
|
|
|
131
173
|
observer.disconnect();
|
|
132
174
|
};
|
|
133
175
|
}, [play, pause]);
|
|
134
|
-
return (jsxs("div", { ref: containerRef, className: cn(S.root, className, isPlaying && S.isPlaying), ...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" }) }) }))] }));
|
|
135
177
|
}
|
|
136
178
|
|
|
137
179
|
export { VimeoEmbed };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import styleInject from 'style-inject';
|
|
2
2
|
|
|
3
|
-
var css_248z = ".VimeoEmbed_root__22Izc{align-self:stretch;aspect-ratio:16/9
|
|
3
|
+
var css_248z = ".VimeoEmbed_root__22Izc{align-self:stretch;aspect-ratio:16/9;border-radius:var(--p-5);box-shadow:0 0 0 1px var(--border);flex-shrink:0;height:auto!important;height:0;margin:0;min-width:0;overflow:hidden;padding-bottom:0;position:relative}.VimeoEmbed_iframe__fuiCR{border:none;border-radius:var(--p-5);display:block;height:100%;left:0;margin:0;opacity:.5;padding:0;position:absolute;top:0;transition:opacity .5s ease;width:100%;z-index:1}.VimeoEmbed_isPlaying__IU81- .VimeoEmbed_iframe__fuiCR{opacity:1}.VimeoEmbed_playButton__2T-Wn{align-items:center;background:var(--background-alpha-500);border:none;box-shadow:0 2px 16px var(--page-color);cursor:pointer;display:flex;height:100%;justify-content:center;position:absolute;transition:all .2s ease;width:100%;z-index:3}.VimeoEmbed_playButton__2T-Wn:hover{background:transparent}.VimeoEmbed_isPlaying__IU81- .VimeoEmbed_playButton__2T-Wn{opacity:0;pointer-events:none}.VimeoEmbed_playIcon__GlKI1{color:var(--brand-color-400);height:var(--p-16);margin-left:4px;width:var(--p-16)}";
|
|
4
4
|
var S = {"root":"VimeoEmbed_root__22Izc","iframe":"VimeoEmbed_iframe__fuiCR","isPlaying":"VimeoEmbed_isPlaying__IU81-","playButton":"VimeoEmbed_playButton__2T-Wn","playIcon":"VimeoEmbed_playIcon__GlKI1"};
|
|
5
5
|
styleInject(css_248z);
|
|
6
6
|
|
package/package.json
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
margin 0
|
|
7
7
|
padding-bottom 0
|
|
8
8
|
|
|
9
|
-
aspect-ratio 16 / 9
|
|
9
|
+
aspect-ratio 16 / 9
|
|
10
10
|
overflow hidden
|
|
11
11
|
border-radius var(--p-5)
|
|
12
12
|
|
|
@@ -30,8 +30,6 @@
|
|
|
30
30
|
opacity .5
|
|
31
31
|
transition opacity 0.5s ease
|
|
32
32
|
|
|
33
|
-
transform scale(1.01) // fix rounded border cut off issue
|
|
34
|
-
|
|
35
33
|
.isPlaying &
|
|
36
34
|
opacity 1
|
|
37
35
|
|
|
@@ -30,8 +30,15 @@ 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);
|
|
39
|
+
// Intrinsic video aspect ratio (width / height) reported by the Vimeo SDK.
|
|
40
|
+
// Null until the player reports it; falls back to the CSS default meanwhile.
|
|
41
|
+
const [aspectRatio, setAspectRatio] = useState<number | null>(null);
|
|
35
42
|
|
|
36
43
|
const parsed = parseVimeoUrl(videoId);
|
|
37
44
|
const id = parsed.id;
|
|
@@ -46,14 +53,24 @@ function VimeoEmbed({
|
|
|
46
53
|
|
|
47
54
|
// Cleanup player when id/hash changes
|
|
48
55
|
useEffect(() => {
|
|
49
|
-
|
|
50
|
-
|
|
56
|
+
setAspectRatio(null);
|
|
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
|
|
51
67
|
.destroy()
|
|
52
68
|
.catch(() => {
|
|
53
69
|
// Ignore destroy errors
|
|
54
70
|
})
|
|
55
71
|
.finally(() => {
|
|
56
|
-
|
|
72
|
+
if (generationRef.current !== token) return;
|
|
73
|
+
if (playerRef.current) return;
|
|
57
74
|
setIsPlayerReady(false);
|
|
58
75
|
setIsPlaying(false);
|
|
59
76
|
});
|
|
@@ -64,38 +81,61 @@ function VimeoEmbed({
|
|
|
64
81
|
const handleIframeLoad = useCallback(() => {
|
|
65
82
|
if (!iframeRef.current) return;
|
|
66
83
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
})
|
|
74
|
-
.finally(() => {
|
|
75
|
-
playerRef.current = null;
|
|
76
|
-
});
|
|
84
|
+
const token = generationRef.current;
|
|
85
|
+
|
|
86
|
+
const previousPlayer = playerRef.current;
|
|
87
|
+
playerRef.current = null;
|
|
88
|
+
if (previousPlayer) {
|
|
89
|
+
previousPlayer.destroy().catch(() => {});
|
|
77
90
|
}
|
|
78
91
|
|
|
79
92
|
try {
|
|
80
93
|
const player = new Player(iframeRef.current);
|
|
81
94
|
|
|
95
|
+
if (generationRef.current !== token) {
|
|
96
|
+
player.destroy().catch(() => {});
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
82
100
|
playerRef.current = player;
|
|
83
101
|
|
|
84
|
-
// Wait for player to be ready before setting up events
|
|
85
102
|
player
|
|
86
103
|
.ready()
|
|
87
104
|
.then(() => {
|
|
105
|
+
if (generationRef.current !== token) {
|
|
106
|
+
if (playerRef.current === player) playerRef.current = null;
|
|
107
|
+
player.destroy().catch(() => {});
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
88
111
|
setIsPlayerReady(true);
|
|
89
112
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
113
|
+
Promise.all([player.getVideoWidth(), player.getVideoHeight()])
|
|
114
|
+
.then(([w, h]) => {
|
|
115
|
+
if (generationRef.current !== token) return;
|
|
116
|
+
if (w > 0 && h > 0) setAspectRatio(w / h);
|
|
117
|
+
})
|
|
118
|
+
.catch(() => {
|
|
119
|
+
// Ignore dimension errors; CSS fallback ratio stays in effect
|
|
120
|
+
});
|
|
121
|
+
|
|
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
|
+
});
|
|
94
134
|
})
|
|
95
135
|
.catch(() => {
|
|
96
136
|
// Ignore ready errors
|
|
97
137
|
});
|
|
98
|
-
} catch
|
|
138
|
+
} catch {
|
|
99
139
|
// Ignore initialization errors
|
|
100
140
|
}
|
|
101
141
|
}, []);
|
|
@@ -156,9 +196,11 @@ function VimeoEmbed({
|
|
|
156
196
|
<div
|
|
157
197
|
ref={containerRef}
|
|
158
198
|
className={cn(S.root, className, isPlaying && S.isPlaying)}
|
|
199
|
+
style={aspectRatio ? { aspectRatio: String(aspectRatio) } : undefined}
|
|
159
200
|
{...props}
|
|
160
201
|
>
|
|
161
202
|
<iframe
|
|
203
|
+
key={embedUrl}
|
|
162
204
|
ref={iframeRef}
|
|
163
205
|
src={embedUrl}
|
|
164
206
|
className={S.iframe}
|