-
Notifications
You must be signed in to change notification settings - Fork 30
Resume looping video when re-enters viewport #14133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+130
−72
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9e39bb0
Ensure thumbnail image is full size of video
domlander 32a3c0b
Resume video when it comes back into view
domlander fb6e4a5
Refactor useIsInView to know whether the element is out of view
domlander 9081824
Refactor to use player state instead of multiple state variables.
domlander 46a1934
Mute video when out of view
domlander 3b3ed9b
fix linting following useIsInView refactor
domlander File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,10 +15,12 @@ const videoStyles = (width: number, height: number) => css` | |
cursor: pointer; | ||
/* Prevents CLS by letting the browser know the space the video will take up. */ | ||
aspect-ratio: ${width} / ${height}; | ||
object-fit: cover; | ||
`; | ||
|
||
const playIconStyles = css` | ||
position: absolute; | ||
/* Center the icon */ | ||
top: calc(50% - ${narrowPlayIconWidth / 2}px); | ||
left: calc(50% - ${narrowPlayIconWidth / 2}px); | ||
cursor: pointer; | ||
|
@@ -49,17 +51,23 @@ const audioIconContainerStyles = css` | |
border: 1px solid ${palette('--loop-video-audio-icon-border')}; | ||
`; | ||
|
||
export const PLAYER_STATES = [ | ||
'NOT_STARTED', | ||
'PLAYING', | ||
'PAUSED_BY_USER', | ||
'PAUSED_BY_INTERSECTION_OBSERVER', | ||
] as const; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really like this type, it makes the intent of the loop video very easy to follow. |
||
type Props = { | ||
src: string; | ||
videoId: string; | ||
width: number; | ||
height: number; | ||
hasAudio: boolean; | ||
fallbackImageComponent: JSX.Element; | ||
isPlayable: boolean; | ||
setIsPlayable: Dispatch<SetStateAction<boolean>>; | ||
isPlaying: boolean; | ||
setIsPlaying: Dispatch<SetStateAction<boolean>>; | ||
playerState: (typeof PLAYER_STATES)[number]; | ||
setPlayerState: Dispatch<SetStateAction<(typeof PLAYER_STATES)[number]>>; | ||
currentTime: number; | ||
setCurrentTime: Dispatch<SetStateAction<number>>; | ||
isMuted: boolean; | ||
|
@@ -68,12 +76,9 @@ type Props = { | |
handleKeyDown: (event: React.KeyboardEvent<HTMLVideoElement>) => void; | ||
onError: (event: SyntheticEvent<HTMLVideoElement>) => void; | ||
AudioIcon: (iconProps: IconProps) => JSX.Element; | ||
/** | ||
* We ONLY show a thumbnail image when the user has indicated that they do | ||
* not want videos to play automatically, e.g. prefers reduced motion. Otherwise, | ||
* we do not bother downloading the image, as the video will be autoplayed. | ||
*/ | ||
thumbnailImage?: string; | ||
posterImage?: string; | ||
shouldPreload: boolean; | ||
showPlayIcon: boolean; | ||
}; | ||
|
||
/** | ||
|
@@ -87,13 +92,12 @@ export const LoopVideoPlayer = forwardRef( | |
videoId, | ||
width, | ||
height, | ||
hasAudio, | ||
fallbackImageComponent, | ||
thumbnailImage, | ||
posterImage, | ||
isPlayable, | ||
setIsPlayable, | ||
isPlaying, | ||
setIsPlaying, | ||
playerState, | ||
setPlayerState, | ||
currentTime, | ||
setCurrentTime, | ||
isMuted, | ||
|
@@ -102,6 +106,8 @@ export const LoopVideoPlayer = forwardRef( | |
handleKeyDown, | ||
onError, | ||
AudioIcon, | ||
shouldPreload, | ||
showPlayIcon, | ||
}: Props, | ||
ref: React.ForwardedRef<HTMLVideoElement>, | ||
) => { | ||
|
@@ -114,15 +120,15 @@ export const LoopVideoPlayer = forwardRef( | |
<video | ||
id={loopVideoId} | ||
ref={ref} | ||
preload={thumbnailImage ? 'metadata' : 'none'} | ||
preload={shouldPreload ? 'metadata' : 'none'} | ||
loop={true} | ||
muted={isMuted} | ||
playsInline={true} | ||
height={height} | ||
width={width} | ||
poster={thumbnailImage ?? undefined} | ||
poster={posterImage} | ||
onPlaying={() => { | ||
setIsPlaying(true); | ||
setPlayerState('PLAYING'); | ||
}} | ||
onCanPlay={() => { | ||
setIsPlayable(true); | ||
|
@@ -132,7 +138,7 @@ export const LoopVideoPlayer = forwardRef( | |
ref && | ||
'current' in ref && | ||
ref.current && | ||
isPlaying | ||
playerState === 'PLAYING' | ||
) { | ||
setCurrentTime(ref.current.currentTime); | ||
} | ||
|
@@ -144,15 +150,14 @@ export const LoopVideoPlayer = forwardRef( | |
onError={onError} | ||
css={videoStyles(width, height)} | ||
> | ||
{/* Ensure webm source is provided. Encoding the video to a webm file will improve | ||
performance on supported browsers. https://web.dev/articles/video-and-source-tags */} | ||
{/* <source src={webmSrc} type="video/webm"> */} | ||
{/* Only mp4 is currently supported. Assumes the video file type is mp4. */} | ||
<source src={src} type="video/mp4" /> | ||
{fallbackImageComponent} | ||
</video> | ||
{ref && 'current' in ref && ref.current && isPlayable && ( | ||
<> | ||
{!isPlaying && ( | ||
{/* Play icon */} | ||
{showPlayIcon && ( | ||
<button | ||
type="button" | ||
onClick={handleClick} | ||
|
@@ -161,32 +166,32 @@ export const LoopVideoPlayer = forwardRef( | |
<PlayIcon iconWidth="narrow" /> | ||
</button> | ||
)} | ||
{/* Progress bar */} | ||
<LoopVideoProgressBar | ||
videoId={loopVideoId} | ||
currentTime={currentTime} | ||
duration={ref.current.duration} | ||
/> | ||
{hasAudio && ( | ||
<button | ||
type="button" | ||
onClick={(event) => { | ||
event.stopPropagation(); // Don't pause the video | ||
setIsMuted(!isMuted); | ||
}} | ||
css={audioButtonStyles} | ||
> | ||
<div css={audioIconContainerStyles}> | ||
<AudioIcon | ||
size="xsmall" | ||
theme={{ | ||
fill: palette( | ||
'--loop-video-audio-icon', | ||
), | ||
}} | ||
/> | ||
</div> | ||
</button> | ||
)} | ||
{/* Audio icon */} | ||
<button | ||
type="button" | ||
onClick={(event) => { | ||
event.stopPropagation(); // Don't pause the video | ||
setIsMuted(!isMuted); | ||
}} | ||
css={audioButtonStyles} | ||
> | ||
<div css={audioIconContainerStyles}> | ||
<AudioIcon | ||
size="xsmall" | ||
theme={{ | ||
fill: palette( | ||
'--loop-video-audio-icon', | ||
), | ||
}} | ||
/> | ||
</div> | ||
</button> | ||
</> | ||
)} | ||
</> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need to grab the image if the video is not in view?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this situation, where the second card has a blank space in the place of the video:

A little confusing, as
isInView
means is at least 50% in view. I wonder if there's a better name for this variable