-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into dependabot/npm_and_yarn/eslint-8.38.0
- Loading branch information
Showing
11 changed files
with
156 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# GenericServerSideEdit | ||
|
||
The `GenericServerSideEdit` component is a generic block edit component that uses server side rendering to display content. | ||
|
||
## Usage | ||
|
||
For a minimum working setup, all you need to do is import the `GenericServerSideEdit` component and assign it as the `edit` parameter when registering a block: | ||
|
||
```js | ||
import { registerBlockType } from '@wordpress/blocks'; | ||
|
||
import blockMetadata from './block.json'; | ||
import GenericServerSideEdit from '@humanmade/block-editor-components'; | ||
|
||
registerBlockType( blockMetadata, { | ||
edit: GenericServerSideEdit, | ||
} ); | ||
|
||
``` | ||
|
||
Optionally, you can use it inside an edit component with inspector controls via a fragment like this: | ||
|
||
```js | ||
import { Panel, PanelBody } from '@wordpress/components'; | ||
import { InspectorControls } from '@wordpress/block-editor'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
import GenericServerSideEdit from '@humanmade/block-editor-components'; | ||
|
||
/** | ||
* Render the editor interface. | ||
* | ||
* @returns {import('react').ReactNode} Rendered editorial UI. | ||
*/ | ||
export default function Edit( { attributes, context, setAttributes } ) { | ||
return <> | ||
<InspectorControls> | ||
<Panel> | ||
<PanelBody | ||
title={ __( 'Panel Title', 'mytextdomain' ) } | ||
> | ||
<p>{ __( 'Panel Content', 'mytextdomain' ) }</p> | ||
</PanelBody> | ||
</Panel> | ||
</InspectorControls> | ||
<GenericServerSideEdit | ||
name="my/blockname" | ||
attributes={ attributes } | ||
context={ context } | ||
/> | ||
</>; | ||
} | ||
``` | ||
|
||
## Props | ||
|
||
These props with the exception of `inspectorControls` are the same props provided to edit components. | ||
|
||
### `name` | ||
|
||
The name of the block being rendered. | ||
|
||
| Type | Required | Default | | ||
|--------------------------------------|--------------------------------------|--------------------------------------| | ||
| `string` | yes | `undefined` | | ||
|
||
### `attributes` | ||
|
||
The block attributes to use | ||
|
||
| Type | Required | Default | | ||
|--------------------------------------|--------------------------------------|--------------------------------------| | ||
| `object` | yes | `null` | | ||
|
||
### `context` | ||
|
||
Necessary for context aware blocks, currently supports the `post_id` context if available. | ||
|
||
| Type | Required | Default | | ||
|--------------------------------------|--------------------------------------|--------------------------------------| | ||
| `object` | no | `null` | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { useBlockProps } from '@wordpress/block-editor'; | ||
import { Disabled } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import ServerSideRender from '@wordpress/server-side-render'; | ||
|
||
/** | ||
* Renders the block edit interface using server side rendering. | ||
* | ||
* @param {object} props Block props | ||
* @param {object} props.attributes block attributes | ||
* @param {object} props.context block context data | ||
* @param {string} props.name name of block | ||
* @returns {React.ReactNode} Rendered editorial UI. | ||
*/ | ||
function GenericServerSideEdit( { attributes, context, name } ) { | ||
|
||
/** | ||
* A generic empty response component that uses the standard wp-block-name class | ||
* for when an empty value is returned by the API. | ||
* | ||
* @returns {React.ReactNode} block rendered as empty component. | ||
*/ | ||
const emptyResponse = () => { | ||
return ( | ||
<div className={ `wp-block-${ name.replace( '/', '-' ) }` } > | ||
{ name } { __( 'Block rendered as empty.' ) } | ||
</div> | ||
); | ||
}; | ||
|
||
return ( | ||
<div { ...useBlockProps() }> | ||
<Disabled> | ||
<ServerSideRender | ||
attributes={ attributes } | ||
block={ name } | ||
EmptyResponsePlaceholder={ emptyResponse } | ||
urlQueryArgs={ | ||
( typeof context === 'object' && Object.hasOwn( context, 'postId' ) ) ? { post_id: context.postId } : {} | ||
} | ||
/> | ||
</Disabled> | ||
</div> | ||
); | ||
} | ||
|
||
export default GenericServerSideEdit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters