Skip to content

Commit fdff18d

Browse files
committed
Use transitions for progress bar. Stop using <progress />.
1 parent f02b8f8 commit fdff18d

File tree

3 files changed

+40
-62
lines changed

3 files changed

+40
-62
lines changed

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

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import { useShouldAdapt } from '../lib/useShouldAdapt';
88
import { useConfig } from './ConfigContext';
99
import { LoopVideoPlayer } from './LoopVideoPlayer';
1010

11-
const videoContainerStyles = css`
11+
const videoContainerStyles = (height: number, width: number) => css`
1212
z-index: ${getZIndex('loop-video-container')};
1313
position: relative;
14+
height: ${height}px;
15+
width: ${width}px;
1416
`;
1517

1618
type Props = {
@@ -36,7 +38,7 @@ export const LoopVideo = ({
3638
const [isPlayable, setIsPlayable] = useState(false);
3739
const [isPlaying, setIsPlaying] = useState(false);
3840
const [isMuted, setIsMuted] = useState(true);
39-
const [elapsedTime, setElapsedTime] = useState(0);
41+
const [currentTime, setCurrentTime] = useState(0);
4042
/**
4143
* Keep a track of whether the video has been in view. We only want to
4244
* pause the video if it has been in view.
@@ -69,22 +71,9 @@ export const LoopVideo = ({
6971
}
7072
}, [isInView, hasBeenInView, isPlayable, isPlaying]);
7173

72-
/**
73-
* Progress bar updates
74-
*/
75-
useEffect(() => {
76-
const interval = setInterval(() => {
77-
setElapsedTime(vidRef.current?.currentTime ?? 0);
78-
}, 40);
79-
80-
return () => clearInterval(interval);
81-
}, []);
82-
8374
if (renderingTarget !== 'Web') return null;
8475

85-
if (adapted) {
86-
return fallbackImage;
87-
}
76+
if (adapted) return fallbackImage;
8877

8978
const handleClick = (event: React.SyntheticEvent) => {
9079
event.preventDefault();
@@ -115,7 +104,7 @@ export const LoopVideo = ({
115104
);
116105

117106
vidRef.current.currentTime = newTime;
118-
setElapsedTime(newTime);
107+
setCurrentTime(newTime);
119108
}
120109
};
121110

@@ -128,7 +117,7 @@ export const LoopVideo = ({
128117
vidRef.current.duration;
129118

130119
vidRef.current.currentTime = newTime;
131-
setElapsedTime(newTime);
120+
setCurrentTime(newTime);
132121
}
133122
};
134123

@@ -155,7 +144,7 @@ export const LoopVideo = ({
155144
<div
156145
className="loop-video-container"
157146
ref={setNode}
158-
css={videoContainerStyles}
147+
css={videoContainerStyles(height, width)}
159148
>
160149
<LoopVideoPlayer
161150
src={src}
@@ -164,6 +153,8 @@ export const LoopVideo = ({
164153
height={height}
165154
hasAudio={hasAudio}
166155
fallbackImage={fallbackImage}
156+
currentTime={currentTime}
157+
setCurrentTime={setCurrentTime}
167158
ref={vidRef}
168159
isPlayable={isPlayable}
169160
setIsPlayable={setIsPlayable}
@@ -174,7 +165,6 @@ export const LoopVideo = ({
174165
handleClick={handleClick}
175166
handleKeyDown={handleKeyDown}
176167
onError={onError}
177-
elapsedTime={elapsedTime}
178168
AudioIcon={AudioIcon}
179169
/>
180170
</div>

dotcom-rendering/src/components/LoopVideoPlayer.tsx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ type Props = {
4747
setIsPlayable: Dispatch<SetStateAction<boolean>>;
4848
isPlaying: boolean;
4949
setIsPlaying: Dispatch<SetStateAction<boolean>>;
50+
currentTime: number;
51+
setCurrentTime: Dispatch<SetStateAction<number>>;
5052
isMuted: boolean;
5153
setIsMuted: Dispatch<SetStateAction<boolean>>;
5254
handleClick: (event: SyntheticEvent) => void;
5355
handleKeyDown: (event: React.KeyboardEvent<HTMLVideoElement>) => void;
5456
onError: (event: SyntheticEvent<HTMLVideoElement>) => void;
55-
elapsedTime: number;
5657
AudioIcon: (iconProps: IconProps) => JSX.Element;
5758
};
5859

@@ -73,12 +74,13 @@ export const LoopVideoPlayer = forwardRef(
7374
setIsPlayable,
7475
isPlaying,
7576
setIsPlaying,
77+
currentTime,
78+
setCurrentTime,
7679
isMuted,
7780
setIsMuted,
7881
handleClick,
7982
handleKeyDown,
8083
onError,
81-
elapsedTime,
8284
AudioIcon,
8385
}: Props,
8486
ref: React.ForwardedRef<HTMLVideoElement>,
@@ -101,6 +103,16 @@ export const LoopVideoPlayer = forwardRef(
101103
onCanPlay={() => {
102104
setIsPlayable(true);
103105
}}
106+
onTimeUpdate={() => {
107+
if (
108+
ref &&
109+
'current' in ref &&
110+
ref.current &&
111+
isPlaying
112+
) {
113+
setCurrentTime(ref.current.currentTime);
114+
}
115+
}}
104116
onClick={handleClick}
105117
onKeyDown={handleKeyDown}
106118
role="button"
@@ -117,12 +129,16 @@ export const LoopVideoPlayer = forwardRef(
117129
{ref && 'current' in ref && ref.current && (
118130
<>
119131
{isPlayable && !isPlaying && (
120-
<div css={playIconStyles}>
132+
<button
133+
type="button"
134+
onClick={handleClick}
135+
css={playIconStyles}
136+
>
121137
<PlayIcon iconWidth="narrow" />
122-
</div>
138+
</button>
123139
)}
124140
<LoopVideoProgressBar
125-
currentTime={elapsedTime}
141+
currentTime={currentTime}
126142
duration={ref.current.duration}
127143
/>
128144
{hasAudio && (
Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,15 @@
11
import { css } from '@emotion/react';
22
import { palette } from '../palette';
33

4-
const styles = css`
4+
const styles = (progressPercentage: number) => css`
55
position: absolute;
66
bottom: 0;
77
left: 0;
88
height: 7px;
99
width: 100%;
10-
11-
progress {
12-
display: block;
13-
width: 100%;
14-
height: 100%;
15-
border: none;
16-
-moz-border-radius: 0;
17-
-webkit-border-radius: 0;
18-
border-radius: 0;
19-
}
20-
21-
/* background: */
22-
progress::-webkit-progress-bar {
23-
background-color: transparent;
24-
}
25-
progress {
26-
background-color: transparent;
27-
}
28-
29-
/* value: */
30-
progress::-webkit-progress-value {
31-
background-color: ${palette('--loop-video-progress-bar-value')};
32-
}
33-
progress::-moz-progress-bar {
34-
background-color: ${palette('--loop-video-progress-bar-value')};
35-
}
36-
progress {
37-
color: ${palette('--loop-video-progress-bar-value')};
38-
}
10+
width: ${progressPercentage}%;
11+
transition: width 0.3s linear;
12+
background-color: ${palette('--loop-video-progress-bar-value')};
3913
`;
4014

4115
type Props = {
@@ -46,12 +20,10 @@ type Props = {
4620
export const LoopVideoProgressBar = ({ currentTime, duration }: Props) => {
4721
if (duration <= 0) return null;
4822

49-
const progressPercentage =
50-
duration > 0 ? (currentTime * 100) / duration : 0;
23+
const progressPercentage = (currentTime * 100) / duration;
24+
if (Number.isNaN(progressPercentage)) {
25+
return null;
26+
}
5127

52-
return (
53-
<div css={styles} className="progress-bar">
54-
<progress value={progressPercentage / 100} />
55-
</div>
56-
);
28+
return <div role="progressbar" css={styles(progressPercentage)} />;
5729
};

0 commit comments

Comments
 (0)