-
Notifications
You must be signed in to change notification settings - Fork 5
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
Allow for Vimeo vids in VideoEmbed
#11412
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,8 @@ import PrismicHtmlBlock from '@weco/common/views/components/PrismicHtmlBlock/Pri | |
|
||
export type Props = { | ||
embedUrl: string; | ||
videoProvider?: string; | ||
videoThumbnail?: string; | ||
caption?: prismic.RichTextField; | ||
transcript?: prismic.RichTextField; | ||
hasFullSizePoster?: boolean; | ||
|
@@ -54,16 +56,23 @@ const VideoTrigger = styled.button<{ $hasFullSizePoster?: boolean }>` | |
|
||
const VideoEmbed: FunctionComponent<Props> = ({ | ||
embedUrl, | ||
videoProvider, | ||
videoThumbnail, | ||
caption, | ||
transcript, | ||
hasFullSizePoster, | ||
}: Props) => { | ||
const [isActive, setIsActive] = useState(false); | ||
const id = embedUrl.match(/embed\/(.*)\?/)?.[1]; | ||
const hasAnalyticsConsent = getConsentState('analytics'); | ||
const isYouTube = embedUrl.indexOf('youtube') >= 0; | ||
const isYouTube = videoProvider === 'YouTube'; | ||
const isVimeo = videoProvider === 'Vimeo'; | ||
|
||
useEffect(() => { | ||
// TODO do we need analytics consent for vimeo? | ||
// Do we need this? | ||
/* <script src="https://player.vimeo.com/api/player.js"></script> */ | ||
|
||
if (isYouTube && hasAnalyticsConsent) { | ||
// GA4 automatically tracks YouTube engagement, but requires the iframe api | ||
// script to have been loaded on the page. Since we're lazyloading youtube | ||
|
@@ -86,17 +95,31 @@ const VideoEmbed: FunctionComponent<Props> = ({ | |
} | ||
}, []); | ||
|
||
const videoSrc = isYouTube | ||
? `${embedUrl}&enablejsapi=1&autoplay=1` | ||
: isVimeo | ||
? `${embedUrl}&autoplay=1` | ||
: undefined; | ||
|
||
const thumbnailSrc = isYouTube | ||
? `https://img.youtube.com/vi/${id}/${ | ||
hasFullSizePoster ? 'maxresdefault' : 'hqdefault' | ||
}.jpg` | ||
: isVimeo | ||
? videoThumbnail | ||
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. Did you see this? 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 did! But I wasn't sure we wanted to use some third party for this..? There's also this that could be worth a look https://github.com/vimeo/vimeo-oembed-examples .. |
||
: undefined; | ||
|
||
return ( | ||
<> | ||
<VideoEmbedWrapper data-chromatic="ignore"> | ||
<IframeContainer> | ||
{isActive ? ( | ||
{isActive && videoSrc ? ( | ||
<iframe | ||
className="iframe" | ||
title="Video" | ||
allowFullScreen={true} | ||
allow="autoplay; picture-in-picture" | ||
src={`${embedUrl}&enablejsapi=1&autoplay=1`} | ||
src={videoSrc} | ||
style={{ border: 0 }} | ||
/> | ||
) : ( | ||
|
@@ -105,13 +128,8 @@ const VideoEmbed: FunctionComponent<Props> = ({ | |
$hasFullSizePoster={hasFullSizePoster} | ||
> | ||
<span className="visually-hidden">Play video</span> | ||
<YouTubePlay /> | ||
<img | ||
src={`https://img.youtube.com/vi/${id}/${ | ||
hasFullSizePoster ? 'maxresdefault' : 'hqdefault' | ||
}.jpg`} | ||
alt="" | ||
/> | ||
{isYouTube && <YouTubePlay />} | ||
{thumbnailSrc && <img src={thumbnailSrc} alt="" />} | ||
</VideoTrigger> | ||
)} | ||
</IframeContainer> | ||
|
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.
Can you constrain this type further?
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.
Done!