-
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 9 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 |
---|---|---|
|
@@ -370,27 +370,6 @@ _Returns_ | |
|
||
- `Array`: Block settings. | ||
|
||
### getBlockVariations | ||
|
||
Returns an array with the variations of a given block type. | ||
|
||
_Parameters_ | ||
|
||
- _blockName_ `string`: Name of block (example: “core/columns”). | ||
- _scope_ `[WPBlockVariationScope]`: Block variation scope name. | ||
|
||
_Returns_ | ||
|
||
- `(WPBlockVariation[]|void)`: Block variations. | ||
|
||
### getCategories | ||
|
||
Returns all the block categories. | ||
|
||
_Returns_ | ||
|
||
- `WPBlockCategory[]`: Block categories. | ||
|
||
### getChildBlockNames | ||
|
||
Returns an array with the child blocks of a given block. | ||
|
@@ -685,6 +664,25 @@ _Returns_ | |
|
||
Registers a new block collection to group blocks in the same namespace in the inserter. | ||
|
||
_Usage_ | ||
|
||
```js | ||
import { __ } from '@wordpress/i18n'; | ||
import { registerBlockCollection, registerBlockType } from '@wordpress/blocks'; | ||
|
||
// Register the collection. | ||
registerBlockCollection( 'my-collection', { | ||
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>, | ||
} ); | ||
``` | ||
|
||
_Parameters_ | ||
|
||
- _namespace_ `string`: The namespace to group blocks by in the inserter; corresponds to the block namespace. | ||
|
@@ -696,6 +694,29 @@ _Parameters_ | |
|
||
Registers a new block style variation for the given block. | ||
|
||
_Usage_ | ||
|
||
```js | ||
import { __ } from '@wordpress/i18n'; | ||
import { registerBlockStyle } from '@wordpress/blocks'; | ||
import { Button } from '@wordpress/components'; | ||
|
||
const ExampleComponent = () => { | ||
return ( | ||
<Button | ||
onClick={ () => { | ||
registerBlockStyle( 'core/quote', { | ||
name: 'fancy-quote', | ||
label: __( 'Fancy Quote' ), | ||
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. One thing that should be included as a note is how CSS styles can be wired with a newly registered variation. Maybe it's enough to link to the documentation that explains all the details: |
||
} ); | ||
} } | ||
> | ||
{ __( 'Add a new block style for core/quote' ) } | ||
</Button> | ||
); | ||
}; | ||
``` | ||
|
||
_Parameters_ | ||
|
||
- _blockName_ `string`: Name of block (example: “core/latest-posts”). | ||
|
@@ -707,6 +728,21 @@ Registers a new block provided a unique name and an object defining its | |
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) | ||
|
||
_Usage_ | ||
|
||
```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>, | ||
} ); | ||
``` | ||
|
||
_Parameters_ | ||
|
||
- _blockNameOrMetadata_ `string|Object`: Block type name or its metadata. | ||
|
@@ -720,6 +756,30 @@ _Returns_ | |
|
||
Registers a new block variation for the given block type. | ||
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 can reference here the documentation page: |
||
|
||
_Usage_ | ||
|
||
```js | ||
import { __ } from '@wordpress/i18n'; | ||
import { registerBlockVariation } from '@wordpress/blocks'; | ||
import { Button } from '@wordpress/components'; | ||
|
||
const ExampleComponent = () => { | ||
return ( | ||
<Button | ||
onClick={ () => { | ||
registerBlockVariation( 'core/embed', { | ||
name: 'custom', | ||
title: __( 'My Custom Embed' ), | ||
attributes: { providerNameSlug: 'custom' }, | ||
} ); | ||
} } | ||
> | ||
__( 'Add a custom variation for core/embed' ) } | ||
</Button> | ||
); | ||
}; | ||
``` | ||
|
||
_Parameters_ | ||
|
||
- _blockName_ `string`: Name of the block (example: “core/columns”). | ||
|
@@ -766,6 +826,37 @@ _Returns_ | |
|
||
Sets the block categories. | ||
|
||
_Usage_ | ||
|
||
```js | ||
import { __ } from '@wordpress/i18n'; | ||
import { store as blocksStore, setCategories } from '@wordpress/blocks'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { Button } from '@wordpress/components'; | ||
|
||
const ExampleComponent = () => { | ||
// Retrieve the list of current categories. | ||
const blockCategories = useSelect( | ||
( select ) => select( blocksStore ).getCategories(), | ||
[] | ||
); | ||
|
||
return ( | ||
<Button | ||
onClick={ () => { | ||
// Add a custom category to the existing list. | ||
setCategories( [ | ||
...blockCategories, | ||
{ title: 'Custom Category', slug: 'custom-category' }, | ||
] ); | ||
} } | ||
> | ||
{ __( 'Add a new custom block category' ) } | ||
</Button> | ||
); | ||
}; | ||
``` | ||
|
||
_Parameters_ | ||
|
||
- _categories_ `WPBlockCategory[]`: Block categories. | ||
|
@@ -774,6 +865,20 @@ _Parameters_ | |
|
||
Assigns the default block name. | ||
|
||
_Usage_ | ||
|
||
```js | ||
import { setDefaultBlockName } from '@wordpress/blocks'; | ||
|
||
const ExampleComponent = () => { | ||
return ( | ||
<Button onClick={ () => setDefaultBlockName( 'core/heading' ) }> | ||
{ __( 'Set the default block to Heading' ) } | ||
</Button> | ||
); | ||
}; | ||
``` | ||
|
||
_Parameters_ | ||
|
||
- _name_ `string`: Block name. | ||
|
@@ -790,6 +895,20 @@ _Parameters_ | |
|
||
Assigns name of block for handling block grouping interactions. | ||
|
||
_Usage_ | ||
|
||
```js | ||
import { setGroupingBlockName } from '@wordpress/blocks'; | ||
|
||
const ExampleComponent = () => { | ||
return ( | ||
<Button onClick={ () => setGroupingBlockName( 'core/columns' ) }> | ||
{ __( 'Set the default block to Heading' ) } | ||
ryanwelcher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</Button> | ||
); | ||
}; | ||
``` | ||
|
||
_Parameters_ | ||
|
||
- _name_ `string`: Block name. | ||
|
@@ -849,6 +968,26 @@ _Returns_ | |
|
||
Unregisters a block style variation for the given block. | ||
|
||
_Usage_ | ||
|
||
```js | ||
import { __ } from '@wordpress/i18n'; | ||
import { unregisterBlockStyle } from '@wordpress/blocks'; | ||
import { Button } from '@wordpress/components'; | ||
|
||
const ExampleComponent = () => { | ||
return ( | ||
<Button | ||
onClick={ () => { | ||
unregisterBlockStyle( 'core/quote', 'plain' ); | ||
} } | ||
> | ||
{ __( 'Remove the "Plain" block style for core/quote' ) } | ||
</Button> | ||
); | ||
}; | ||
``` | ||
|
||
_Parameters_ | ||
|
||
- _blockName_ `string`: Name of block (example: “core/latest-posts”). | ||
|
@@ -858,6 +997,23 @@ _Parameters_ | |
|
||
Unregisters a block. | ||
|
||
_Usage_ | ||
|
||
```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> | ||
); | ||
}; | ||
``` | ||
|
||
_Parameters_ | ||
|
||
- _name_ `string`: Block name. | ||
|
@@ -870,6 +1026,26 @@ _Returns_ | |
|
||
Unregisters a block variation defined for the given block type. | ||
|
||
_Usage_ | ||
|
||
```js | ||
import { __ } from '@wordpress/i18n'; | ||
import { unregisterBlockVariation } from '@wordpress/blocks'; | ||
import { Button } from '@wordpress/components'; | ||
|
||
const ExampleComponent = () => { | ||
return ( | ||
<Button | ||
onClick={ () => { | ||
unregisterBlockVariation( 'core/embed', 'youtube' ); | ||
} } | ||
> | ||
{ __( 'Remove the YouTube variation from core/embed' ) } | ||
</Button> | ||
); | ||
}; | ||
``` | ||
|
||
_Parameters_ | ||
|
||
- _blockName_ `string`: Name of the block (example: “core/columns”). | ||
|
@@ -879,6 +1055,26 @@ _Parameters_ | |
|
||
Updates a category. | ||
|
||
_Usage_ | ||
|
||
```js | ||
import { __ } from '@wordpress/i18n'; | ||
import { updateCategory } from '@wordpress/blocks'; | ||
import { Button } from '@wordpress/components'; | ||
|
||
const ExampleComponent = () => { | ||
return ( | ||
<Button | ||
onClick={ () => { | ||
updateCategory( 'text', { title: __( 'Written Word' ) } ); | ||
} } | ||
> | ||
{ __( 'Update Text category title' ) } | ||
</Button> | ||
); | ||
}; | ||
``` | ||
|
||
_Parameters_ | ||
|
||
- _slug_ `string`: Block category slug. | ||
|
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!