-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Inner blocks: edit/save symmetry + stabilise API #26031
Conversation
Size Change: +107 B (0%) Total Size: 1.08 MB
ℹ️ View Unchanged
|
@ellatrix I think we should also update the documentation of the Component itself here: https://github.com/WordPress/gutenberg/blob/trunk/packages/block-editor/src/components/inner-blocks/README.md as part of this PR. I can take care of that once you rebased this pr if that works for you (y) |
f3ce3f2
to
81a8c9e
Compare
efd7b73
to
69d544a
Compare
69d544a
to
dfa5b25
Compare
72e7db6
to
9b4c461
Compare
Thank you @ellatrix for all your work on this :) I really appreciate your work here and just want you to know that it is very much appreciated! Excited for this to be I. 5.9 :) |
It looks like we were using it wrong, in a way that worked before WordPress/gutenberg#26031 but breaks after.
* Fix tweetstorm test for good It keeps flopping between 1 and 2, so accept either * Fix use of InnerBlocks.Content It looks like we were using it wrong, in a way that worked before WordPress/gutenberg#26031 but breaks after. Co-authored-by: Renovate Bot <[email protected]> Co-authored-by: matticbot <[email protected]> Co-authored-by: Brad Jorsch <[email protected]>
* Fix tweetstorm test for good It keeps flopping between 1 and 2, so accept either * Fix use of InnerBlocks.Content It looks like we were using it wrong, in a way that worked before WordPress/gutenberg#26031 but breaks after. Co-authored-by: Renovate Bot <[email protected]> Co-authored-by: matticbot <[email protected]> Co-authored-by: Brad Jorsch <[email protected]> Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/1446415445
* Fix tweetstorm test for good It keeps flopping between 1 and 2, so accept either * Fix use of InnerBlocks.Content It looks like we were using it wrong, in a way that worked before WordPress/gutenberg#26031 but breaks after. Co-authored-by: Renovate Bot <[email protected]> Co-authored-by: matticbot <[email protected]> Co-authored-by: Brad Jorsch <[email protected]> Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/1446415445
* Fix tweetstorm test for good It keeps flopping between 1 and 2, so accept either * Fix use of InnerBlocks.Content It looks like we were using it wrong, in a way that worked before WordPress/gutenberg#26031 but breaks after. Co-authored-by: Renovate Bot <[email protected]> Co-authored-by: matticbot <[email protected]> Co-authored-by: Brad Jorsch <[email protected]> Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/1446415445
* Fix tweetstorm test for good It keeps flopping between 1 and 2, so accept either * Fix use of InnerBlocks.Content It looks like we were using it wrong, in a way that worked before WordPress/gutenberg#26031 but breaks after. Co-authored-by: Renovate Bot <[email protected]> Co-authored-by: matticbot <[email protected]> Co-authored-by: Brad Jorsch <[email protected]> Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/1446415445
* Fix tweetstorm test for good It keeps flopping between 1 and 2, so accept either * Fix use of InnerBlocks.Content It looks like we were using it wrong, in a way that worked before WordPress/gutenberg#26031 but breaks after. Co-authored-by: Renovate Bot <[email protected]> Co-authored-by: matticbot <[email protected]> Co-authored-by: Brad Jorsch <[email protected]> Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/1446415445
* Fix tweetstorm test for good It keeps flopping between 1 and 2, so accept either * Fix use of InnerBlocks.Content It looks like we were using it wrong, in a way that worked before WordPress/gutenberg#26031 but breaks after. Co-authored-by: Renovate Bot <[email protected]> Co-authored-by: matticbot <[email protected]> Co-authored-by: Brad Jorsch <[email protected]> Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/1446415445
* Fix tweetstorm test for good It keeps flopping between 1 and 2, so accept either * Fix use of InnerBlocks.Content It looks like we were using it wrong, in a way that worked before WordPress/gutenberg#26031 but breaks after. Co-authored-by: Renovate Bot <[email protected]> Co-authored-by: matticbot <[email protected]> Co-authored-by: Brad Jorsch <[email protected]> Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/1446415445
Dev Note Draft:Take more control over Inner Block Areas (as a block developer)Prior to WordPress 5.9 the only way to work with inner block was to use the With WordPress 5.9 we're introducing a new react hook called To use the hook, take the object returned from calling the hook and spread it onto the host element you want to render the inner blocks into. For example: function BlockEdit(props) {
const blockProps = useBlockProps();
const innerBlocksProps = useInnerBlocksProps();
return (
<section {...blockProps}>
<div {...innerBlocksProps} />
</section>
);
} The above code will render to the following markup in the editor: <section>
<div>
<!-- Inner Blocks get inserted here -->
</div>
</section> The hook also accepts two arguments:
useInnerBlocksProps( additionalWrapperProps, innerBlocksProps ); Saving the inner blocksTo save inner blocks content there is a special variant of the hook, this is similar to the component-based approach that uses function BlockSave( props ) {
const innerBlocksProps = useInnerBlocksProps.save( { className: 'my-class' } );
return (
<section {...innerBlocksProps} ) />
);
} When you use the Why use hooks?The hooks allow for new options and nice enhancements. For example, you can take the object returned from the function BlockEdit(props) {
const blockProps = useBlockProps();
const innerBlocksProps = useInnerBlocksProps( blockProps );
return (
<section {...innerBlocksProps} />
);
} <section>
<!-- Inner Blocks get inserted here -->
</section> Another benefit to using the hook approach is using the returned value, which is just an object, and deconstruct to get the react function BlockEdit(props) {
const blockProps = useBlockProps();
const { children, ...innerBlocksProps } = useInnerBlocksProps( blockProps );
return (
<section {...innerBlocksProps}>
{ children }
<!-- Insert any arbitrary html here at the same level as the children -->
</section>
);
} <section>
<!-- Inner Blocks get inserted here -→
<!-- The custom html gets rendered on the same level -->
</section> Example:
{
"apiVersion": 2,
"name": "example/block",
"title": "Example",
"category": "text",
"editorScript": "file:./index.js"
}
import { registerBlockType } from '@wordpress/blocks';
import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor';
import metadata from './block.json';
function BlockEdit(props) {
const blockProps = useBlockProps( { className: 'my-class' } );
const innerBlocksProps = useInnerBlocksProps(
blockProps,
{ allowedBlocks: [ 'core/heading', 'core/paragraph', 'core/image' ] }
);
return (
<section {...innerBlocksProps} />
);
}
function BlockSave(props) {
const blockProps = useBlockProps.save( { className: 'my-class' } );
const innerBlocksProps = useInnerBlocksProps.save( blockProps );
return <section {...innerBlocksProps} />
}
registerBlockType( metadata, {
edit: BlockEdit,
save: BlockSave,
} ) |
@fabiankaegy, great working putting this dev note together. It is nicely structured as each section builds on top of each other and includes nicely crafted examples. I don't know the new API fully but I find this explanation very informative 💯 One thing that would be worth explaining explicitly is that similar to how |
@gziolo Updated the comment with a new section called |
@fabiankaegy This looks good, thanks for taking the time to put it together. I edited the note above for clarity and wording in a couple spots (you can see a diff of the edits using the dropdown). It is good to publish to make/core, use the |
Thank you for the dev note @fabiankaegy! It looks great. |
* Fix tweetstorm test for good It keeps flopping between 1 and 2, so accept either * Fix use of InnerBlocks.Content It looks like we were using it wrong, in a way that worked before WordPress/gutenberg#26031 but breaks after. Co-authored-by: Renovate Bot <[email protected]> Co-authored-by: matticbot <[email protected]> Co-authored-by: Brad Jorsch <[email protected]> Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/1446415445
Description
Previously #25644 and #25633.
Fixes #35071.
This Replaces the
InnerBlocks.Content
component with a functionuseInnerBlocksProps.save
for thesave
component so it's similar in use asuseInnerBlocksProps
in theedit
component. There's no need for an API version bump since both can be used, but the goal would be to eventually deprecateInnerBlocks.Content
.The only instances that I didn't convert were dynamic blocks that have save functions that return only
<innerBlocks.Content />
. I'm not sure what the reason is to return the inner blocks without a wrapper. I see some dynamic blocks that don't have a save function at all or that do have a wrapper div.How has this been tested?
Screenshots
Types of changes
Checklist: