@@ -18,7 +18,10 @@ const foregroundStyles = (progressPercentage: number) => css`
18
18
width: ${ progressPercentage } %;
19
19
z- index: ${ getZIndex ( 'loop-video-progress-bar-foreground' ) } ;
20
20
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` } ;
22
25
` ;
23
26
24
27
type Props = {
@@ -31,7 +34,7 @@ type Props = {
31
34
* A progress bar for the loop video component.
32
35
*
33
36
* 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 .
35
38
*/
36
39
export const LoopVideoProgressBar = ( {
37
40
videoId,
@@ -40,10 +43,24 @@ export const LoopVideoProgressBar = ({
40
43
} : Props ) => {
41
44
if ( duration <= 0 ) return null ;
42
45
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
+ ) ;
44
60
if ( Number . isNaN ( progressPercentage ) ) {
45
61
return null ;
46
62
}
63
+
47
64
const roundedProgressPercentage = Number ( progressPercentage . toFixed ( 2 ) ) ;
48
65
49
66
return (
0 commit comments