admin管理员组文章数量:1125596
I'm trying to add a tailwind class to the block wrapper of a custom block I'm developing.
However, useBlockProps({className: 'h-full'}) generates an additional wrapper with the tailwind class name instead of adding the class to the block wrapper which is what I presumed it does.
I'm not able to figure out if this is expected behaviour from the sparse documentation on this hook. If it is, is there another way I could simply add a class to the block wrapper? If not, where am I going wrong? Code and image of outputted html below.
I should also add that I'm working with a dynamic block so I only need to add the class to the block wrapper in the editor and not on the front end.
import { registerBlockType } from "@wordpress/blocks"
import { Button,PanelBody,PanelRow } from "@wordpress/components"
import { InspectorControls,MediaUpload,MediaUploadCheck } from "@wordpress/block-editor"
import { useBlockProps,useInnerBlocksProps,InnerBlocks } from '@wordpress/block-editor';
registerBlockType("custom-cafe-theme/custom-column",{
title: "Custom Column",
attributes: {
imgID: { type: "number" },
imgUrl: { type: 'string',default: '' },
},
edit: EditComponent,
save: SaveComponent
})
function EditComponent(props) {
const blockProps = useBlockProps({ className: 'h-full' });
const innerBlocksProps = useInnerBlocksProps({ className: 'grid gap-8 px-6 py-16' });
function onImageSelect(image) {
props.setAttributes({ imgID: image.id,imgUrl: image.url })
}
function removeImage() {
props.setAttributes({ imgID: 0,imgUrl: '' })
}
return (
<>
<InspectorControls>
<PanelBody>
<PanelRow>
<MediaUploadCheck>
<MediaUpload onSelect={onImageSelect} value={props.attributes.imgID} render={({ open }) => {
return <Button onClick={open}>Upload or Replace Image</Button>
}} />
</MediaUploadCheck>
</PanelRow>
<PanelRow>
<MediaUploadCheck>
<Button onClick={removeImage}>Remove Image</Button>
</MediaUploadCheck>
</PanelRow>
</PanelBody>
</InspectorControls>
<div {...blockProps}>
<div className="w-full min-h-[300px] bg-no-repeat bg-cover bg-center grid justify-center items-center text-center md:h-full md:min-h-[400px]" style={{ backgroundImage: `url('${props.attributes.imgUrl}')` }}>
<div {...innerBlocksProps}></div>
</div>
</div>
</>
)
}
function SaveComponent() {
return <InnerBlocks.Content />
}
I'm trying to add a tailwind class to the block wrapper of a custom block I'm developing.
However, useBlockProps({className: 'h-full'}) generates an additional wrapper with the tailwind class name instead of adding the class to the block wrapper which is what I presumed it does.
I'm not able to figure out if this is expected behaviour from the sparse documentation on this hook. If it is, is there another way I could simply add a class to the block wrapper? If not, where am I going wrong? Code and image of outputted html below.
I should also add that I'm working with a dynamic block so I only need to add the class to the block wrapper in the editor and not on the front end.
import { registerBlockType } from "@wordpress/blocks"
import { Button,PanelBody,PanelRow } from "@wordpress/components"
import { InspectorControls,MediaUpload,MediaUploadCheck } from "@wordpress/block-editor"
import { useBlockProps,useInnerBlocksProps,InnerBlocks } from '@wordpress/block-editor';
registerBlockType("custom-cafe-theme/custom-column",{
title: "Custom Column",
attributes: {
imgID: { type: "number" },
imgUrl: { type: 'string',default: '' },
},
edit: EditComponent,
save: SaveComponent
})
function EditComponent(props) {
const blockProps = useBlockProps({ className: 'h-full' });
const innerBlocksProps = useInnerBlocksProps({ className: 'grid gap-8 px-6 py-16' });
function onImageSelect(image) {
props.setAttributes({ imgID: image.id,imgUrl: image.url })
}
function removeImage() {
props.setAttributes({ imgID: 0,imgUrl: '' })
}
return (
<>
<InspectorControls>
<PanelBody>
<PanelRow>
<MediaUploadCheck>
<MediaUpload onSelect={onImageSelect} value={props.attributes.imgID} render={({ open }) => {
return <Button onClick={open}>Upload or Replace Image</Button>
}} />
</MediaUploadCheck>
</PanelRow>
<PanelRow>
<MediaUploadCheck>
<Button onClick={removeImage}>Remove Image</Button>
</MediaUploadCheck>
</PanelRow>
</PanelBody>
</InspectorControls>
<div {...blockProps}>
<div className="w-full min-h-[300px] bg-no-repeat bg-cover bg-center grid justify-center items-center text-center md:h-full md:min-h-[400px]" style={{ backgroundImage: `url('${props.attributes.imgUrl}')` }}>
<div {...innerBlocksProps}></div>
</div>
</div>
</>
)
}
function SaveComponent() {
return <InnerBlocks.Content />
}
Share
Improve this question
edited Jun 26, 2023 at 15:42
Gayle F.
asked Jun 26, 2023 at 14:32
Gayle F.Gayle F.
213 bronze badges
9
|
Show 4 more comments
1 Answer
Reset to default 1I was bitten by this as well, and it caused me much frustration. I had exactly the same nested block duplication problem that you described above.
I was able to solve this issue by specifying an apiVersion for my custom block.
E.g.:
registerBlockType("custom-cafe-theme/custom-column",{
apiVersion: 2, // or 3
title: "Custom Column",
attributes: {
imgID: { type: "number" },
imgUrl: { type: 'string',default: '' },
},
edit: EditComponent,
save: SaveComponent
})
本文标签: plugin developmentuseBlockProps() nests wrapper with class name inside block wrapper in the editor
版权声明:本文标题:plugin development - useBlockProps() nests wrapper with class name inside block wrapper in the editor 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736620644a1945568.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
<div>
with an inline style attribute, this could be simplified to a single<div>
– Tom J Nowell ♦ Commented Jun 26, 2023 at 15:20