Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/zackify/gutenblock

The easiest way to develop and release Gutenberg blocks (components) for WordPress
https://github.com/zackify/gutenblock

gutenberg gutenberg-blocks react reactjs wordpress

Last synced: 3 months ago
JSON representation

The easiest way to develop and release Gutenberg blocks (components) for WordPress

Lists

README

        

# Contents

- [Install](#install)
- [Comparison with competition](#comparison-with-other-tooling)
- [Future Plans](#future-plans)
- [Usage](#usage)
- [Creating a Block](#creating-a-block)

# Install

`npm install gutenblock -g`

This is a **Gutenberg plugin creator** + reusable inspector components with **hot loading** and **code splits** built in.

# Quickstart

If you have never done WordPress development, getting started couldn't be easier.

- [Install Docker](https://store.docker.com/search?type=edition&offering=community)
- `gutenblock init && cd blocks`
- `gutenblock watch docker`

When you add `docker` on the end of the watch command, it will bring up WordPress for you. Simply create an account, install the Gutenberg plugin, and activate the blocks plugin. You're all set.

# Comparison with other tooling

Currently, there is only one tool out there to help create blocks (that I have found so far). It's called [Create Guten Block](https://github.com/ahmadawais/create-guten-block). This library was inspired by it. I've added what I consider to be good defaults that _everyone_ would want when creating blocks. These features are not included in other libraries by default:

- Auto Block registration
- Helper utlities
- Automatic code splitting
- Hot reloading (without page reload)
- Custom webpack config without ejection

**Auto Block registration**

No need to call `registerBlockType` for WordPress. Our loader does this for you.

**Helper utilities**

Currently, when editing things in gutenberg you make components like this:

```js
const { RichText } = wp.editor;

export default ({ setAttributes, attributes }) => (


setAttributes({ title })}
/>

);
```

With Gutenblock, we created a higher order context that is loaded into all edit components. This means you can import our abstracted inputs:

```js
import { RichText } from 'gutenblock-controls';

const Edit = () => (




);
```

We've included a `Select` `MediaSelect` `Input` `Inspector` `Repeat` and other form fields to help you build blocks faster. [A repeat](/plugin/src/example/inspector.js) component will handle the hard work of letting users add infinite items to an array of form fields, replacing items, and deleting them.

The name field is the key in your gutenberg attributes defined in `block.js`. You can create your own inputs that connect and get access to `setAttributes` and `attributes`, no longer needing to pass them all over in your components. [See the example](/controls/src/form/rich-text.js)

**Code splitting**

If you have many blocks, you don't want Gutenberg to load all of that JS when it initializes. With this plugin, your edit blocks will only be loaded in once they are dragged out to the canvas.

**Hot reloading**

Every edit block is hooked into react-hot-loader with our loader so that updates won't need a full page load. Full reloads can make development much slower when Gutenberg has a lot of content on the page at once.

**Custom Webpack**

Add a `gutenblock.config.js` file in your blocks folder. It looks like this:

```js
const path = require('path');

module.exports = webpack => ({
//customize gutenblock options if needed
gutenblock: {
devHost: 'localhost',
devPort: 8080,
//build asset output path relative to the plugin directory
outputPath: '/test',
//when building the plugin, gutenblock will default to the folder name inside wp-content, if you have a different wp-content folder you can change it here
publicPath: `/app/plugins/blocks/test/`,
},
resolve: {
alias: {
shared: path.resolve(__dirname, '../src/shared'),
},
},
plugins: [new webpack.EnvironmentPlugin(['NODE_ENV'])],
module: {
rules: [
{
test: /\.css$/,
use: [require.resolve('style-loader'), require.resolve('css-loader')],
},
],
},
});
```

The configuration is the exact same as webpack with one extra piece: pass `babelOptions` with plugins and presets like a babelrc has to customize the babel loader.

If you choose to extend the configuration, down the road a future webpack release may require you to make changes and update your configuration. If you do not extend anything, you'll never have to update any configuration in order to upgrade gutenblock!

# Future plans

- Automatic i18n
- Complicated examples (tabs component, loading in data from WordPress)
- Test coverage
- Batch updates when updating nested tabs that cause lots of rerenders in Gutenberg

# Usage

`gutenblock init` will scaffold out a WordPress plugin for you.

`gutenblock watch` inside the folder will start development mode. Copy the blocks folder into your plugins directory, or [use docker](https://gist.github.com/zackify/d8e428f93e018c3fbcce512414d02e62)

`gutenblock build` production build the plugin. After, edit line 35 of `yourplugin/src/init.php` to point to the production assets. All set to publish!

# Creating a block

Inside `src` you will create blocks matching the `example` one.

All blocks need a `block.js` and `edit.js`.

`./src/paragraph/block.js`

```js
//Optionally use a save block for static rendering on the WordPress frontend

import Save from './save';

const Block = {
title: 'Paragraph',
icon: 'shield-alt',
category: 'common',
attributes: {
body: {
type: 'string',
},
},
save: Save,
};
```

`./src/paragraph/edit.js`

```js
import { RichText } from 'gutenblock-controls';

const Edit = () => (

);
```

`./src/paragraph/save.js`

```js
export default ({ attributes }) =>

{attributes.body}

;
```

Side note: We don't use save blocks at Crossfield. This is because we fetch WordPress pages and posts via the api and render the blocks using a custom react frontend. Sadly, if you use save blocks, they will not be code split. This is a limitation of the gutenberg editor not supporting awaiting to render the save method.

No registering blocks, importing them into a root folder. It's all done for you.

Now we can run `gutenblock watch` inside our plugin folder. Inside WordPress the components will hot reload as you edit, thanks to [react-hot-loader](https://github.com/gaearon/react-hot-loader)

You can read more about the [Block API](https://wordpress.org/gutenberg/handbook/block-api/)