Gutenberg Examples
Gutenberg Blocks
by Space-Rocket · github.com/space-rocket/gutenberg-blocks · website
Install
No release zip yet. The repository archive installs, but the folder name will carry the branch suffix and updates will not flow:
wp plugin install https://github.com/space-rocket/gutenberg-blocks/archive/refs/heads/master.zipQM Gutenberg Blocks
Uses https://github.com/WordPress/gutenberg-examples as a starting point
To make new blocks use https://github.com/bigbassroller/gutenberg-block-builder. To run it you will need Elixir installed. To install Elixir, go to https://elixir-lang.org/install.html. Install the block builder inside the plugin root directory. Open up make_block.exs and change the block name in the fields :
block_dash_name
block_space_name
block_name
Once you've done that you can run elixir make_block.exs and it will create copy of two-column-callout block with the name of the new block. Copy the folder of the new block to the root of the qm-gutenberg-blocks plugin folder and run npm install (it takes awhile to install the dependencies) and the run npm start. After that all you have to do is add the block to qm-gutenberg-blocks/index.php and add for example: include 'image-strip/index.php';.
See two-column-callout/src/index.js to get a general idea of how the blocks are built. The three main parts are:
- Top level registerBlockType settings and attributes
- Edit function attributes, functions and rendered JSX
- Save function with data passed down from the Edit function and rendered JSX
Each template part should be its own block. Simply add the HTML to rendered JSX and add the necessary editable fields the Edit function. See https://github.com/WordPress/gutenberg-examples, https://github.com/bigbassroller/gutenberg-course and https://github.com/bigbassroller/advanced-gutenberg-course for my examples.
For tutorial see https://wordpress.org/gutenberg/handbook/designers-developers/
Here are some example steps taken to add dynamic inputs:
Adding a media upload button and displaying it in your template
- Add
MediaUploadto the global object set by wp.editor - Add
MediaUploadto the global object set by wp.editor - Add button object from wp.components
- Add
mediaIDandmediaURLobjects to the theattributesobject inside yourregisterBlockType - Add
mediaIDandmediaURLinside the attributes object inside theeditobject literal, - Add
onSelectImageconst that is an IIFE - Add
MediaUploadtag to the edit template - Add Add
mediaIDandmediaURLto save function attributes that is set from props - Finally add an object inside the template that imediately invokes the image if mediaURL && image is available
Adding Alignment and using it in your template
- Add
AlignmentToolbarandBlockControlsto the global object set by wp.editor - Add
alignmentto theregisterBlockType’sattributesobject - Add
alignmentto the edit function attributes - Add
onChangeAlignmentevent listener - Add object with
BlockControlstag inside of it - Add the value to the className
Adding a button link and using it inside your template
- Import
URLInputfrom wp.editor imports - Import
IconButtonfrom wp.components - Add
title,text,urlto theregisterBlockType’sattributesobject - Add
title,textandurlto the edit function attributes - Add
isSelectedto objects imported from props - Add object with isSelected ternary with RichText
- Add form with URLInput with
value={url}and onChange have url equal setAttributes function with url object passed in. - Add
IconButtonwith type=submit - Add if not isSelected markup with a href that uses the url and text object values