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

Commit edae158

Browse files
author
Greg Rickaby
authored
Merge pull request #163 from WebDevStudios/feature/component-handling
Feature/component handling
2 parents ae6062e + 348a8eb commit edae158

File tree

21 files changed

+499
-189
lines changed

21 files changed

+499
-189
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import RichText from '@/components/atoms/RichText'
2+
import cn from 'classnames'
3+
import PropTypes from 'prop-types'
4+
import {TwitterTweetEmbed} from 'react-twitter-embed'
5+
import styles from './TwitterEmbed.module.css'
6+
7+
/**
8+
* TwitterEmbed Block
9+
*
10+
* @author WebDevStudios
11+
* @param {object} props TwitterEmbed component props.
12+
* @param {string} props.className Optional className.
13+
* @param {string} props.caption Optional caption.
14+
* @param {string} props.url The full URL to the video.
15+
* @return {Element} The TwitterEmbed component.
16+
*/
17+
export default function TwitterEmbed({className, caption, url}) {
18+
const tweetURL = url ? url.split('/') : '' // Split URL string into array.
19+
const tweetId = tweetURL ? tweetURL[tweetURL.length - 1] : '' // Get ID from url array.
20+
21+
return (
22+
<>
23+
{!!tweetId && (
24+
<div className={cn(styles.twitterEmbed, className)}>
25+
<TwitterTweetEmbed tweetId={tweetId} />
26+
{!!caption && (
27+
<div className={styles.caption}>
28+
<RichText tag="span">{caption}</RichText>
29+
</div>
30+
)}
31+
</div>
32+
)}
33+
</>
34+
)
35+
}
36+
37+
TwitterEmbed.propTypes = {
38+
className: PropTypes.string,
39+
caption: PropTypes.string,
40+
url: PropTypes.string.isRequired
41+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.twitterEmbed {
2+
@apply block;
3+
4+
& .caption {
5+
@apply text-center text-xs pt-12;
6+
}
7+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {Meta, Story, Canvas} from '@storybook/addon-docs/blocks'
2+
3+
import TwitterEmbed from '.'
4+
5+
<Meta title="Components/Atoms/TwitterEmbed" component={TwitterEmbed} />
6+
7+
# Twitter Embed
8+
9+
Use this to display a tweet from a timeline
10+
11+
<Canvas>
12+
<Story name="Component">
13+
<TwitterEmbed
14+
url="https://twitter.com/webdevstudios/status/1351167649418514432"
15+
caption="This is an optional caption"
16+
/>
17+
</Story>
18+
</Canvas>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {default} from './TwitterEmbed'
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import cn from 'classnames'
2+
import PropTypes from 'prop-types'
3+
import React from 'react'
4+
import RichText from '@/components/atoms/RichText'
5+
import styles from './VideoEmbed.module.css'
6+
7+
/**
8+
* VideoEmbed Block
9+
*
10+
* @author WebDevStudios
11+
* @param {object} props VideoEmbed component props.
12+
* @param {string} props.className Optional className.
13+
* @param {string} props.url The full URL to the video.
14+
* @param {string} props.type The type of video (youtube, vimeo).
15+
* @param {string} props.caption Optional caption.
16+
* @return {Element} The VideoEmbed component.
17+
*/
18+
export default function VideoEmbed({className, url, type, caption}) {
19+
/**
20+
* Create URL embed for YouTube or Vimeo videos.
21+
*
22+
* @param {string} url The video URL.
23+
*/
24+
function createVideoUrl(url) {
25+
if (!url) {
26+
return false
27+
}
28+
29+
let videoUrl, videoId
30+
31+
videoId = url.indexOf('v=') !== -1 ? url.split('v=') : url.split('/')
32+
videoId = videoId[videoId.length - 1]
33+
34+
videoUrl = url.includes('vimeo')
35+
? `//player.vimeo.com/video/${videoId}`
36+
: `//youtube.com/embed/${videoId}`
37+
38+
return videoUrl
39+
}
40+
41+
return (
42+
<div className={cn(styles.videoEmbed, className)}>
43+
<div className={styles.wrapper}>
44+
<iframe
45+
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
46+
allowFullScreen
47+
frameBorder="0"
48+
height="315"
49+
loading="lazy"
50+
src={createVideoUrl(url)}
51+
width="560"
52+
title={`Embedded content from ${type}`}
53+
className={className}
54+
></iframe>
55+
</div>
56+
{!!caption && (
57+
<div className={styles.caption}>
58+
<RichText tag="span">{caption}</RichText>
59+
</div>
60+
)}
61+
</div>
62+
)
63+
}
64+
65+
VideoEmbed.propTypes = {
66+
className: PropTypes.string,
67+
caption: PropTypes.string,
68+
type: PropTypes.string,
69+
url: PropTypes.string.isRequired
70+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.videoEmbed {
2+
@apply mb-24;
3+
4+
& .wrapper {
5+
@apply w-full relative overflow-hidden;
6+
7+
padding-top: 56.25%;
8+
}
9+
10+
& iframe {
11+
@apply absolute w-full h-full inset-0;
12+
}
13+
14+
& .caption {
15+
@apply text-center text-xs pt-12;
16+
}
17+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {Meta, Story, Canvas} from '@storybook/addon-docs/blocks'
2+
3+
import VideoEmbed from '.'
4+
5+
<Meta title="Components/Atoms/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
14+
url="https://www.youtube.com/watch?v=ebQbQufIb0E"
15+
caption="This is an optional caption"
16+
/>
17+
</Story>
18+
</Canvas>
19+
20+
## Controls
21+
22+
Play around with `<VideoEmbed />` props in the [Canvas tab of the Controls story](?path=/story/design-system-organisims-videoembed--controls).
23+
24+
export const Template = (args) => <VideoEmbed {...args} />
25+
26+
<Canvas>
27+
<Story
28+
name="Controls"
29+
args={{
30+
url: 'https://www.youtube.com/watch?v=ebQbQufIb0E'
31+
}}
32+
>
33+
{Template.bind({})}
34+
</Story>
35+
</Canvas>

components/atoms/VideoEmbed/index.js

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

components/blocks/Gutenberg/BlockAccordions/BlockAccordions.js

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

components/blocks/Gutenberg/BlockAccordions/index.js

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

0 commit comments

Comments
 (0)