-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support embedded youtube videos in blog posts (#803)
* feat: support embedded youtube videos in blog posts * chore: revert unintended changes
- Loading branch information
1 parent
6a62a07
commit 7999f3a
Showing
5 changed files
with
188 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import React, { useContext } from 'react'; | ||
import styled from 'styled-components'; | ||
import { theme } from '../../layout/theme'; | ||
import { Button } from '../../ui/buttons/button'; | ||
import { TextStyles } from '../../typography'; | ||
import YouTubeConsentContext from '../../../context/youtube-consent-context'; | ||
import { Link } from '../links/links'; | ||
|
||
interface YoutubeEmbedProps { | ||
videoId: string; | ||
} | ||
|
||
// how to make iframe responsive: https://stackoverflow.com/questions/17838607/making-an-iframe-responsive | ||
const StyledIframe = styled.iframe` | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 100%; | ||
`; | ||
|
||
const YoutubeEmbedWrapper = styled.div` | ||
position: relative; | ||
padding-bottom: 56.25%; /* 16:9 */ | ||
padding-top: 25px; | ||
height: 0; | ||
`; | ||
|
||
const YouTubePrivacyBannerWrapper = styled.div` | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 100%; | ||
padding: 16px; | ||
gap: 16px; | ||
background-color: ${theme.palette.background.card}; | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: center; | ||
align-items: center; | ||
`; | ||
|
||
const LegalText = styled.p` | ||
${TextStyles.label} | ||
text-align: center; | ||
`; | ||
|
||
const StyledButton = styled(Button)` | ||
width: fit-content; | ||
`; | ||
|
||
const SyledLink = styled(Link)` | ||
color: ${theme.palette.text.link.default}; | ||
&:hover { | ||
border-bottom: 1px solid ${theme.palette.text.link.default}; | ||
} | ||
`; | ||
|
||
export const YoutubeEmbed = ({ videoId }: YoutubeEmbedProps) => { | ||
const { consentGiven, giveConsent } = useContext(YouTubeConsentContext); | ||
|
||
return ( | ||
<YoutubeEmbedWrapper> | ||
{consentGiven ? ( | ||
<StyledIframe | ||
title="YouTube video player" | ||
width="560" | ||
height="315" | ||
src={`https://www.youtube-nocookie.com/embed/${videoId}`} | ||
frameBorder="0" | ||
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" | ||
allowFullScreen | ||
></StyledIframe> | ||
) : ( | ||
<YouTubePrivacyBanner giveConsent={giveConsent} /> | ||
)} | ||
</YoutubeEmbedWrapper> | ||
); | ||
}; | ||
|
||
const YouTubePrivacyBanner = ({ giveConsent }) => { | ||
return ( | ||
<YouTubePrivacyBannerWrapper> | ||
<LegalText> | ||
By clicking on "Show YouTube Content" you accept{' '} | ||
<SyledLink to={'https://policies.google.com/privacy?hl=en'}> | ||
YouTube's privacy policy | ||
</SyledLink> | ||
. | ||
</LegalText> | ||
<StyledButton onClick={giveConsent}>Show YouTube Content</StyledButton> | ||
</YouTubePrivacyBannerWrapper> | ||
); | ||
}; |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React, { createContext, useState, useEffect, ReactNode } from 'react'; | ||
|
||
interface YouTubeConsentContextType { | ||
consentGiven: boolean; | ||
giveConsent: () => void; | ||
} | ||
|
||
const YouTubeConsentContext = createContext<YouTubeConsentContextType>({ | ||
consentGiven: false, | ||
giveConsent: () => {}, | ||
}); | ||
|
||
interface YouTubeConsentProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
export const YouTubeConsentProvider = ({ | ||
children, | ||
}: YouTubeConsentProviderProps) => { | ||
const [consentGiven, setConsentGiven] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
const consent = localStorage.getItem('youtube-consent'); | ||
if (consent === 'true') { | ||
setConsentGiven(true); | ||
} | ||
}, []); | ||
|
||
const giveConsent = () => { | ||
setConsentGiven(true); | ||
localStorage.setItem('youtube-consent', 'true'); | ||
}; | ||
|
||
return ( | ||
<YouTubeConsentContext.Provider value={{ consentGiven, giveConsent }}> | ||
{children} | ||
</YouTubeConsentContext.Provider> | ||
); | ||
}; | ||
|
||
export default YouTubeConsentContext; |