Skip to content

API for Inter-block communication #5012

@Shelob9

Description

@Shelob9

I made this trivial example of how two blocks can communicate between each other. https://gitlab.com/caldera-labs/gutenberg-examples/ex-6-counter It is Redux's counter example in WordPress.

@gziolo asked me to open an issue with use cases where plugin developers need to subscribe to changes in the editor or subscribe to changes in each other's state. We discussed it here

Specifically @gziolo said:

The only limitation with the way you dispatch actions as you did is that you don’t have access to other modules. That’s why I raised it. I think we need a high level API which will allow to do some actions programmatically. I guess someone needs to provide a valid use case first

I gave the example of a client site I had that needed to populate WooCommerce's metabox with data from an API lookup based on one field's value. I used jQuery.on() and jQuery.val() a lot on that job. With Gutenberg, it would be crazy for me as a plugin developer to use the DOM as the single source of truth for state. I want to subscribe to state through wp.data, which I can.

Another example would be if we had a custom block for information about a musical artist. We might also want to develop a custom block that extended the default SoundCloud block, but had a subscription to the artist block. We'd want our extended SoundCloud block to update to a playlist of that artist, if the custom artist block existed.

Relationships between posts of different types -- books to authors -- is something that plugins like Pods or Posts 2 Posts do. The UI is tricky, since you really want to edit two posts at once. In the author metabox of the books post type, you should be able to select and edit existing authors and create new authors.

With Gutenberg, I'd like to be able to develop a set of blocks for books and a set of blocks for authors. The set of blocks I'm building for books will need to include an author block, I'd like to use the same components to construct the book author block as I do constructing the blocks I need for the author post type.

As a plugin developer I need an API that provides for inter-block communication and is consistently shaped no matter which of these two contexts I'm in. Also, once this API is well defined, I can use it with other instances of wp.data or of Redux, which makes my components potentially useful in the front-end, which is where this gets even more fun :)

Related for context:

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Feature] ExtensibilityThe ability to extend blocks or the editing experience[Priority] HighUsed to indicate top priority items that need quick attention

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions