Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to supply a fallback image to the Image component #163

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 56 additions & 3 deletions components/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { MediaPlaceholder, InspectorControls } from '@wordpress/block-editor';
import { Spinner, FocalPointPicker, PanelBody } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import PropTypes from 'prop-types';
import { useRefEffect } from '@wordpress/compose';
import { useState } from '@wordpress/element';

import { useMedia } from '../../hooks/use-media';

Expand All @@ -12,9 +14,10 @@ const Image = (props) => {
onSelect,
focalPoint = { x: 0.5, y: 0.5 },
onChangeFocalPoint,
fallback,
...rest
} = props;
const hasImage = !!id;
const hasImage = !!id && id !== -1;
const { media, isResolvingMedia } = useMedia(id);

const shouldDisplayFocalPointPicker = typeof onChangeFocalPoint === 'function';
Expand Down Expand Up @@ -42,6 +45,54 @@ const Image = (props) => {
};
}

const [isBlockPreview, setIsBlockPreview] = useState(false);

const ref = useRefEffect((node) => {
const isRenderedInsideIframe = node.ownerDocument.documentElement.classList.contains(
'block-editor-block-preview__content-iframe',
);

setIsBlockPreview(isRenderedInsideIframe);
});

if (isBlockPreview && hasFallbackImage) {
rest.style = {
...rest.style,
maxWidth: '100%',
};
return <img src={fallback} alt="" ref={ref} {...rest} />;
}

if (!hasImage) {
return (
<>
<MediaPlaceholder onSelect={onSelect} accept="image" multiple={false} />
<span
style={{
border: 0,
clip: 'rect(1px, 1px, 1px, 1px)',
clipPath: 'inset(50%)',
height: '1px',
margin: '-1px',
overflow: 'hidden',
padding: 0,
position: 'absolute',
width: '1px',
wordWrap: 'normal !important',
}}
ref={ref}
/>
</>
);
}

if (isResolvingMedia) {
return <Spinner />;
}

const imageUrl = media?.media_details?.sizes[size]?.source_url ?? media?.source_url;
const altText = media?.alt_text;

return (
<>
{shouldDisplayFocalPointPicker && (
Expand All @@ -65,6 +116,7 @@ export { Image };

Image.defaultProps = {
size: 'large',
fallback: '',
focalPoint: { x: 0.5, y: 0.5 },
onChangeFocalPoint: undefined,
};
Expand All @@ -73,9 +125,10 @@ Image.propTypes = {
id: PropTypes.number.isRequired,
size: PropTypes.string,
onSelect: PropTypes.func.isRequired,
fallback: PropTypes.string,
onChangeFocalPoint: PropTypes.func,
focalPoint: PropTypes.shape({
x: PropTypes.string,
y: PropTypes.string,
x: PropTypes.number,
y: PropTypes.number,
}),
};
17 changes: 14 additions & 3 deletions example/src/blocks/image-example/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@
"description": "Example Block to show the Image in usage",
"icon": "smiley",
"category": "common",
"example": {},
"example": {
"attributes": {
"image": {
"id": -1,
"fallback": "https://images.unsplash.com/photo-1666625267725-310b76f139d6"
}
}
},
"supports": {
"html": false
},
"attributes": {
"imageId": {
"type": "number"
"image": {
"type": "object",
"default": {
"id": null,
"fallback": null
}
},
Comment on lines +20 to 26
Copy link
Contributor

@ncoetzer ncoetzer Oct 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabiankaegy Essentially, I think we could have gone two ways about this:

  1. The way you approached it here by using a single prop structured as an object with two values, i.e., id & fallback, respectively. However, I'm just wondering about any possible confusion about what type of data needs to be provided for these two values, i.e., number vs. string.
  2. OR perhaps it would be more clear (and less complex) to define the fallback image URL as a separate prop (optional), e.g.,
"imageId": {
    "type": "number",
    "default": null
},
"fallbackImageUrl": {
    "type": "string",
    "default": ""
}

"focalPoint": {
"type": "object",
Expand Down
8 changes: 4 additions & 4 deletions example/src/blocks/image-example/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export function BlockEdit(props) {
setAttributes
} = props;

const { imageId, focalPoint } = attributes;
const { image, focalPoint } = attributes;
const blockProps = useBlockProps();

const handleImageSelection = value => {
setAttributes({imageId: value.id })
setAttributes({image: { id: value.id } })
};
const removeImage = () => setAttributes({imageId: null});

Expand All @@ -24,11 +24,11 @@ export function BlockEdit(props) {
return (
<>
<BlockControls>
<MediaToolbar id={imageId} onSelect={ handleImageSelection } isOptional={true} onRemove={removeImage} />
<MediaToolbar id={image?.id} onSelect={ handleImageSelection } isOptional={true} onRemove={removeImage} />
</BlockControls>
<div {...blockProps}>
<h2>Hello World!</h2>
<Image id={imageId} size="large" onSelect={handleImageSelection} className="example-image" focalPoint={focalPoint} onChangeFocalPoint={handleFocalPointChange} />
<Image fallback={image?.fallback} id={image?.id} size="large" onSelect={handleImageSelection} className="example-image" focalPoint={focalPoint} onChangeFocalPoint={handleFocalPointChange} />
</div>
</>
)
Expand Down