Skip to content

Commit f5adb12

Browse files
authored
Merge pull request #14973 from guardian/doml/self-hosted-video-dimensions
Use dimensions from video element in self-hosted video
2 parents ef9eba9 + 76713a8 commit f5adb12

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

dotcom-rendering/src/components/SelfHostedVideo.importable.tsx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ export const SelfHostedVideo = ({
128128
sources,
129129
atomId,
130130
uniqueId,
131-
height,
132-
width,
131+
height: expectedHeight,
132+
width: expectedWidth,
133133
videoStyle,
134134
posterImage,
135135
fallbackImage,
@@ -158,14 +158,19 @@ export const SelfHostedVideo = ({
158158
null,
159159
);
160160
const [hasPageBecomeActive, setHasPageBecomeActive] = useState(false);
161-
162161
/**
163-
* Keep a track of whether the video has been in view. We only
164-
* want to pause the video if it has been in view.
162+
* Keeps track of whether the video has been in view.
163+
* For example, we only want to try to pause the video if it has been in view.
165164
*/
166165
const [hasBeenInView, setHasBeenInView] = useState(false);
167166
const [hasBeenPlayed, setHasBeenPlayed] = useState(false);
168167
const [hasTrackedPlay, setHasTrackedPlay] = useState(false);
168+
/**
169+
* The actual video is a better source of truth of its dimensions.
170+
* The width and height from props are useful to prevent CLS.
171+
*/
172+
const [width, setWidth] = useState(expectedWidth);
173+
const [height, setHeight] = useState(expectedHeight);
169174

170175
const VISIBILITY_THRESHOLD = 0.5;
171176

@@ -527,8 +532,14 @@ export const SelfHostedVideo = ({
527532
};
528533

529534
const handleLoadedData = () => {
530-
if (vidRef.current) {
531-
setHasAudio(doesVideoHaveAudio(vidRef.current));
535+
const video = vidRef.current;
536+
if (!video) return;
537+
538+
setHasAudio(doesVideoHaveAudio(video));
539+
540+
if (video.videoWidth > 0 && video.videoHeight > 0) {
541+
setWidth(video.videoWidth);
542+
setHeight(video.videoHeight);
532543
}
533544
};
534545

0 commit comments

Comments
 (0)