Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nextjs-starter-template
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Moorthy G
nextjs-starter-template
Commits
2ba023fc
Commit
2ba023fc
authored
Jan 25, 2024
by
Moorthy G
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(responsive-video): check reduced motion preferences, ignore abortError on the video element
parent
5737bb9c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
17 deletions
+44
-17
ResponsiveVideo.stories.tsx
...onents/shared/ResponsiveVideo/ResponsiveVideo.stories.tsx
+8
-5
ResponsiveVideo.tsx
components/shared/ResponsiveVideo/ResponsiveVideo.tsx
+36
-12
No files found.
components/shared/ResponsiveVideo/ResponsiveVideo.stories.tsx
View file @
2ba023fc
...
...
@@ -14,6 +14,12 @@ export default meta;
type
Story
=
StoryObj
<
typeof
meta
>
;
const
autoPlayFallback
=
(
<
h3
className=
"font-bold text-lg text-light bg-primary px-4 py-2"
>
Auto play failed. I am a fallback element
</
h3
>
);
export
const
ResponsiveVideo
:
Story
=
{
args
:
{
src
:
'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerFun.mp4'
,
...
...
@@ -23,6 +29,7 @@ export const ResponsiveVideo: Story = {
media
:
'(min-width: 640px)'
}
],
autoPlayFallback
,
autoPlay
:
true
,
controls
:
true
,
muted
:
true
...
...
@@ -33,10 +40,6 @@ export const AutoPlayFailed: Story = {
args
:
{
src
:
'broken.mp4'
,
autoPlay
:
true
,
autoPlayFallback
:
(
<
h3
className=
"font-bold text-lg text-light bg-primary px-4 py-2"
>
Auto play failed. I am a fallback element
</
h3
>
)
autoPlayFallback
}
};
components/shared/ResponsiveVideo/ResponsiveVideo.tsx
View file @
2ba023fc
...
...
@@ -33,28 +33,52 @@ const ResponsiveVideo = ({
const
videoRef
=
useRef
<
HTMLVideoElement
>
(
null
);
useEffect
(()
=>
{
async
function
play
()
{
try
{
autoPlay
&&
(
await
videoRef
?.
current
?.
play
());
}
catch
(
err
)
{
setIsAutoPlayFailed
(
true
);
}
}
play
();
},
[
autoPlay
,
currentSrc
]);
useEffect
(()
=>
{
const
handleResize
=
()
=>
{
/** find the set that matches current media query */
const
matchedSet
=
srcSet
?.
find
(
({
media
})
=>
window
.
matchMedia
(
breakpoints
[
media
]
||
media
).
matches
);
/* set the current src to the matched set or the default src */
setCurrentSrc
(
matchedSet
?.
src
||
src
);
};
window
.
addEventListener
(
'resize'
,
handleResize
);
/* call handleResize on mount */
handleResize
();
/* add resize event listener to window */
window
.
addEventListener
(
'resize'
,
handleResize
);
/* remove event listener on unmount */
return
()
=>
window
.
removeEventListener
(
'resize'
,
handleResize
);
},
[
src
,
srcSet
]);
useEffect
(()
=>
{
async
function
play
()
{
try
{
/** If user prefers reduced motion,
* throw an error and stop autoplay
*/
const
prefersReducedMotion
=
window
.
matchMedia
(
'(prefers-reduced-motion: reduce)'
);
if
(
prefersReducedMotion
.
matches
)
{
throw
new
Error
(
'User prefers reduced motion'
);
}
else
{
/* if autoplay is true, play the video */
autoPlay
&&
(
await
videoRef
?.
current
?.
play
());
}
}
catch
(
error
:
any
)
{
/* AbortError happens when source is changed in runtime,
if error is not AbortError, mark autoPlay as failed
*/
if
(
error
.
name
!==
'AbortError'
)
{
console
.
error
(
error
);
/* if play failed, report that autoplay has failed */
setIsAutoPlayFailed
(
true
);
}
}
}
play
();
},
[
autoPlay
,
currentSrc
]);
return
isAutoPlayFailed
&&
autoPlayFallback
?
(
autoPlayFallback
)
:
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment