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

Commit 852863f

Browse files
committed
Refactor MediaText for lazyblocks
1 parent 444f9dd commit 852863f

File tree

3 files changed

+37
-96
lines changed

3 files changed

+37
-96
lines changed
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/organisms/MediaText/MediaText.js

Lines changed: 20 additions & 37 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.
1919
* @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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ 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}
3534
key={index}
3635
/>
3736
)

0 commit comments

Comments
 (0)