-
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
[Documentation] Adding @example
entries to the public API exposed in core/blocks
#42745
Changes from 3 commits
0cf85d1
e8dd7c7
a3e28bc
e302a88
2e32e3d
f9f6859
7fc7b90
bca3a77
fb82046
40dfff9
e3a36f3
07f6196
c39c0b7
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 |
---|---|---|
|
@@ -231,9 +231,23 @@ function getBlockSettingsFromMetadata( { textdomain, ...metadata } ) { | |
* behavior. Once registered, the block is made available as an option to any | ||
* editor interface where blocks are implemented. | ||
* | ||
* For more in-depth information on registering a custom block see the [Create a block tutorial](docs/how-to-guides/block-tutorial/README.md) | ||
* | ||
* @param {string|Object} blockNameOrMetadata Block type name or its metadata. | ||
* @param {Object} settings Block settings. | ||
* | ||
* @example | ||
* ```js | ||
* import { __ } from '@wordpress/i18n'; | ||
* import { registerBlockType } from '@wordpress/blocks' | ||
* | ||
* registerBlockType( 'namespace/block-name', { | ||
* title: __( 'My First Block' ), | ||
* edit: () => <div>{ __( 'Hello from the editor!' ) }</div>, | ||
* save: () => <div>{ __( 'Hello from the saved content!' ) }</div>, | ||
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. This example could be misleading, as typically As the saved content is static within a post it wouldn't have the desired effect, and it may cause block validation issues if the block is saved in one language and loaded in another. It might be good to remove the edit: ( { attributes } ) => <div>{ attributes.content }</div>,
save: ( { attributes } ) => <div>{ attributes.content }</div>, 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. This is very interesting. Has this been tested? If this is the case, we'll need to change other reference such as the 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. The rule to follow for static blocks is that the Checking the codebase this bug seems to already exist in the file block (it's the only one that uses To repro:
I would recommend reproducing without the Gutenberg plugin active, as a lot of the translations are missing (at least for the dev version). I'll make a seperate bug report for it if there isn't one already. (#43013) 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. Great catch @talldan with the File block and the translation used with the
Yes, that is a great point. It never occurred to me that it might cause issues but now it seems so obvious 🙈 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. OK cool. I'll update the example here and then do a PR to change the template the create-block package and related items. |
||
* } ); | ||
* ``` | ||
* | ||
* @return {?WPBlockType} The block, if it has been successfully registered; | ||
* otherwise `undefined`. | ||
*/ | ||
|
@@ -343,6 +357,24 @@ function translateBlockSettingUsingI18nSchema( | |
* @param {Object} settings The block collection settings. | ||
* @param {string} settings.title The title to display in the block inserter. | ||
* @param {Object} [settings.icon] The icon to display in the block inserter. | ||
* | ||
* @example | ||
* ```js | ||
* import { __ } from '@wordpress/i18n'; | ||
* import { registerBlockCollection, registerBlockType } from '@wordpress/blocks'; | ||
* | ||
* // Register the collection. | ||
* registerBlockCollection( 'my-collection', { | ||
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. Do we have any detailed documentation for block collections? It would be great to have on presenting how to group blocks in the inserter by shared namespace. 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 don't believe we do. I have it on my list to create some. |
||
* title: __( 'Custom Collection' ), | ||
* } ); | ||
* | ||
* // Register a block in the same namespace to add it to the collection. | ||
* registerBlockType( 'my-collection/block-name', { | ||
* title: __( 'My First Block' ), | ||
* edit: () => <div>{ __( 'Hello from the editor!' ) }</div>, | ||
* save: () => <div>{ __( 'Hello from the saved content!' ) }</div>, | ||
* } ); | ||
* ``` | ||
*/ | ||
export function registerBlockCollection( namespace, { title, icon } ) { | ||
dispatch( blocksStore ).addBlockCollection( namespace, title, icon ); | ||
|
@@ -363,6 +395,24 @@ export function unregisterBlockCollection( namespace ) { | |
* | ||
* @param {string} name Block name. | ||
* | ||
* @example | ||
* ```js | ||
* import { __ } from '@wordpress/i18n'; | ||
* import { unregisterBlockType } from '@wordpress/blocks'; | ||
* | ||
* const ExampleComponent = () => { | ||
* return ( | ||
* <Button | ||
* onClick={ () => | ||
* unregisterBlockType( 'my-collection/block-name' ) | ||
* } | ||
* > | ||
* { __( 'Unregister my custom block.' ) } | ||
* </Button> | ||
* ); | ||
* }; | ||
* ``` | ||
* | ||
* @return {?WPBlockType} The previous block value, if it has been successfully | ||
* unregistered; otherwise `undefined`. | ||
*/ | ||
|
@@ -427,6 +477,20 @@ export function getUnregisteredTypeHandlerName() { | |
* Assigns the default block name. | ||
* | ||
* @param {string} name Block name. | ||
* | ||
* @example | ||
* ```js | ||
* import { setDefaultBlockName } from '@wordpress/blocks'; | ||
* | ||
* const ExampleComponent = () => { | ||
* | ||
* return ( | ||
* <Button onClick={ () => setDefaultBlockName( 'core/heading' ) }> | ||
* { __( 'Set the default block to Heading' ) } | ||
* </Button> | ||
* ); | ||
* }; | ||
* ``` | ||
*/ | ||
export function setDefaultBlockName( name ) { | ||
dispatch( blocksStore ).setDefaultBlockName( name ); | ||
|
@@ -436,6 +500,20 @@ export function setDefaultBlockName( name ) { | |
* Assigns name of block for handling block grouping interactions. | ||
* | ||
* @param {string} name Block name. | ||
* | ||
* @example | ||
* ```js | ||
* import { setGroupingBlockName } from '@wordpress/blocks'; | ||
* | ||
* const ExampleComponent = () => { | ||
* | ||
* return ( | ||
* <Button onClick={ () => setGroupingBlockName( 'core/columns' ) }> | ||
* { __( 'Set the default block to Heading' ) } | ||
* </Button> | ||
* ); | ||
* }; | ||
* ``` | ||
*/ | ||
export function setGroupingBlockName( name ) { | ||
dispatch( blocksStore ).setGroupingBlockName( name ); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As pointed out by @talldan, let's avoid using translations with
save
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's odd, I thought I removed it. Thanks for catching this!