Last active
April 7, 2021 15:56
-
-
Save phpbits/1e649ba3982fd5d29943ca21f5f68450 to your computer and use it in GitHub Desktop.
Apply custom class to Gutenberg block element. View full tutorials at https://jeffreycarandang.com/extending-gutenberg-core-blocks-with-custom-attributes-and-controls/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* External Dependencies | |
*/ | |
import classnames from 'classnames'; | |
/** | |
* Add custom element class in save element. | |
* | |
* @param {Object} extraProps Block element. | |
* @param {Object} blockType Blocks object. | |
* @param {Object} attributes Blocks attributes. | |
* | |
* @return {Object} extraProps Modified block element. | |
*/ | |
function applyExtraClass( extraProps, blockType, attributes ) { | |
const { visibleOnMobile } = attributes; | |
//check if attribute exists for old Gutenberg version compatibility | |
//add class only when visibleOnMobile = false | |
if ( typeof visibleOnMobile !== 'undefined' && !visibleOnMobile ) { | |
extraProps.className = classnames( extraProps.className, 'mobile-hidden' ); | |
} | |
return extraProps; | |
} | |
addFilter( | |
'blocks.getSaveContent.extraProps', | |
'editorskit/applyExtraClass', | |
applyExtraClass | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is it possible to add extraclass to core blocks ?