Skip to content

Commit a16ebbc

Browse files
authored
Merge pull request #14224 from guardian/doml/progress
Tweak progress bar
2 parents 3900348 + 2ce76fd commit a16ebbc

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

dotcom-rendering/src/components/LoopVideoProgressBar.tsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ const foregroundStyles = (progressPercentage: number) => css`
1818
width: ${progressPercentage}%;
1919
z-index: ${getZIndex('loop-video-progress-bar-foreground')};
2020
background-color: ${palette('--loop-video-progress-bar-value')};
21-
transition: width 0.25s linear;
21+
/**
22+
* Don't show a transition when the progress bar returns to the start.
23+
*/
24+
transition: ${progressPercentage < 1 ? 'none' : `width 0.25s linear`};
2225
`;
2326

2427
type Props = {
@@ -31,7 +34,7 @@ type Props = {
3134
* A progress bar for the loop video component.
3235
*
3336
* Q. Why don't we use the <progress /> element?
34-
* A. It was not possible to properly style the native progress element in safari.
37+
* A. It was not possible to properly style the native progress element in Safari.
3538
*/
3639
export const LoopVideoProgressBar = ({
3740
videoId,
@@ -40,10 +43,24 @@ export const LoopVideoProgressBar = ({
4043
}: Props) => {
4144
if (duration <= 0) return null;
4245

43-
const progressPercentage = (currentTime * 100) / duration;
46+
/**
47+
* We achieve a smooth progress bar by using CSS transitions. Given that
48+
* onTimeUpdate firesevery 250ms or so, this means that the time on the
49+
* progress bar is always about 0.25s behind and begins 0.25s late.
50+
* Therefore, when calculating the progress percentage, we take 0.25s off the duration.
51+
*
52+
* Videos less than a second in duration will have no adjustment.
53+
*/
54+
const adjustedDuration = duration > 1 ? duration - 0.25 : duration;
55+
56+
const progressPercentage = Math.min(
57+
(currentTime * 100) / adjustedDuration,
58+
100,
59+
);
4460
if (Number.isNaN(progressPercentage)) {
4561
return null;
4662
}
63+
4764
const roundedProgressPercentage = Number(progressPercentage.toFixed(2));
4865

4966
return (

0 commit comments

Comments
 (0)