admin管理员组文章数量:1410682
I have been searching everywhere to figure out how to add an event inside a gutenberg block. I am working to add an accordion system using ACF and Foundation. I have created a block and fields and template using ACF. I would like my users to be able to open and close the accordion in the visual mode.
I did find the following script that monitors when blocks are changed. The only issue is that it fires before everything is loaded and I had to use a timeout to allow the blocks to fully load. I have been unable to find a better way of acplishing this. Any suggestions?
const getBlockList = () => wp.data.select( 'core/editor' ).getBlocks();
let blockList = getBlockList();
wp.data.subscribe(() => {
const newBlockList = getBlockList();
const blockListChanged = newBlockList !== blockList;
blockList = newBlockList;
if ( blockListChanged ) {
setTimeout(function(){
jQuery(document).foundation();
Foundation.reInit($('[data-accordion]'));
}, 4000);
}
});
I have been searching everywhere to figure out how to add an event inside a gutenberg block. I am working to add an accordion system using ACF and Foundation. I have created a block and fields and template using ACF. I would like my users to be able to open and close the accordion in the visual mode.
I did find the following script that monitors when blocks are changed. The only issue is that it fires before everything is loaded and I had to use a timeout to allow the blocks to fully load. I have been unable to find a better way of acplishing this. Any suggestions?
const getBlockList = () => wp.data.select( 'core/editor' ).getBlocks();
let blockList = getBlockList();
wp.data.subscribe(() => {
const newBlockList = getBlockList();
const blockListChanged = newBlockList !== blockList;
blockList = newBlockList;
if ( blockListChanged ) {
setTimeout(function(){
jQuery(document).foundation();
Foundation.reInit($('[data-accordion]'));
}, 4000);
}
});
Share
Improve this question
asked Oct 30, 2018 at 20:49
JamesJames
7202 gold badges15 silver badges39 bronze badges
1
- This doesn't answer your question but for consideration, you can create accordion blocks easily with the block api, without using AFC. React has onclick functions built in. The block api can replace ACF entirely for a better ux – CyberJunkie Commented Jan 9, 2019 at 5:23
1 Answer
Reset to default 3On the front end you get static html just like before. If you add on click event to your ponent in save function, it will be stripped off during serialization so no need to sweat there. Everything works as before blocks.
However, on the editor side, you can add an event on any element in edit function's return element:
Here is how you do it in es6:
const handleClick = (event) => {
console.log(event)
}
const element = <div onClick={handleClick}>Click Me</div>;
or in es2015:
var handleClick = function handleClick(event) {
console.log(event);
};
var element = React.createElement(
"div",
{ onClick: handleClick },
"Click Me"
);
By the way, this is a gutenber block, not its in memory representation rendered by react.
<!-- wp:paragraph {"key": "value"} -->
<p>Wele to the world of blocks.</p>
<!-- /wp:paragraph -->
本文标签: javascriptWordpress Add onclick event inside Gutenberg blockStack Overflow
版权声明:本文标题:javascript - Wordpress Add onclick event inside Gutenberg block - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744988293a2636226.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论