• Stars
    star
    239
  • Rank 168,763 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

Contents

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.

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. 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:

const { RichText } = wp.editor;

export default ({ setAttributes, attributes }) => (
  <div>
    <RichText
      tagName="h1"
      value={attributes.title}
      placeholder="Title"
      onChange={title => setAttributes({ title })}
    />
  </div>
);

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

import { RichText } from 'gutenblock-controls';

const Edit = () => (
  <div>
    <RichText name="description" />
  </div>
);

We've included a Select MediaSelect Input Inspector Repeat and other form fields to help you build blocks faster. A repeat 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

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:

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

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

//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

import { RichText } from 'gutenblock-controls';

const Edit = () => (
  <RichText tagName="p" name="body" style={{ color: 'black' }} />
);

./src/paragraph/save.js

export default ({ attributes }) => <p>{attributes.body}</p>;

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

You can read more about the Block API

More Repositories

1

validify

Simple-as-possible React form validation
TypeScript
281
star
2

cli.rs

use {yourproject}.cli.rs!
275
star
3

react-calendar

Simple and flexible events calendar written in React
TypeScript
142
star
4

react-router-async-routing

react router v4 async routing
JavaScript
127
star
5

reactview

reactview lets you instantly view (and hot reload) any react component in your browser
JavaScript
96
star
6

react-native-autofocus

Autofocus the next input field on enter in React Native
JavaScript
86
star
7

use-upload

Framework agnostic library handling file uploads with progress, direct to AWS / GCP, and other locations
TypeScript
77
star
8

match

a port of Rust's match in JS
JavaScript
77
star
9

formjs

A form generator for Reactjs "Alpachajs for React" Open the inspector on the demo page:
JavaScript
49
star
10

react-slug

A React component that will turn any input into a URL friendly slug!
JavaScript
23
star
11

graphql-course

Code from ultimatecourses
JavaScript
17
star
12

moovite

TypeScript
15
star
13

Simpler

A simple react / angular clone. Well, that's the end goal anyway. Just an experiment.
JavaScript
11
star
14

react-runner

build react apps with minimal setup WIP
JavaScript
10
star
15

Spot

a horribly coded thingy using contenteditable attributes
PHP
9
star
16

stock-api

get latest stock price data using puppeteer and google
JavaScript
8
star
17

redux-restore

Handles storage in React Native
JavaScript
8
star
18

flydb

nosql, in memory database for json documents
Rust
6
star
19

nexus-prisma-boilerplate

Get started with Nexus and Prisma quickly
TypeScript
6
star
20

legit.js

a lightweight javascript framework for modifying the DOM / CSS
JavaScript
6
star
21

passwordless

[Beta] Open source webauthn hardware key implementation for Node + JS
TypeScript
5
star
22

nodechat

working on making a full on live chatroom with node.js and socket.io
JavaScript
4
star
23

reason-apollo-server

Set of bindings for Apollo Server in ReasonML
OCaml
4
star
24

restor

simpler-than-redux react state management
JavaScript
3
star
25

React-Generator

quickly create components and get to coding!
JavaScript
3
star
26

vscode-sort-by-length

Sort imports by line length in VS Code!
TypeScript
3
star
27

render-placement-loader

Add React.render to your component automatically (used in reactview)
JavaScript
2
star
28

webpack-force-hash-plugin

Only changes the vendor hash if needed
JavaScript
2
star
29

react-easy-test

need a better name... scroll down and see what this is!
JavaScript
2
star
30

memeify

A slack bot that generates memes from photos you choose!
JavaScript
2
star
31

suspense-fetch

experimental useFetch with suspense concurrent mode
JavaScript
1
star
32

action.js

a mini library for turning form requests into ajax
JavaScript
1
star
33

stream

a simple node / react app for displaying live streams
JavaScript
1
star
34

iBlog

a PHP project I made back when I was 15 (5 years ago) woah....
PHP
1
star
35

hmr-apollo-server

Apollo server 2 with hot module replacement
JavaScript
1
star
36

homie

iOS app that turns on Philips Hue lights using iBeacons (WIP)
Swift
1
star
37

huetron

a desktop app for managing your hue lights
JavaScript
1
star
38

class

js / node coding course
JavaScript
1
star
39

react-suspendable

a function that can wrap any async action and make it compatible with suspense
TypeScript
1
star
40

WProgress

Reading progress bar and other readability enhancements for your Wordpress blog
JavaScript
1
star
41

react-state-management

Created with StackBlitz ⚑️
JavaScript
1
star