Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Inner Blocks (ESNext) to use latest API #163

Merged
merged 3 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update block.
  • Loading branch information
ryanwelcher committed Oct 19, 2021
commit 7856eb98831cb3862a5a5a49cc0366f1af3e733c
10 changes: 10 additions & 0 deletions 06-inner-blocks-esnext/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"apiVersion": 2,
"name": "gutenberg-examples/example-06-esnext",
"title": "Example: Inner Blocks (ESNext)",
"category": "layout",
"icon": "universal-access-alt",
"textDomain": "gutenberg-examples",
"example": {},
"editorScript": "file:./build/index.js"
}
20 changes: 2 additions & 18 deletions 06-inner-blocks-esnext/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,7 @@ function gutenberg_examples_06_esnext_register_block() {
return;
}

// automatically load dependencies and version
$asset_file = include(plugin_dir_path(__FILE__) . 'build/index.asset.php');

wp_register_script(
'gutenberg-examples-06-esnext',
plugins_url( 'build/index.js', __FILE__ ),
$asset_file['dependencies'],
$asset_file['version'],
true
);

register_block_type(
'gutenberg-examples/example-06-esnext',
[
'editor_script' => 'gutenberg-examples-06-esnext',
]
);

// Register the block by passing the location of block.json to register_block_type.
register_block_type( __DIR__ );
}
add_action( 'init', 'gutenberg_examples_06_esnext_register_block' );
14 changes: 14 additions & 0 deletions 06-inner-blocks-esnext/src/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* WordPress dependencies
*/
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor';

const Edit = () => {
const blockProps = useBlockProps();
return (
<div { ...blockProps }>
<InnerBlocks />
</div>
);
};
export default Edit;
34 changes: 16 additions & 18 deletions 06-inner-blocks-esnext/src/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
/**
* WordPress dependencies
*/
import { registerBlockType } from '@wordpress/blocks';
import { InnerBlocks } from '@wordpress/block-editor';

registerBlockType( 'gutenberg-examples/example-06-esnext', {
title: 'Example: Inner Blocks (ESNext)',
category: 'layout',
edit: ( { className } ) => {
return (
<div className={ className }>
<InnerBlocks />
</div>
);
},
save: ( { className } ) => {
return (
<div className={ className }>
<InnerBlocks.Content />
</div>
);
},
/**
* Internal dependencies
*/
import json from '../block.json';
import edit from './edit';
import save from './save';

const { name, ...settings } = json;

registerBlockType( name, {
...settings,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One note. Technically speaking, you don't need to spread settings here. They are exposed from the server when registered with register_block_type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for confirming that! It was a question that came up in a couple of live streams.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/WordPress/gutenberg/blob/1a3bda10d3d04233e864d89eb9db3ca2adb0e62c/packages/blocks/src/api/registration.js#L258-L271

That means that whatever the client provides overrides what the server provides (if registered also on the server).

If you register blocks only with JavaScript then you surely need to spread settings from block.json here.

Copy link
Member

@gziolo gziolo Oct 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

edit,
save,
} );
14 changes: 14 additions & 0 deletions 06-inner-blocks-esnext/src/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* WordPress dependencies
*/
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor';

const Save = () => {
const blockProps = useBlockProps.save();
return (
<div { ...blockProps }>
<InnerBlocks.Content />
</div>
);
};
export default Save;