Skip to content

Commit bd35469

Browse files
committed
Don't autoplay if user prefers reduced motion
1 parent 88db1a5 commit bd35469

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Props = {
2222
height?: number;
2323
hasAudio?: boolean;
2424
fallbackImage: JSX.Element;
25+
posterImage?: string;
2526
};
2627

2728
export const LoopVideo = ({
@@ -31,6 +32,7 @@ export const LoopVideo = ({
3132
height = 360,
3233
hasAudio = true,
3334
fallbackImage,
35+
posterImage,
3436
}: Props) => {
3537
const adapted = useShouldAdapt();
3638
const { renderingTarget } = useConfig();
@@ -39,6 +41,7 @@ export const LoopVideo = ({
3941
const [isPlaying, setIsPlaying] = useState(false);
4042
const [isMuted, setIsMuted] = useState(true);
4143
const [currentTime, setCurrentTime] = useState(0);
44+
const [prefersReducedMotion, setPrefersReducedMotion] = useState(false);
4245
/**
4346
* Keep a track of whether the video has been in view. We only want to
4447
* pause the video if it has been in view.
@@ -58,6 +61,13 @@ export const LoopVideo = ({
5861

5962
if (isInView) {
6063
if (!hasBeenInView) {
64+
if (
65+
window.matchMedia('(prefers-reduced-motion: reduce)')
66+
.matches
67+
) {
68+
setPrefersReducedMotion(true);
69+
return;
70+
}
6171
// When the video first comes into view, it should autoplay
6272
setIsPlaying(true);
6373
void vidRef.current.play();
@@ -166,6 +176,7 @@ export const LoopVideo = ({
166176
handleKeyDown={handleKeyDown}
167177
onError={onError}
168178
AudioIcon={AudioIcon}
179+
posterImage={prefersReducedMotion ? posterImage : undefined}
169180
/>
170181
</div>
171182
);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ export const Default = {
2323
videoId: 'test-video-1',
2424
height: 337.5,
2525
width: 600,
26+
posterImage:
27+
'https://media.guim.co.uk/9bdb802e6da5d3fd249b5060f367b3a817965f0c/0_0_1800_1080/master/1800.jpg',
2628
fallbackImage: (
2729
<CardPicture
28-
mainImage="https://i.guim.co.uk/img/media/13dd7e5c4ca32a53cd22dfd90ac1845ef5e5d643/91_0_1800_1080/master/1800.jpg?width=465&dpr=1&s=none&crop=5%3A4"
30+
mainImage="https://media.guim.co.uk/9bdb802e6da5d3fd249b5060f367b3a817965f0c/0_0_1800_1080/master/1800.jpg"
2931
imageSize="large"
3032
loading="eager"
3133
/>

dotcom-rendering/src/components/LoopVideoPlayer.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ type Props = {
5555
handleKeyDown: (event: React.KeyboardEvent<HTMLVideoElement>) => void;
5656
onError: (event: SyntheticEvent<HTMLVideoElement>) => void;
5757
AudioIcon: (iconProps: IconProps) => JSX.Element;
58+
/**
59+
* We show a poster image when the user has indicated that
60+
* they do not want videos to play automatically
61+
*/
62+
posterImage?: string;
5863
};
5964

6065
/**
@@ -70,6 +75,7 @@ export const LoopVideoPlayer = forwardRef(
7075
height,
7176
hasAudio,
7277
fallbackImage,
78+
posterImage,
7379
isPlayable,
7480
setIsPlayable,
7581
isPlaying,
@@ -93,12 +99,13 @@ export const LoopVideoPlayer = forwardRef(
9399
<video
94100
id={loopVideoId}
95101
ref={ref}
96-
preload="none"
102+
preload={posterImage ? 'metadata' : 'none'}
97103
loop={true}
98104
muted={isMuted}
99105
playsInline={true}
100106
height={height}
101107
width={width}
108+
poster={posterImage ?? undefined}
102109
onPlaying={() => {
103110
setIsPlaying(true);
104111
}}

0 commit comments

Comments
 (0)