Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.

Commit c45d28c

Browse files
committed
Adding embed blocks
1 parent cb39881 commit c45d28c

File tree

10 files changed

+155
-62
lines changed

10 files changed

+155
-62
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import VideoEmbed from '@/components/molecules/VideoEmbed'
2+
import PropTypes from 'prop-types'
3+
import React, {useState, useEffect} from 'react'
4+
5+
/**
6+
* Embed Block
7+
*
8+
* The core Embed block from Gutenberg.
9+
*
10+
* @author WebDevStudios
11+
* @param {string} className Optional classnames.
12+
* @param {string} align Optional alignment style.
13+
* @param {string} anchor Optional anchor/id.
14+
* @param {string} content The content of the block.
15+
* @param {string} level The heading level.
16+
* @return {Element} The component to embed.
17+
*/
18+
export default function BlockEmbed({
19+
className,
20+
url,
21+
caption,
22+
providerNameSlug
23+
}) {
24+
if (providerNameSlug === 'twitter') {
25+
fetch(
26+
'https://publish.twitter.com/oembed?url=https://twitter.com/Interior/status/463440424141459456'
27+
)
28+
.then((response) => response.json())
29+
.then((data) => console.log(data))
30+
}
31+
32+
return (
33+
<>
34+
{!!url && (
35+
<>
36+
{providerNameSlug === 'twitter' ? (
37+
<div></div>
38+
) : (
39+
<VideoEmbed className={className} url={url} caption={caption} />
40+
)}
41+
</>
42+
)}
43+
</>
44+
)
45+
}
46+
47+
BlockEmbed.propTypes = {
48+
className: PropTypes.string,
49+
url: PropTypes.string,
50+
align: PropTypes.string,
51+
caption: PropTypes.string,
52+
providerNameSlug: PropTypes.string
53+
}
54+
BlockEmbed.defaultProps = {
55+
align: 'left'
56+
}

components/blocks/BlockEmbed/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {default} from './BlockEmbed'

components/blocks/BlockVideoEmbed/BlockVideoEmbed.js

Lines changed: 0 additions & 59 deletions
This file was deleted.

components/blocks/BlockVideoEmbed/index.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

components/blocks/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ export {default as BlockButton} from './BlockButton'
1919
export {default as BlockButtons} from './BlockButtons'
2020
export {default as BlockColumns} from './BlockColumns'
2121
export {default as BlockCover} from './BlockCover'
22+
export {default as BlockEmbed} from './BlockEmbed'
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import cn from 'classnames'
2+
import PropTypes from 'prop-types'
3+
import React from 'react'
4+
import styles from './VideoEmbed.module.css'
5+
6+
export default function VideoEmbed({className, title, url}) {
7+
/**
8+
* Create URL embed for YouTube or Vimeo videos.
9+
*
10+
* @param {string} url The video URL.
11+
*/
12+
function createVideoUrl(url) {
13+
if (!url) {
14+
return false
15+
}
16+
17+
let videoUrl, videoId
18+
19+
videoId = url.indexOf('v=') !== -1 ? url.split('v=') : url.split('/')
20+
videoId = videoId[videoId.length - 1]
21+
22+
videoUrl = url.includes('vimeo')
23+
? `//player.vimeo.com/video/${videoId}`
24+
: `//youtube.com/embed/${videoId}`
25+
26+
return videoUrl
27+
}
28+
29+
/* eslint-disable jsx-a11y/iframe-has-title */
30+
return (
31+
<div className={cn(styles.videoEmbed, className)}>
32+
<iframe
33+
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
34+
allowFullScreen
35+
frameBorder="0"
36+
height="315"
37+
loading="lazy"
38+
src={createVideoUrl(url)}
39+
width="560"
40+
title={title}
41+
className={className}
42+
></iframe>
43+
</div>
44+
)
45+
/* eslint-enable jsx-a11y/iframe-has-title */
46+
}
47+
48+
VideoEmbed.propTypes = {
49+
className: PropTypes.string,
50+
title: PropTypes.string,
51+
url: PropTypes.string.isRequired
52+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.videoEmbed {
2+
@apply w-full relative overflow-hidden mb-24;
3+
4+
padding-top: 56.25%;
5+
6+
& iframe {
7+
@apply absolute w-full h-full inset-0;
8+
}
9+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {Meta, Story, Canvas} from '@storybook/addon-docs/blocks'
2+
3+
import VideoEmbed from '.'
4+
5+
<Meta title="Design System/Organisms/VideoEmbed" component={VideoEmbed} />
6+
7+
# Video Embed
8+
9+
Use this to display a video in iFrame, pass the YouTube video ID to the `VideoEmbed` component.
10+
11+
<Canvas>
12+
<Story name="Component">
13+
<VideoEmbed url="https://www.youtube.com/watch?v=ebQbQufIb0E" />
14+
</Story>
15+
</Canvas>
16+
17+
## Controls
18+
19+
Play around with `<VideoEmbed />` props in the [Canvas tab of the Controls story](?path=/story/design-system-organisims-videoembed--controls).
20+
21+
export const Template = (args) => <VideoEmbed {...args} />
22+
23+
<Canvas>
24+
<Story
25+
name="Controls"
26+
args={{
27+
url: 'https://www.youtube.com/watch?v=ebQbQufIb0E'
28+
}}
29+
>
30+
{Template.bind({})}
31+
</Story>
32+
</Canvas>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {default} from './VideoEmbed'

functions/displayBlock.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ export default function displayBlock(block, index) {
2525
case 'core/code':
2626
case 'core/preformatted':
2727
return <Blocks.BlockCode {...attributes} key={index} />
28-
// case 'core/embed':
29-
// return <Blocks.BlockVideoEmbed {...attributes} key={index} />
28+
case 'core/embed':
29+
console.log(attributes)
30+
return <Blocks.BlockEmbed {...attributes} key={index} />
3031
case 'core/media-text':
3132
return (
3233
<Blocks.BlockMediaText

0 commit comments

Comments
 (0)