• Stars
    star
    159
  • Rank 235,916 (Top 5 %)
  • Language
    CSS
  • License
    GNU General Publi...
  • Created over 4 years ago
  • Updated almost 2 years ago

Reviews

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

Repository Details

Build and Share WordPress Blocks in Isolation.

BlockBook

โœจ Build and Share WordPress Blocks in Isolation.

BlockBook is a development environment for WordPress/Gutenberg blocks. It allows you to browse a block library, view the properties of each block, and develop and view the block's output in isolation.

It runs outside of your plugin/theme and outside of WordPress or any Gutenberg environment. This allows you to develop block in isolation, which can improve component reuse, testability, and development speed.

Here is an example featuring the main WordPress Core Blocks: https://youknowriad.github.io/blockbook/

Features

  • Discover and showcase your blocks, attributes, previews and markup.
  • Support static blocks.
  • Test blocks in different themes.
  • Hot/Live Reloading.
  • Generate a static website.

๐Ÿš€ Getting Started

First install blockbook:

cd my-blocks-plugin
npm install blockbook-cli --save-dev

Once installed, add the two following scripts to the scripts section of your package.json

{
	"scripts": {
		"blockbook:start": "blockbook start",
		"blockbook:build": "blockbook build"
	}
}

The last step is to load your blocks and register them in your BlockBook. To do so, create an index.js file inside a .blockbook folder at the root of your project.

This file represents the entry point of your BlockBook config. You can use it to register block types in your BlockBook or provide alternative themes.

Registering Block Types

// .blockbook/index.js

// It's important to import the JS code that registers your block in the @wordpress/blocks package
import './my-block-file.js';

// You should also import the stylesheets (editor and style) of your blocks.
import './my-block-styles.css';

// Once your blocks are loaded, make sure to tell BlockBook to add them to the menu.
import { registerBlockType } from 'blockbook-api';

registerBlockType( 'myplugin/myblock-name' );

And your block file (the same one that is loaded in your WordPress plugin) should look something like that:

import { registerBlockType } from '@wordpress/blocks';

registerBlockType( 'myplugin/myblock-name', blockSettings );

You can check the example folder in this repository to see a working BlockBook.

Registering Themes

BlockBook also allows you to test your blocks with several theme editor styles, or if you're a theme developer, build your editor theme styles in isolation and test theme quickly on different blocks.

To do so, you can register your theme like so:

// .blockbook/index.js
import myThemeStyle from '!!raw-loader!./path-to-my-theme-editor-styles.css';
import { registerTheme } from 'blockbook-api';

registerTheme( {
	name: 'my-theme-name',
	title: 'My Theme',
	editorStyles: myThemeStyle,
} );

Registering Block Stories

While developing blocks, you want to be able to see different variations of the same block at the same to ensure the changes you're making are correct no matter the block attributes. To do so you can register Block Stories like so:

// .blockbook/index.js
import { registerBlockStory } from 'blockbook-api';
registerBlockStory( 'core/buttons', {
	name: 'Two Buttons', // Name of the story
	blocks: [
		// A blocks array, each block has a name, a set of attributes and potentially inner blocks
		{
			name: 'core/buttons',
			attributes: {
				align: 'center',
			},
			innerBlocks: [
				{
					name: 'core/button',
					attributes: {
						backgroundColor: 'very-dark-gray',
						borderRadius: 0,
						text: 'Get Started',
					},
					innerBlocks: [],
				},
				{
					name: 'core/button',
					attributes: {
						borderRadius: 0,
						className: 'is-style-outline',
						text: 'Learn more',
						textColor: 'very-dark-gray',
					},
					innerBlocks: [],
				},
			],
		},
	],
} );

Custom Build Setup

By default, BlockBook works well with projects relying on the default wp-scripts setup. That said, if you have your own custom setup for JavaScript and Styles, it is possible to use BlockBook by overriding the default webpack config. To do so, add a webpack.config.js in the .blockbook folder that looks like that:

// .blockbook/webpack.config.js

module.exports = ( defaultWebpackConfig ) => {
   // Here you can apply modifications to the default config `defaultWebpackConfig`

   return defaultWebpackConfig;
}

Frequent Questions

Can I use the WordPress globals in my block code?

Since BlockBook is an isolated environment, by default, it assumes that you're consuming code using import like suggested by the wp-scripts default setup. So your block code should be something like:

import { registerBlockType } from '@wordpress/blocks';

registerBlockType( myBlockName, myBlockSettings );

but we noticed that a lot of plugins use the WordPress globals directly without build step, more like that:

wp.blocks.registerBlockType( myBlockName, myBlockSettings );

In order to support the various ways, developers can write their block plugins, some of these globals are made available in BlockBook as well. For the moment, the following variables are available:

wp.data
wp.hooks
wp.blocks
wp.domReady
wp.i18n

If your block plugin relies on other variables not available at the moment, please do open an issue on the repository to ask for inclusion.

Contributing

We welcome contributions to BlockBook! We have a lot of ideas on the roadmap where you could help, check the repository issues for more details.

๐Ÿ“ฅ Pull requests and ๐ŸŒŸ Stars are always welcome.

More Repositories

1

wp-js-plugin-starter

Just another WordPress plugin starter
Shell
219
star
2

gcf

Gutenberg Custom Fields... wait what?
JavaScript
196
star
3

asblocks

Share content as blocks
JavaScript
97
star
4

standalone-gutenberg

Gutenberg Playground as a standalone editor
CSS
96
star
5

blocky

Fastest way to create a WordPress block.
JavaScript
76
star
6

react-graphql-redux

This library allows you to use GraphQL to query your Redux store
JavaScript
49
star
7

dropit

Insert photos from unsplash.com directly inside your Gutenberg Editor.
JavaScript
47
star
8

gutenberg-extensibility-workshop

JavaScript
47
star
9

authenticate.js

AuthenticateJS is a simple angular library for handling security
JavaScript
38
star
10

anchour

A deployment engine for web applications
PHP
26
star
11

OREM

Restfull API Abstraction Layer
PHP
26
star
12

rungen

A generic runtime around async flow in javascript
JavaScript
22
star
13

kubrick2

Block Based Kubrick
HTML
21
star
14

bloginy

Blog Aggregator based on Symfony2
PHP
19
star
15

averroes

A Gutenberg compatible markdown editor. Write in Markdown, edit in Markdown and preview in HTML.
JavaScript
15
star
16

angular2-cookbooks

Learn Angular 2 by practice
HTML
13
star
17

redux-rungen

Write your redux action creators as generators
JavaScript
13
star
18

gutenberg-vue

A Gutenberg Vue Prototype
Vue
12
star
19

newtify

New Notification Center for WordPress
JavaScript
9
star
20

bedoon

yet another no backend solution using mongodb and nodejs
JavaScript
8
star
21

app-boilerplate

A template for building, a fast realtime application with angular + nodejs + socket.io
JavaScript
5
star
22

wanchour

Web application to manage your deployments using anchour
PHP
4
star
23

intercom2db

Intercom2db is a small utility that allow you to extract data from http://intercom.io to a database.
JavaScript
4
star
24

asblocks-collab

AsBlocks collaboration server.
JavaScript
4
star
25

codevitals

JavaScript
3
star
26

cas-bundle

CAS Authentification Bundle in Symfony2
PHP
3
star
27

degres

Dependency graph resolver
JavaScript
2
star
28

dzblogday_wall

The Wall of the DZBlogDay
PHP
2
star
29

angular-datastore

yet another angular data abstraction layer
JavaScript
2
star
30

RizewayJobBundle

A simple job bundle for symfony 2 projects (with doctrine)
PHP
2
star
31

zik42day

What to listen today ?
TypeScript
2
star
32

fenyan

i'm lazy
JavaScript
1
star
33

react-redux-auth-poc

JavaScript
1
star
34

redux-prez

Redux Meetup LyonJS
JavaScript
1
star