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
34e3dba8
Commit
34e3dba8
authored
Jan 25, 2024
by
Moorthy G
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(responsive-video): add ResponsiveVideo component
parent
4e07b742
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
121 additions
and
0 deletions
+121
-0
ResponsiveVideo.stories.tsx
...onents/shared/ResponsiveVideo/ResponsiveVideo.stories.tsx
+42
-0
ResponsiveVideo.tsx
components/shared/ResponsiveVideo/ResponsiveVideo.tsx
+65
-0
breakpoints.ts
components/shared/ResponsiveVideo/breakpoints.ts
+13
-0
index.ts
components/shared/ResponsiveVideo/index.ts
+1
-0
No files found.
components/shared/ResponsiveVideo/ResponsiveVideo.stories.tsx
0 → 100644
View file @
34e3dba8
import
type
{
Meta
,
StoryObj
}
from
'@storybook/react'
;
import
ResponsiveVideoComponent
from
'./ResponsiveVideo'
;
const
meta
:
Meta
<
typeof
ResponsiveVideoComponent
>
=
{
title
:
'Shared/ResponsiveVideo'
,
component
:
ResponsiveVideoComponent
,
parameters
:
{
layout
:
'centered'
},
tags
:
[
'autodocs'
]
};
export
default
meta
;
type
Story
=
StoryObj
<
typeof
meta
>
;
export
const
ResponsiveVideo
:
Story
=
{
args
:
{
src
:
'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerFun.mp4'
,
srcSet
:
[
{
src
:
'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4'
,
media
:
'(min-width: 640px)'
}
],
autoPlay
:
true
,
controls
:
true
,
muted
:
true
}
};
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
>
)
}
};
components/shared/ResponsiveVideo/ResponsiveVideo.tsx
0 → 100644
View file @
34e3dba8
'use client'
;
import
React
,
{
useState
,
useEffect
,
useRef
}
from
'react'
;
import
breakpoints
from
'./breakpoints'
;
export
interface
ResponsiveVideoProps
extends
React
.
VideoHTMLAttributes
<
HTMLVideoElement
>
{
src
:
string
;
/** first match in the array will be selected
* media should be a valid media query or
* a key from breakpoints object
*/
srcSet
?:
{
src
:
string
;
media
:
string
;
}[];
/** auto plays video source */
autoPlay
?:
boolean
;
/** If autoplay fails and if there is a fallback,
* display this fallback element */
autoPlayFallback
?:
React
.
ReactNode
;
}
const
ResponsiveVideo
=
({
src
,
srcSet
,
autoPlay
,
autoPlayFallback
,
...
rest
}:
ResponsiveVideoProps
)
=>
{
const
[
isAutoPlayFailed
,
setIsAutoPlayFailed
]
=
useState
(
false
);
const
[
currentSrc
,
setCurrentSrc
]
=
useState
(
src
);
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
=
()
=>
{
const
matchedSet
=
srcSet
?.
find
(
({
media
})
=>
window
.
matchMedia
(
breakpoints
[
media
]
||
media
).
matches
);
setCurrentSrc
(
matchedSet
?.
src
||
src
);
};
window
.
addEventListener
(
'resize'
,
handleResize
);
handleResize
();
return
()
=>
window
.
removeEventListener
(
'resize'
,
handleResize
);
},
[
src
,
srcSet
]);
return
isAutoPlayFailed
&&
autoPlayFallback
?
(
autoPlayFallback
)
:
(
<
video
ref=
{
videoRef
}
src=
{
currentSrc
}
{
...
rest
}
/>
);
};
export
default
ResponsiveVideo
;
components/shared/ResponsiveVideo/breakpoints.ts
0 → 100644
View file @
34e3dba8
type
Breakpoints
=
{
[
key
:
string
]:
string
;
};
const
breakpoints
:
Breakpoints
=
{
sm
:
'(min-width: 640px)'
,
md
:
'(min-width: 768px)'
,
lg
:
'(min-width: 1024px)'
,
xl
:
'(min-width: 1280px)'
,
xxl
:
'(min-width: 1536px)'
};
export
default
breakpoints
;
components/shared/ResponsiveVideo/index.ts
0 → 100644
View file @
34e3dba8
export
*
from
'./ResponsiveVideo'
;
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