admin管理员组文章数量:1287833
I'm building a few custom blocks and have come across something I am not sure is either possible at the current time (as there does not seem to be docs on it), or it is something people are still figuring out.
I'm wondering is it possible to access the image srcsets in a custom blocks. This is my current markup which is accessing the sizes array to get my chosen image size. Which is ok, but obviously not ideal when you get down to a mobile size.
attributes: {
mediaID: {
type: "number",
},
mediaURL: {
type: "string",
source: "attribute",
selector: "img.promogroupitem__media--img",
attribute: "src",
},
mediaAlt: {
type: 'string',
source: 'attribute',
selector: 'img.promogroupitem__media--img',
attribute: 'alt'
},
mediaWidth: {
type: 'number',
source: 'attribute',
selector: 'img.promogroupitem__media--img',
attribute: 'width',
},
mediaHeight: {
type: 'number',
source: 'attribute',
selector: 'img.promogroupitem__media--img',
attribute: 'height',
},
},
parent: 'wpboiler-core/promo-group',
edit: function(props) {
const { attributes, setAttributes } = props;
const {
mediaID,
mediaURL,
mediaAlt,
mediaWidth,
mediaHeight,
} = attributes;
const onSelectImage = media => setAttributes({
mediaID: media.id,
mediaURL: (media.sizes.promo ? media.sizes.promo.url : media.url),
mediaAlt: media.alt,
mediaWidth: (media.sizes.promo ? media.sizes.promo.width : media.width),
mediaHeight: (media.sizes.promo ? media.sizes.promo.height : media.height),
});
return el(
'article',
useBlockProps(attributes),
el(
InspectorControls,
null,
// IMAGE UPLOAD BEGIN
el(
PanelBody,
{
title: "Background Image",
},
el(MediaUpload, {
onSelect: onSelectImage,
allowedTypes: "image",
value: mediaID,
render: (obj) => {
return el(
Button,
{
className: "components-button is-primary",
onClick: obj.open,
},
!mediaID
? __("Upload Image", "card-group")
: __("Replace Image", "card-group")
);
},
}),
mediaID
? el(
Button,
{
className: "components-button is-tertiary",
style: { marginLeft: "5px" },
onClick: () => setAttributes({
mediaID: "",
mediaURL: "",
mediaAlt: "",
mediaHeight: "",
mediaWidth: "",
}),
},
"Remove Image"
)
: ""
)
// IMAGE UPLOAD END
),
el(
'div',
{ className: 'promogroupitem__container' },
mediaURL ?
el(
'div',
{ className: 'promogroupitem__media' },
el(
'img',
{
className: 'promogroupitem__media--img',
src: mediaURL,
loading: 'lazy',
width: mediaWidth,
height: mediaHeight,
alt: mediaAlt,
}
),
) : '',
)
);
},
save: function(props) {
const { attributes } = props;
const {
mediaURL,
mediaAlt,
mediaHeight,
mediaWidth,
} = attributes;
return el(
'article',
{ className: 'promogroupitem' },
el(
'div',
{ className: 'promogroupitem__container' },
mediaURL ?
el(
'div',
{ className: 'promogroupitem__media' },
el(
'img', {
className: 'promogroupitem__media--img',
src: mediaURL,
loading: 'lazy',
width: mediaWidth,
height: mediaHeight,
alt: mediaAlt,
}
),
) : '',
),
);
},
Its something I am not quite sure how to resolve. Has anyone come across this and developed/found a solution for it?
本文标签: imagesUsing srcsets in a custom block
版权声明:本文标题:images - Using srcsets in a custom block 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741329130a2372667.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论