admin管理员组文章数量:1314480
When registering new blocks, on front-end I get only the markup that I specify inside of save
method. Cool! That's what I need.
But when using the edit
method I get my markup wrapped in a div
. That's not what I need, because it limits my possibilities of styling.
How can I get rid of that div and have it similar to what the current core/paragraph block does, for example, with the p
tag?
Here is how I have it now:
and that's how it should become:
For reference the code is simple like this:
edit: () => {
return (
<span className="sample-block">
<mark>Sample</mark>
</span>
);
},
save: () => {
return (
<span className="sample-block">
<mark>Sample</mark>
</span>
);
},
When registering new blocks, on front-end I get only the markup that I specify inside of save
method. Cool! That's what I need.
But when using the edit
method I get my markup wrapped in a div
. That's not what I need, because it limits my possibilities of styling.
How can I get rid of that div and have it similar to what the current core/paragraph block does, for example, with the p
tag?
Here is how I have it now:
and that's how it should become:
For reference the code is simple like this:
edit: () => {
return (
<span className="sample-block">
<mark>Sample</mark>
</span>
);
},
save: () => {
return (
<span className="sample-block">
<mark>Sample</mark>
</span>
);
},
Share
Improve this question
asked Nov 20, 2020 at 10:41
Andrei SurduAndrei Surdu
6491 gold badge7 silver badges18 bronze badges
1 Answer
Reset to default 0Update: WordPress 5.6 will be released soon and this will include a block API version 2 which allows all these modifications noted in OP. https://make.wordpress/core/2020/11/18/block-api-version-2/
I found a filter that allows me to modify the wrapper. Still, I can't change the HTML tag, but it's better than nothing.
const { createHigherOrderComponent } = wppose;
const withClientIdClassName = createHigherOrderComponent( ( BlockListBlock ) => {
return ( props ) => {
const newClassName = "block-" + props.clientId + ' sample-block';
return <BlockListBlock { ...props } className={ newClassName } />;
};
}, 'withClientIdClassName' );
wp.hooks.addFilter( 'editor.BlockListBlock', 'awps/modify-blocks-wrapper', withClientIdClassName );
And the methods:
edit: () => {
return (
<mark>Sample</mark>
);
},
save: () => {
return (
<span className="sample-block">
<mark>Sample</mark>
</span>
);
},
本文标签: Do not wrap custom block in an additional DIV but instead use only the JSX I provide
版权声明:本文标题:Do not wrap custom block in an additional DIV but instead use only the JSX I provide 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741968674a2407699.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论