Skip to content

Instantly share code, notes, and snippets.

@jrmd
Created May 21, 2018 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrmd/920e6c2c682ec46a7f9613733a75b5bc to your computer and use it in GitHub Desktop.
Save jrmd/920e6c2c682ec46a7f9613733a75b5bc to your computer and use it in GitHub Desktop.
Demo overlay block
const { __ } = wp.i18n; // Import __() from wp.i18n
const { registerBlockType } = wp.blocks; // Import registerBlockType() from wp.blocks
const { Component } = wp.element;
/**
* Register: aa Gutenberg Block.
*
* Registers a new block provided a unique name and an object defining its
* behavior. Once registered, the block is made editor as an option to any
* editor interface where blocks are implemented.
*
* @link https://wordpress.org/gutenberg/handbook/block-api/
* @param {string} name Block name.
* @param {Object} settings Block settings.
* @return {?WPBlock} The block, if it has been successfully
* registered; otherwise `undefined`.
*/
registerBlockType( 'demo/block-overlay', {
// Block name. Block names must be string that contains a namespace prefix. Example: my-plugin/my-custom-block.
title: __( 'Overlay' ), // Block title.
icon: 'shield', // Block icon from Dashicons → https://developer.wordpress.org/resource/dashicons/.
category: 'common', // Block category — Group blocks together based on common traits E.g. common, formatting, layout widgets, embed.
keywords: [
__( 'overlay' ),
],
attributes: {
},
/**
* The edit function describes the structure of your block in the context of the editor.
* This represents what the editor will render when the block is used.
*
* The "edit" property must be a valid function.
*
* @link https://wordpress.org/gutenberg/handbook/block-api/block-edit-save/
*/
edit: class extends Component {
constructor(props) {
super(...arguments);
this.state = {
overlayVisible: false
};
}
toggleOverlay = (event) => {
event.preventDefault();
this.setState({
overlayVisible: !this.state.overlayVisible
})
};
render() {
return (
<div className={ this.props.className }>
<button onClick={this.toggleOverlay}>Toggle Overlay</button>
<div style={{
display: this.state.overlayVisible ? 'block' : 'none',
zIndex: 99999,
position: 'fixed',
top: 0,
left: 0,
width: '100%',
height: '100%',
backgroundColor: 'rgba(255, 255, 255, .37)'
}}>
<button onClick={this.toggleOverlay}>Toggle Overlay</button>
</div>
</div>
)
}
},
/**
* The save function defines the way in which the different attributes should be combined
* into the final markup, which is then serialized by Gutenberg into post_content.
*
* The "save" property must be specified and must be a valid function.
*
* @link https://wordpress.org/gutenberg/handbook/block-api/block-edit-save/
*/
save: function( props ) {
return null
},
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment