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 4 for the Save component #17

Draft
wants to merge 24 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b7e9dd2
Add Exercise 4 for the Save Component
kienstra Jan 28, 2022
8c25170
Add to the Save component INSTRUCTIONS.md
kienstra Jan 28, 2022
7daaade
Add dots inside code example
kienstra Jan 28, 2022
b99c4e2
Correct a typo
kienstra Jan 28, 2022
df7a335
Change spaces to tabs
kienstra Jan 28, 2022
b9dd32d
Remove ... as they're confusing
kienstra Jan 28, 2022
c5b57d8
Clarify the Exercise section
kienstra Jan 28, 2022
601f7ac
Make the File heading singular
kienstra Jan 28, 2022
328daaf
Merge branch 'main' into exercise/4-save-component
kienstra Jan 28, 2022
0426fbb
Remove the (es), and add 'sometimes'
kienstra Jan 29, 2022
a3e6e3c
Change the order of sentences
kienstra Jan 29, 2022
268ad09
Remove comment about possible brackets
kienstra Jan 29, 2022
03dd7f0
Change to 'Declare a const here'
kienstra Jan 29, 2022
17948e5
Simplify the ... instructions
kienstra Jan 29, 2022
6e8e67f
Add an '
kienstra Jan 29, 2022
fd9eacb
Add an example to the INSTRUCTIONS.md
kienstra Feb 2, 2022
71076f8
Merge branch 'main' into exercise/4-save-component
kienstra Feb 2, 2022
2c75323
Remove a needless line
kienstra Feb 2, 2022
a18e3b2
Add a .gif of the unexpected content notice
kienstra Feb 2, 2022
8f95f2d
Merge branch 'main' into exercise/4-save-component
kienstra Feb 4, 2022
e0fb2be
Make comments singular
kienstra Feb 4, 2022
39080ba
Merge branch 'main' into exercise/4-save-component
kienstra Feb 10, 2022
11e75d1
Merge branch 'main' into exercise/4-save-component
kienstra Feb 10, 2022
d96a7bc
Add a link to the solution video
kienstra Feb 10, 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
51 changes: 51 additions & 0 deletions INSTRUCTIONS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Save Component

This React [component](https://github.com/WordPress/gutenberg/blob/57da3c91a166d917a2a9de98177be9c3dfe07ee5/docs/reference-guides/block-api/block-edit-save.md#save) determines what's saved to the database.

Its only job is to return markup.

The returned markup is saved to the database as HTML, with attributes stored in a comment:

```html
<!-- wp:progress-indicator/progress-indicator {"color":"#ffe2c7","currentStep":3,"numberOfSteps":7} -->
<div class="wp-block-progress-indicator-progress-indicator">
```

And that saved markup is what displays on the front-end.

Unlike the `Edit` component, this component isn't interactive.

So you won't see `useState()` or REST API calls in `Save`.

The wrapping element of `Save` [should have](https://github.com/WordPress/gutenberg/blob/57da3c91a166d917a2a9de98177be9c3dfe07ee5/docs/reference-guides/block-api/block-edit-save.md#block-wrapper-props-1) `useBlockProps.save()` spread into it:
```jsx
export default function Save( { attributes } ) {
const blockProps = useBlockProps.save();

return <div { ...blockProps }>
```

This adds classes, and sometimes attributes from the [Block Supports](https://github.com/WordPress/gutenberg/blob/57da3c91a166d917a2a9de98177be9c3dfe07ee5/docs/reference-guides/block-api/block-supports.md#supports) API:
```html
<div class="wp-block-progress-indicator-progress-indicator">
```

## Exercise
You'll see a lot of [block validation](https://github.com/WordPress/gutenberg/blob/57da3c91a166d917a2a9de98177be9c3dfe07ee5/docs/reference-guides/block-api/block-edit-save.md#validation) notices like:

>This block contains unexpected or invalid content.

This is normal when editing the `Save` component.

Click 'Attempt Block Recovery' and continue developing:

![unexpected-invalid-content](https://user-images.githubusercontent.com/4063887/152257716-0477a0dc-d666-4249-a5e4-65c96a1e7817.gif)

Once you finish this `Save` component, this block will be done.

It'll display on the front-end.

### File
- [js/src/save.exercise.js](js/src/save.exercise.js)

[Solution video](https://bit.ly/34tiHr2)
29 changes: 29 additions & 0 deletions js/src/save.exercise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* External dependencies
*/
import * as React from 'react';

/**
* WordPress dependencies
*/
// @ts-ignore The declaration file is outdated.
// 🚧 Import useBlockProps from '@wordpress/block-editor'

/**
* Internal dependencies
*/
// 🚧 Import ProgressIndicator

/**
* The component to save the markup.
*
* @param {{attributes: import('./index').Attributes}} props
* @return {React.ReactElement} The component.
*/
export default function Save( { attributes } ) {
// 🚧 Declare a const here and give it the value of useBlockProps.save()

return <div { /* 🚧 Spread the const you declared above, using the ... operator before the const */ }>
{ /* 🚧 Render ProgressIndicator, and pass it the prop it expects */ }
</div>;
}
29 changes: 29 additions & 0 deletions js/src/save.final.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* External dependencies
*/
import * as React from 'react';

/**
* WordPress dependencies
*/
// @ts-ignore The declaration file is outdated.
import { useBlockProps } from '@wordpress/block-editor';

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

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

return <div { ...blockProps }>
<ProgressIndicator attributes={ attributes } />
</div>;
}
28 changes: 3 additions & 25 deletions js/src/save.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
/**
* External dependencies
*/
import * as React from 'react';

/**
* WordPress dependencies
*/
// @ts-ignore The declaration file is outdated.
import { useBlockProps } from '@wordpress/block-editor';

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

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

return <div { ...blockProps }>
<ProgressIndicator attributes={ attributes } />
</div>;
}
export default Save;