Skip to content
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

Add exercise 1 for block registration #23

Open
wants to merge 45 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
6b0df69
Creat the exercise of block.json
kienstra Jan 27, 2022
e84f6ee
Add an exercise file with comments about what to do
kienstra Jan 27, 2022
8d0a5f7
Simplify README.md in how it describes registration
kienstra Jan 27, 2022
31c01e3
Merge branch 'main' into exercise/1-block-json
kienstra Jan 27, 2022
cc28a12
Add comments about how to register the block in PHP
kienstra Jan 27, 2022
776620c
Merge branch 'main' into exercise/1-block-json
kienstra Jan 27, 2022
4a927bf
Make the instructions more clear
kienstra Jan 27, 2022
68b184e
Add 🚧 emojis to show what to add
kienstra Jan 28, 2022
9ebcc8f
Merge branch 'main' into exercise/1-block-json
kienstra Jan 28, 2022
01eb4c2
Add links to the files and text to INSTRUCTIONS.md
kienstra Jan 28, 2022
3c618e1
Add an example block, add links
kienstra Jan 28, 2022
178277d
Add a missing 'with'
kienstra Jan 28, 2022
9647938
Capitalize Block Directory
kienstra Jan 28, 2022
905fd64
Change the function to register_block_type()
kienstra Jan 28, 2022
cce0020
Add placeholders in Edit and Save
kienstra Jan 29, 2022
e42f8a0
Remove the full URL
kienstra Jan 29, 2022
1d440d7
Change to 'All content'
kienstra Jan 29, 2022
35c50e8
Change spaces to a tab
kienstra Jan 29, 2022
94d69fe
Add a hint about destructuring
kienstra Jan 29, 2022
54fce3c
Remove always, which doesn't add much
kienstra Jan 29, 2022
66b6cf5
Fix a type of editor
kienstra Jan 29, 2022
8dfcd8a
Change it from must be in roo to must be present
kienstra Jan 29, 2022
54b52b4
Remove the line about being in the root of the plugin
kienstra Jan 29, 2022
d4899ea
Change 'The' to 'A'
kienstra Jan 29, 2022
bef8fdb
Add a link to block.json
kienstra Jan 29, 2022
f783bf4
Remove 'the' before the emoji
kienstra Jan 29, 2022
70f2ab6
Merge in main, resolve conflicts
kienstra Feb 2, 2022
5aa580a
Add comments about the arguments to __()
kienstra Feb 2, 2022
5159aca
Remove a part about the saved HTML
kienstra Feb 2, 2022
5a0a256
Add to INSTRUCTIONS.md to clarify registration
kienstra Feb 4, 2022
cef3b4f
Merge in main, resolve conflict
kienstra Feb 4, 2022
0b931a8
Change the heading in INSTRUCTIONS.md
kienstra Feb 10, 2022
6dd5736
Merge branch 'main' into exercise/1-block-registration
kienstra Feb 10, 2022
9fedb8b
Remove needless __ import
kienstra Feb 10, 2022
8443c47
Add a link for the solution video
kienstra Feb 10, 2022
5cbf6ce
Make the link text only lower case in the first word
kienstra Feb 10, 2022
f75580c
Merge in main, resolve conflict
kienstra Feb 18, 2022
6d1626f
Merge in the latest from main
kienstra Mar 17, 2022
a69e65b
Correct another merge conflict I forgot
kienstra Mar 17, 2022
34bd173
Update index.final.js for the latest changes to main
kienstra Mar 17, 2022
0c48470
Clarify the use of edit in INSTRUCTIONS.md
kienstra Mar 17, 2022
7d6dd80
Remove 'Block' from the plugin name
kienstra Mar 17, 2022
f25c417
Infer the return type, no need to have a return tag
kienstra Mar 17, 2022
80ba3b6
Change the order of the exercise files
kienstra Mar 18, 2022
52aa9f4
Add the solution video to INSTRUCTIONS.md, see if that works
kienstra Mar 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions INSTRUCTIONS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Block Registration

All content in the WordPress block editor is a block.

This is similar to other editors like [Medium](https://medium.com/), [Notion](https://www.notion.so/), and [MailChimp](https://mailchimp.com/).

Blocks are usually interactive, so they have a React component to edit them.

In this exercise, we're going to register a block so the editor is aware of it.

The modern way to register a WordPress block is [with](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/) a `block.json` file.

A `block.json` file [must be present](https://github.com/WordPress/wporg-plugin-guidelines/blob/28d945f414db3bb42e04805fb109e7178cbabc9a/blocks.md#4-block-plugins-must-include-a-blockjson-file) to be eligible for the [Block Directory](https://wordpress.org/plugins/browse/block/).

Also, WordPress [recommends](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#benefits-using-the-metadata-file) registering a block with PHP and JS.

Both registrations will be simple because of `block.json`.

## Exercise

You're going to register the block in JS and PHP.

Look for 🚧 in the exercise files for where to edit.

In [js/src/index.exercise.js](js/src/index.exercise.js), `registerBlockType()` will look like:

```js
registerBlockType( block.name, {
// edit component here.
```

### Files
- [progress-indicator.php](progress-indicator.php)
- [js/src/index.exercise.js](js/src/index.exercise.js)

### Solution
https://user-images.githubusercontent.com/4063887/159091534-12a41901-4100-4fc5-8222-fa25413977d7.mp4
62 changes: 5 additions & 57 deletions js/src/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,11 @@ import * as React from 'react';
/**
* WordPress dependencies
*/
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import { ColorPalette, PanelBody, RangeControl } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import ProgressIndicator from './progress-indicator';

/**
* The component for the editor.
*
* @param {{
* attributes: import('./index').Attributes,
* setAttributes: Function
* }} props
* @return {React.ReactElement} The component.
*/
export default function Edit( { attributes, setAttributes } ) {
const blockProps = useBlockProps();
const { colors } = useSelect(
( select ) => select( 'core/block-editor' ).getSettings()
);

return <div { ...blockProps }>
<ProgressIndicator attributes={ attributes } />
<InspectorControls>
<PanelBody title={ __( 'Color', 'progress-indicator' ) }>
<ColorPalette
colors={ colors }
value={ attributes.color }
onChange={ ( newValue ) =>
setAttributes( { color: newValue } )
}
/>
</PanelBody>
<PanelBody title={ __( 'Steps', 'progress-indicator' ) }>
<RangeControl
label={ __( 'Current Step', 'progress-indicator' ) }
value={ attributes.currentStep }
onChange={ ( newValue ) =>
setAttributes( { currentStep: Number( newValue ) } )
}
min={ 1 }
max={ attributes.numberOfSteps }
/>
<RangeControl
label={ __( 'Number of Steps', 'progress-indicator' ) }
value={ attributes.numberOfSteps }
onChange={ ( newValue ) =>
setAttributes( { numberOfSteps: Number( newValue ) } )
}
min={ 1 }
max={ 10 }
/>
</PanelBody>
</InspectorControls>
</div>;
/** The component for the editor. */
export default function Edit() {
return <span>
{ __( 'This is a placeholder for the Progress Indicator block', 'progress-indicator' ) }
</span>;
}
25 changes: 25 additions & 0 deletions js/src/index.exercise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* WordPress dependencies
*/
import { registerBlockType } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import block from '../../block.json';
// 🚧 Import the edit component.
// Import the save component.

/**
* @typedef {{
* color: typeof block.attributes.color.default,
* currentStep: typeof block.attributes.currentStep.default,
* numberOfSteps: typeof block.attributes.numberOfSteps.default
* }} Attributes
*/

// @ts-ignore The declaration file is wrong.
registerBlockType( block.name, {
// 🚧 For property 'edit', pass the edit component imported above.
// For property 'save', pass the save component imported above.
} );
25 changes: 25 additions & 0 deletions js/src/index.final.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* WordPress dependencies
*/
import { registerBlockType } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import block from '../../block.json';
import edit from './edit';
import save from './save';

/**
* @typedef {{
* color: typeof block.attributes.color.default,
* currentStep: typeof block.attributes.currentStep.default,
* numberOfSteps: typeof block.attributes.numberOfSteps.default
* }} Attributes
*/

// @ts-ignore The declaration file is wrong.
registerBlockType( block.name, {
edit,
save,
} );
27 changes: 2 additions & 25 deletions js/src/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,2 @@
/**
* WordPress dependencies
*/
import { registerBlockType } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import block from '../../block.json';
import edit from './edit';
import save from './save';

/**
* @typedef {{
* color: typeof block.attributes.color.default,
* currentStep: typeof block.attributes.currentStep.default,
* numberOfSteps: typeof block.attributes.numberOfSteps.default
* }} Attributes
*/

// @ts-ignore The declaration file is wrong.
registerBlockType( block.name, {
edit,
save,
} );
// export * from './index.final';
export * from './index.exercise';
1 change: 0 additions & 1 deletion js/src/progress-indicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import tinycolor2 from 'tinycolor2';
* The progress indicator component.
*
* @param {{attributes: import('./').Attributes}} props
* @return {React.ReactElement} The component.
*/
export default function ProgressIndicator( { attributes } ) {
const color = tinycolor2( attributes.color );
Expand Down
44 changes: 0 additions & 44 deletions js/src/progress-indicator.test.js

This file was deleted.

18 changes: 5 additions & 13 deletions js/src/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,15 @@ import * as React from 'react';
/**
* WordPress dependencies
*/
import { useBlockProps } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import ProgressIndicator from './progress-indicator';
import { __ } from '@wordpress/i18n';

/**
* The component to save the markup.
*
* @param {{attributes: import('./').Attributes}} props
* @return {React.ReactElement} The component.
*/
export default function Save( { attributes } ) {
const blockProps = useBlockProps.save();

return <div { ...blockProps }>
<ProgressIndicator attributes={ attributes } />
</div>;
export default function Save() {
return <span>
{ __( 'This is a placeholder for the Progress Indicator block', 'progress-indicator' ) }
</span>;
}
2 changes: 1 addition & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<ruleset name="WordPress Coding Standards for Progress Indicator Block">
<ruleset name="WordPress Coding Standards for Progress Indicator">

<rule ref="WordPress-Core">
<exclude name="Generic.Arrays.DisallowShortArraySyntax" />
Expand Down
12 changes: 6 additions & 6 deletions progress-indicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

add_action( 'init', __NAMESPACE__ . '\register_block' );

/**
* Registers the block with the block.json file.
*/
function register_block() {
register_block_type( __DIR__ );
}
// 🚧 Define a function register_block().
// That function should simply call the PHP function to register a block: https://developer.wordpress.org/reference/functions/register_block_type/
// And that function's only argument should be the directory of this plugin.
// You can get that directory from a magic constant: https://www.php.net/manual/en/language.constants.magic.php
// That's where it'll look for the block.json file.
// Do npm run lint:php to see if there's anything else to add.