-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Update platform docs intro #61341
Merged
Merged
Update platform docs intro #61341
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,7 @@ sidebar_position: 1 | |
|
||
# Getting Started | ||
|
||
Let's discover how to use the **Gutenberg Block Editor** to build your own block editor in less than 10 minutes**. | ||
|
||
Let's discover how to use the **Gutenberg Block Editor** to build your own block editor in less than 10 minutes. | ||
|
||
## What you'll need | ||
|
||
|
@@ -22,7 +21,6 @@ Once done, you can navigate to your application folder and run it locally using | |
|
||
To build a block editor, you need to install the following dependencies: | ||
|
||
- `@wordpress/blocks` | ||
- `@wordpress/block-editor` | ||
- `@wordpress/block-library` | ||
- `@wordpress/components` | ||
|
@@ -33,66 +31,69 @@ We're going to be using JSX to write our UI and components as the block editor i | |
|
||
## Bootstrap your block editor | ||
|
||
It's time to render our first block editor. Update your `src/App.jsx` file with the following code: | ||
It's time to render our first block editor. We’ll do this with changes to three files – `index.html`, `src/main.jsx`, and `src/App.jsx`. | ||
|
||
First, we’ll add the base styles are for the editor UI. In `index.html` add these styles in the `<head>`: | ||
```html | ||
<link href="node_modules/@wordpress/components/build-style/style.css" rel="stylesheet" vite-ignore/> | ||
<link href="node_modules/@wordpress/block-editor/build-style/style.css" rel="stylesheet" vite-ignore/> | ||
``` | ||
:::note | ||
|
||
There are more styles needed but can’t be added here because they are for the editor’s content which is in a separate document within an `<iframe>`. We’ll add those styles via the `BlockCanvas` component in a later step. | ||
|
||
::: | ||
|
||
Next, we’ll add blocks for the editor to work with. In `src/main.jsx` import and call `registerCoreBlocks`: | ||
```js | ||
import { registerCoreBlocks } from '@wordpress/block-library' | ||
registerCoreBlocks(); | ||
``` | ||
|
||
Finally, we’ll put our editor together. In `src/App.jsx` replace the contents with the following code: | ||
|
||
```jsx | ||
import { useState } from "react"; | ||
import { | ||
BlockEditorProvider, | ||
BlockCanvas, | ||
} from "@wordpress/block-editor"; | ||
import { registerCoreBlocks } from "@wordpress/block-library"; | ||
import { getBlockTypes } from "@wordpress/blocks"; | ||
|
||
// Default styles that are needed for the editor. | ||
// Base styles for the content within the block canvas iframe. | ||
import componentsStyles from "@wordpress/components/build-style/style.css?raw"; | ||
import blockEditorStyles from "@wordpress/block-editor/build-style/style.css?raw"; | ||
import blockEditorContentStyles from "@wordpress/block-editor/build-style/content.css?raw"; | ||
|
||
// Default styles that are needed for the core blocks. | ||
import blocksCommonStyles from "@wordpress/block-library/build-style/common.css?raw"; | ||
import blocksStyles from "@wordpress/block-library/build-style/style.css?raw"; | ||
import blocksEditorStyles from "@wordpress/block-library/build-style/editor.css?raw"; | ||
|
||
const styles = ` | ||
${ componentsStyles } | ||
${ blockEditorStyles } | ||
${ blockEditorContentStyles } | ||
${ blocksCommonStyles } | ||
${ blocksStyles } | ||
${ blocksEditorStyles } | ||
`; | ||
|
||
const registeredBlockTypes = getBlockTypes(); | ||
// Registers the default core block types, avoiding doing so again during HMR. | ||
if ( | ||
! registeredBlockTypes.length || | ||
! registeredBlockTypes.some((blockType) => blockType.name.startsWith('core/')) | ||
) registerCoreBlocks(); | ||
|
||
export default function Editor() { | ||
const [blocks, setBlocks] = useState([]); | ||
const [ blocks, setBlocks ] = useState( [] ); | ||
return ( | ||
<> | ||
<style>{ styles }</style> | ||
{/* | ||
The BlockEditorProvider is the wrapper of the block editor's state. | ||
All the UI elements of the block editor need to be rendered within this provider. | ||
*/} | ||
<BlockEditorProvider | ||
value={blocks} | ||
onChange={setBlocks} | ||
onInput={setBlocks} | ||
> | ||
{/* | ||
The BlockCanvas component renders the block list within an iframe | ||
and wires up all the necessary events to make the block editor work. | ||
*/} | ||
<BlockCanvas height="500px" styles={ [ { css: styles } ] }/> | ||
</BlockEditorProvider> | ||
</> | ||
/* | ||
The BlockEditorProvider is the wrapper of the block editor's state. | ||
All the UI elements of the block editor need to be rendered within this provider. | ||
*/ | ||
<BlockEditorProvider | ||
value={ blocks } | ||
onChange={ setBlocks } | ||
onInput={ setBlocks } | ||
> | ||
{ /* | ||
The BlockCanvas component renders the block list within an iframe | ||
and wires up all the necessary events to make the block editor work. | ||
*/ } | ||
<BlockCanvas | ||
height="500px" | ||
styles={ [ | ||
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 you think there's any performance value in putting a stable array here (maybe define this array outside the component) |
||
{ css: componentsStyles }, | ||
{ css: blockEditorContentStyles }, | ||
{ css: blocksStyles }, | ||
{ css: blocksEditorStyles }, | ||
] } | ||
/> | ||
</BlockEditorProvider> | ||
); | ||
} | ||
|
||
``` | ||
|
||
That's it! You now have a very basic block editor with several block types included by default: paragraphs, headings, lists, quotes, images... |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I realized this one was extra, it is
@import
‘d in the block library’s style (the one on the next line).