-
Notifications
You must be signed in to change notification settings - Fork 4.7k
PHP-only blocks: ensure editor interactions and block supports work correctly #72039
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
Changes from all commits
6a26046
c5320c5
bec6580
d694208
3b6ec07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,8 +10,9 @@ import { | |
| store as blocksStore, | ||
| } from '@wordpress/blocks'; | ||
| import { select } from '@wordpress/data'; | ||
| import { createElement } from '@wordpress/element'; | ||
| import ServerSideRender from '@wordpress/server-side-render'; | ||
| import { useBlockProps } from '@wordpress/block-editor'; | ||
| import { useServerSideRender } from '@wordpress/server-side-render'; | ||
| import { __, sprintf } from '@wordpress/i18n'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
|
|
@@ -326,11 +327,39 @@ export const registerCoreBlocks = ( | |
| registerBlockType( blockName, { | ||
| title: blockName, | ||
| ...( bootstrappedApiVersion < 3 && { apiVersion: 3 } ), | ||
| edit: ( { attributes } ) => { | ||
| return createElement( ServerSideRender, { | ||
| edit: function Edit( { attributes } ) { | ||
| const blockProps = useBlockProps(); | ||
| const { content, status, error } = useServerSideRender( { | ||
| block: blockName, | ||
| attributes, | ||
| } ); | ||
|
|
||
| if ( status === 'loading' ) { | ||
| return ( | ||
| <div { ...blockProps }>{ __( 'Loading…' ) }</div> | ||
| ); | ||
| } | ||
|
|
||
| if ( status === 'error' ) { | ||
| return ( | ||
| <div { ...blockProps }> | ||
| { sprintf( | ||
| /* translators: %s: error message describing the problem */ | ||
| __( 'Error loading block: %s' ), | ||
| error | ||
| ) } | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <div | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there auto-registered blocks who's wrapper is not a div?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Related question, is this div also present in the frontend or is this going to result in a different DOM tree than frontend?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's only for the editor; the frontend only shows the content of the render callback. I don't think it's a necessity to allow other wrappers, unless we add an optional attribute to allow a limited set of wrappers (e.g., figure)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this is an issue for all blocks that use "server sider render" component or hook. I wonder if there's a way to unify the DOM trees. A question for later.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand that one of the reasons for not recommending the ServerSideRender package (or useServerSideRender hook) is that it creates the extra div wrapper in the Edit component. It would be great if we could make the DOM tree perfectly match on both the frontend and the editor. Do you guess this is technically possible?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might be able to use a library like this
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's possible to try something like this yes, but I don't believe that's the reason for not using client side edit, client site edit is more about tailored UX.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm curious about this, for example, creating a React component dynamically based on a string defined on the PHP side?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No I meant that we don't recommend ServerSideRender because the ideal experience is that the block in the editor is interactive and updates as you make block modifications without reaching the server, which is not the case for ServerSiteRender. That said, that doesn't stop us from making ServerSideRender better for those who use it and server side registered block. And using a library like you shared could be a good path forward to make the editor markup match the frontend. |
||
| { ...blockProps } | ||
| dangerouslySetInnerHTML={ { | ||
| __html: content || '', | ||
| } } | ||
| /> | ||
| ); | ||
| }, | ||
| save: () => null, | ||
| } ); | ||
|
|
||


Uh oh!
There was an error while loading. Please reload this page.