Skip to content

Commit 9563d35

Browse files
authored
feat(DefaultVideo): add onVideoEnd callback (#1282)
* feat(DefaultVideo): add onVideoEnd callback * fix(DefaultVideo): fix lint problems
1 parent 2bc5a42 commit 9563d35

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/components/DefaultVideo/DefaultVideo.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ interface DefaultVideoProps {
2121
export const DefaultVideo = React.forwardRef<DefaultVideoRefType, DefaultVideoProps>(
2222
(props, ref) => {
2323
const {video, qa, customBarControlsClassName} = props;
24-
const {controls, customControlsOptions, muted: initiallyMuted = true} = video;
24+
const {controls, customControlsOptions, muted: initiallyMuted = true, onVideoEnd} = video;
2525
const {
2626
muteButtonShown,
2727
positioning,
@@ -44,6 +44,23 @@ export const DefaultVideo = React.forwardRef<DefaultVideoRefType, DefaultVideoPr
4444

4545
return videoRef.current;
4646
}, [videoRef]);
47+
48+
React.useEffect(() => {
49+
const videoElement = videoRef.current;
50+
if (!videoElement || !onVideoEnd) {
51+
return undefined;
52+
}
53+
54+
const handleVideoEnd = () => {
55+
onVideoEnd?.();
56+
};
57+
58+
videoElement.addEventListener('ended', handleVideoEnd);
59+
return () => {
60+
videoElement.removeEventListener('ended', handleVideoEnd);
61+
};
62+
}, [videoRef, onVideoEnd]);
63+
4764
const onPlayToggle = React.useCallback(() => {
4865
setIsPaused((value) => {
4966
if (value) {

src/components/Media/__stories__/Media.stories.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,15 @@ export const Iframe = IframeTemplate.bind({});
3535

3636
Image.args = data.image.content;
3737
ImageSlider.args = data.imageSlider.content;
38-
Video.args = data.video.content;
38+
Video.args = {
39+
...data.video.content,
40+
video: {
41+
...data.video.content.video,
42+
onVideoEnd: () => {
43+
console.log('Video has ended, onVideoEnd callback has fired');
44+
},
45+
},
46+
};
3947
Youtube.args = data.youtube.content;
4048
DataLens.args = data.dataLens.content;
4149
DataLensDarkTheme.args = data.dataLensDarkTheme.content as MediaProps;

src/models/constructor-items/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ export interface MediaVideoProps extends AnalyticsEventsBase {
189189
customControlsOptions?: CustomControlsOptions;
190190
ariaLabel?: string;
191191
contain?: boolean;
192+
onVideoEnd?: () => void;
192193
}
193194

194195
// links

0 commit comments

Comments
 (0)