The ImageSelect
component allows for selecting a single image from the Media Library.
Internally, ImageSelect
is wrapping a MediaPlaceholder
component.
For a minimum working setup, you need to pass an image object as image
, as well as an onChange
callback that accepts an image object.
If focal point setter is needed, then you need to
- set
useFocalPoint
totrue
. - add
focalPoint
attribute andonChangeFocalPoint
callback that handles updating focal point value.
import { ImageSelect } from '@meom/block-components';
// Inside block `edit` function:
const {
attributes: { image, focalPoint },
setAttributes,
} = props;
<ImageSelect
image={image}
onChange={(newImage) => setAttributes({ image: newImage })}
useFocalPoint={true}
focalPoint={focalPoint}
onChangeFocalPoint={(newFocalPoint) =>
setAttributes({ focalPoint: newFocalPoint })
}
/>;
Example:
import { ImageSelect } from '@meom/block-components';
// Inside block `edit` function:
const {
attributes: { image },
setAttributes,
} = props;
<ImageSelect
buttonText={'Change Image'}
image={image}
onChange={(newImage) => setAttributes({ image: newImage })}
/>;
The button text to remove and replace the image.
- Type:
string
- Required: No
- Default:
Remove Image
Example:
import { ImageSelect } from '@meom/block-components';
// Inside block `edit` function:
const {
attributes: { image },
setAttributes,
} = props;
<ImageSelect
showRemoveImageButton={false}
image={image}
onChange={(newImage) => setAttributes({ image: newImage })}
/>;
Property to decide if remove image button want's to be shown.
- Type:
boolean
- Required: No
- Default:
true
The button text for image settings. For example focalpoint picker.
- Type:
string
- Required: No
- Default:
Image settings
The callback to use for handling changing the image.
Please note that onChange
will receive an image object from the Media Library.
An image object.
Remember to define image
in block.json
file as attribute:
"attributes": {
"image": {
"type": "object"
}
}
Boolean which determines do we use MediaToolbar
in this image.
- Type:
boolean
- Required: No
- Default:
true
// Hide `MediaToolbar`.
<ImageSelect
useMediaToolbar={false}
image={image}
onChange={(newImage) => setAttributes({ image: newImage })}
/>;
Boolean which determines do we use focalpoint in this image.
- Type:
boolean
- Required: No
- Default:
false
An object that includes x
and y
coordinates as percentage. Values should be set between 0 and 1.
"attributes": {
"focalPoint": {
"type": "object",
"default": {
"x": 0.5,
"y": 0.5
}
}
}
The callback to use for handling focal point percentage change.
Callback should update focalPoint
attribute values with new x
and y
percentages between values 0 and 1.
The focalpoint label.
- Type:
string
- Required: No
- Default:
Image focalpoint
The focalpoint label helper text.
- Type:
string
- Required: No
- Default:
Pick image focalpoint which is central point of the image.
Note that attr
is custom function for getting the attribute. Here is an example how to use focalpoint in PHP
$focal_point = attr( 'focalPoint', $attributes, [ 'x' => 0.5, 'y' => 0.5 ] );
$focal_point_x = $focal_point && isset( $focal_point['x'] ) ? $focal_point['x'] * 100 : false;
$focal_point_y = $focal_point && isset( $focal_point['y'] ) ? $focal_point['y'] * 100 : false;
$focal_point_styles = $focal_point_x && $focal_point_y ? "object-position: {$focal_point_x}% {$focal_point_y}%" : '';
wp_get_attachment_image( $image_id, 'full', '', [ 'loading' => 'eager', 'style' => esc_attr( $focal_point_styles ) ] );