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

Commit 3071021

Browse files
author
Greg Rickaby
authored
Merge pull request #162 from WebDevStudios/feature/lazy-blocks
Feature/lazy blocks
2 parents cb39881 + 5c0df5c commit 3071021

File tree

7 files changed

+119
-143
lines changed

7 files changed

+119
-143
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Hero from '@/components/organisms/Hero'
2+
import PropTypes from 'prop-types'
3+
4+
/**
5+
* Handle the Hero block.
6+
*
7+
* @author WebDevStudios
8+
* @param {object} props The props.
9+
* @param {object} props.attributes The attributes object.
10+
* @return {Element} The component.
11+
*/
12+
export default function BlockHero({attributes}) {
13+
attributes = {
14+
...attributes,
15+
backgroundImage: JSON.parse(decodeURIComponent(attributes.backgroundImage))
16+
}
17+
18+
return (
19+
<>
20+
{attributes ? (
21+
<Hero {...attributes} />
22+
) : (
23+
'There was a problem with attributes in BlockHero.js.'
24+
)}
25+
</>
26+
)
27+
}
28+
29+
BlockHero.propTypes = {
30+
attributes: PropTypes.object
31+
}

components/blocks/BlockHero/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {default} from './BlockHero'
Lines changed: 15 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,31 @@
1-
import Image from '@/components/atoms/Image'
2-
import Blocks from '@/components/molecules/Blocks'
31
import MediaText from '@/components/organisms/MediaText'
42
import PropTypes from 'prop-types'
53

6-
// TODO: Swap `img` in `MediaText` component with `Image` atom component.
7-
84
/**
9-
* Code Block
10-
*
11-
* The core Code block from Gutenberg.
5+
* Handle the MediaText block.
126
*
137
* @author WebDevStudios
14-
* @param {object} media media props.
15-
* @param {string} media.anchor The anchor/id of the block.
16-
* @param {string} media.mediaAlt The image alt text.
17-
* @param {string} media.caption The image caption.
18-
* @param {string} media.className The image class.
19-
* @param {string} media.mediaId The image ID.
20-
* @param {string} media.href The URL of the link.
21-
* @param {string} media.linkTarget Target for the link.
22-
* @param {string} media.linkClass Class for the link.
23-
* @param {string} media.rel The rel attribute for the link.
24-
* @param {string} media.sizeSlug The WP image size.
25-
* @param {string} media.mediaUrl The full URL path of the image.
26-
* @param {string} media.mediaPosition The position of the image, left or right.
27-
* @param {Array} innerBlocks The array of inner blocks to display.
28-
* @return {Element} The Code component.
8+
* @param {object} props The props.
9+
* @param {object} props.attributes The attributes object.
10+
* @return {Element} The component.
2911
*/
30-
export default function BlockMediaText({media, innerBlocks}) {
12+
export default function BlockMediaText({attributes}) {
13+
attributes = {
14+
...attributes,
15+
image: JSON.parse(decodeURIComponent(attributes.image))
16+
}
17+
3118
return (
3219
<>
33-
{!!media && innerBlocks?.length && (
34-
<MediaText
35-
mediaLeft={media?.mediaPosition === 'left' ? true : false}
36-
image={{url: media?.mediaUrl, alt: media?.mediaAlt}}
37-
>
38-
<Blocks blocks={innerBlocks} />
39-
</MediaText>
20+
{attributes ? (
21+
<MediaText {...attributes} />
22+
) : (
23+
'There was a problem with attributes in BlockMediaText.js.'
4024
)}
4125
</>
4226
)
4327
}
4428

4529
BlockMediaText.propTypes = {
46-
media: PropTypes.shape({
47-
anchor: PropTypes.string,
48-
caption: PropTypes.string,
49-
className: PropTypes.string,
50-
href: PropTypes.string,
51-
linkTarget: PropTypes.string,
52-
linkClass: PropTypes.string,
53-
rel: PropTypes.string,
54-
sizeSlug: PropTypes.string,
55-
mediaAlt: PropTypes.string,
56-
mediaId: PropTypes.number,
57-
mediaType: PropTypes.string,
58-
mediaUrl: PropTypes.string,
59-
mediaPosition: PropTypes.string
60-
}),
61-
innerBlocks: PropTypes.arrayOf(
62-
PropTypes.shape({
63-
name: PropTypes.string,
64-
attributes: PropTypes.object
65-
})
66-
)
67-
}
68-
BlockMediaText.defaultProps = {
69-
media: PropTypes.shape({
70-
mediaPosition: 'left'
71-
})
30+
attributes: PropTypes.object
7231
}

components/blocks/index.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
// Create default exports for each Block.
22
export {default as BlockAccordions} from './BlockAccordions'
3-
export {default as BlockQuote} from './BlockQuote'
4-
export {default as BlockPullQuote} from './BlockPullQuote'
3+
export {default as BlockButton} from './BlockButton'
4+
export {default as BlockButtons} from './BlockButtons'
5+
export {default as BlockCode} from './BlockCode'
6+
export {default as BlockColumns} from './BlockColumns'
7+
export {default as BlockCover} from './BlockCover'
8+
export {default as BlockHeadings} from './BlockHeadings'
9+
export {default as BlockHero} from './BlockHero'
510
export {default as BlockImage} from './BlockImage'
611
export {default as BlockImageGallery} from './BlockImageGallery'
712
export {default as BlockList} from './BlockList'
13+
export {default as BlockMediaText} from './BlockMediaText'
814
export {default as BlockNetflixCarousel} from './BlockNetflixCarousel'
915
export {default as BlockParagraph} from './BlockParagraph'
10-
export {default as BlockHeadings} from './BlockHeadings'
16+
export {default as BlockPullQuote} from './BlockPullQuote'
17+
export {default as BlockQuote} from './BlockQuote'
1118
export {default as BlockSeparator} from './BlockSeparator'
1219
export {default as BlockShortcode} from './BlockShortcode'
1320
export {default as BlockSpacer} from './BlockSpacer'
14-
export {default as BlockVideoEmbed} from './BlockVideoEmbed'
1521
export {default as BlockTable} from './BlockTable'
16-
export {default as BlockCode} from './BlockCode'
17-
export {default as BlockMediaText} from './BlockMediaText'
18-
export {default as BlockButton} from './BlockButton'
19-
export {default as BlockButtons} from './BlockButtons'
20-
export {default as BlockColumns} from './BlockColumns'
21-
export {default as BlockCover} from './BlockCover'
22+
export {default as BlockVideoEmbed} from './BlockVideoEmbed'

components/organisms/Hero/Hero.js

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Button from '@/components/atoms/Button'
2+
import cn from 'classnames'
23
import PropTypes from 'prop-types'
34
import React from 'react'
45
import tailwindConfig from '../../../tailwind.config'
@@ -8,69 +9,63 @@ import styles from './Hero.module.css'
89
* Render the Hero component.
910
*
1011
* @param {object} props Hero component props.
11-
* @param {string} props.backgroundImage The background image url.
12-
* @param {string} props.id Optional ID for component.
12+
* @param {string} props.backgroundImage The background image object.
1313
* @param {string} props.body Text for the body.
14-
* @param {object} props.cta Object with text and url props for the CTA button.
14+
* @param {string} props.className The className.
15+
* @param {object} props.ctaText The cta text.
16+
* @param {object} props.ctaUrl The cta url.
1517
* @param {string} props.subtitle Text for the subtitle.
1618
* @param {string} props.title Text for the title.
1719
* @param {object} props.children React children.
1820
* @return {Element} The Hero component.
1921
*/
2022
export default function Hero({
2123
backgroundImage,
22-
id,
2324
body,
24-
cta,
25+
className,
26+
ctaText,
27+
ctaUrl,
2528
subtitle,
26-
title,
27-
children
29+
title
2830
}) {
2931
return (
3032
<section
31-
id={id || null}
32-
className={styles.hero}
33+
className={cn(styles.hero, className)}
3334
style={{
3435
// These css custom properties are used inside the css module file to set the card's background image, tint overlay, and fallback bg color.
35-
'--image-url': `url(${backgroundImage})`,
36-
'--image-tint-color': `${tailwindConfig.theme.colors.black.DEFAULT}50`,
36+
'--image-url': `url(${backgroundImage.url})`,
37+
'--image-tint-color': `${tailwindConfig.theme.colors.black['DEFAULT']}50`,
3738
'--image-fallback-color': `${tailwindConfig.theme.colors.grey['darkest']}`
3839
}}
3940
>
4041
<div className={styles.content}>
41-
{children ? (
42-
children
43-
) : (
44-
<>
45-
{subtitle && <p className={styles.subtitle}>{subtitle}</p>}
46-
<h1 className={styles.title}>{title}</h1>
47-
{body && <p className={styles.body}>{body}</p>}
48-
{cta && (
49-
<Button
50-
className={styles.button}
51-
url={cta.url ? cta.url : null}
52-
icon={cta.icon ? cta.icon : null}
53-
text={cta.text ? cta.text : null}
54-
type="primary"
55-
size="md"
56-
/>
57-
)}
58-
</>
42+
{subtitle && <p className={styles.subtitle}>{subtitle}</p>}
43+
<h1 className={styles.title}>{title}</h1>
44+
{body && <p className={styles.body}>{body}</p>}
45+
{ctaText && ctaUrl && (
46+
<Button
47+
className={styles.button}
48+
url={ctaUrl}
49+
text={ctaText}
50+
icon="arrowRight"
51+
type="primary"
52+
size="md"
53+
/>
5954
)}
6055
</div>
6156
</section>
6257
)
6358
}
6459

6560
Hero.propTypes = {
66-
backgroundImage: PropTypes.string,
67-
id: PropTypes.string,
68-
body: PropTypes.string,
69-
cta: PropTypes.shape({
70-
icon: PropTypes.string,
71-
text: PropTypes.string,
72-
url: PropTypes.string
61+
backgroundImage: PropTypes.shape({
62+
url: PropTypes.string,
63+
alt: PropTypes.string
7364
}),
65+
body: PropTypes.string,
66+
className: PropTypes.string,
67+
ctaText: PropTypes.string,
68+
ctaUrl: PropTypes.string,
7469
subtitle: PropTypes.string,
7570
children: PropTypes.node,
7671
title: PropTypes.string

components/organisms/MediaText/MediaText.js

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,31 @@ import Button from '@/components/atoms/Button'
22
import Container from '@/components/atoms/Container'
33
import cn from 'classnames'
44
import PropTypes from 'prop-types'
5-
import React, {useEffect} from 'react'
5+
import React from 'react'
66
import styles from './MediaText.module.css'
77

88
/**
99
* Render the MediaText component.
1010
*
1111
* @param {object} props MediaText component props.
1212
* @param {string} props.body The body text.
13-
* @param {element} props.children The child elements.
1413
* @param {string} props.className The className.
15-
* @param {object} props.cta The cta object with text and url strings.
14+
* @param {object} props.ctaText The cta text.
15+
* @param {object} props.ctaUrl The cta url.
1616
* @param {object} props.image The image object with url and alt text.
1717
* @param {boolean} props.mediaLeft Whether to show media on the left of the text.
1818
* @param {string} props.title The title.
19-
* @return {Element} The MediaText component.
19+
* @return {Element} The MediaText component.
2020
*/
2121
export default function MediaText({
2222
body,
23-
children,
2423
className,
25-
cta,
24+
ctaText,
25+
ctaUrl,
2626
image,
2727
mediaLeft,
2828
title
2929
}) {
30-
useEffect(() => {
31-
if ((children && title) || (children && body) || (children && cta)) {
32-
console.warn(
33-
'The title, body and cta props are ignored when using children.'
34-
)
35-
}
36-
})
37-
3830
return (
3931
<Container>
4032
<section
@@ -45,24 +37,19 @@ export default function MediaText({
4537
)}
4638
>
4739
<div className={styles.text}>
48-
{children ? (
49-
children
50-
) : (
51-
<>
52-
{title && <h1 className={styles.title}>{title}</h1>}
53-
{body && <p className={styles.body}>{body}</p>}
54-
{cta && (
55-
<Button
56-
className={styles.button}
57-
url={cta.url ? cta.url : null}
58-
text={cta.text ? cta.text : null}
59-
icon={cta.icon ? cta.icon : null}
60-
type="primary"
61-
size="md"
62-
/>
63-
)}
64-
</>
65-
)}
40+
<>
41+
{title && <h1 className={styles.title}>{title}</h1>}
42+
{body && <p className={styles.body}>{body}</p>}
43+
{ctaText && ctaUrl && (
44+
<Button
45+
className={styles.button}
46+
url={ctaUrl}
47+
text={ctaText}
48+
type="primary"
49+
size="md"
50+
/>
51+
)}
52+
</>
6653
</div>
6754
<div className={styles.media}>
6855
{image && image.url && (
@@ -79,12 +66,8 @@ export default function MediaText({
7966
MediaText.propTypes = {
8067
body: PropTypes.string,
8168
className: PropTypes.string,
82-
children: PropTypes.element,
83-
cta: PropTypes.shape({
84-
text: PropTypes.string,
85-
url: PropTypes.string,
86-
icon: PropTypes.string
87-
}),
69+
ctaText: PropTypes.string,
70+
ctaUrl: PropTypes.string,
8871
image: PropTypes.shape({
8972
url: PropTypes.string,
9073
alt: PropTypes.string

functions/displayBlock.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,17 @@ export default function displayBlock(block, index) {
2727
return <Blocks.BlockCode {...attributes} key={index} />
2828
// case 'core/embed':
2929
// return <Blocks.BlockVideoEmbed {...attributes} key={index} />
30-
case 'core/media-text':
30+
case 'lazyblock/mediatext':
3131
return (
3232
<Blocks.BlockMediaText
33-
media={attributes}
34-
innerBlocks={innerBlocks}
33+
attributes={attributes}
34+
key={index}
35+
/>
36+
)
37+
case 'lazyblock/hero':
38+
return (
39+
<Blocks.BlockHero
40+
attributes={attributes}
3541
key={index}
3642
/>
3743
)

0 commit comments

Comments
 (0)