本文档提供了设计新区块的最佳实践,包括界面布局、交互原则和示例说明,旨在帮助开发者创建直观易用的区块。
// 添加区块描述示例
registerBlockType('example/block', {
description: '创建项目符号或编号列表。',
// 其他属性...
}); The following are best practices for designing a new block, with recommendations and detailed descriptions of existing blocks to illustrate our approach to creating blocks.
Since the block itself represents what will actually appear on the site, interaction here hews closest to the principle of direct manipulation and will be most intuitive to the user. This should be thought of as the primary interface for adding and manipulating content and adjusting how it is displayed. There are two ways of interacting here:
Basic block settings won’t always make sense in the context of the placeholder/content UI. As a secondary option, options that are critical to the functionality of a block can live in the block toolbar. The Block Toolbar is still highly contextual and visible on all screen sizes. One notable constraint with the Block Toolbar is that it is icon-based UI, so any controls that live in the Block Toolbar need to be ones that can effectively be communicated via an icon or icon group.
The Block Toolbar groups controls in segments, hierarchically. The first segment contains block type controls, such as the block switcher, the drag handle, and the mover control. The second group contains common and specific block tools that affect the entire block, followed by inline formatting, and the “More” menu. Optionally “Meta” or “Other” groups can separate some tools in their own segment.

The Settings Sidebar is not visible by default on a small / mobile screen, and may also be collapsed in a desktop view. Therefore, it should not be relied on for anything that is necessary for the basic operation of the block. Pick good defaults, make important actions available in the block toolbar, and think of the Settings Sidebar as something that most users should not need to open.
In addition, use sections and headers in the Settings Sidebar if there are more than a handful of options, in order to allow users to easily scan and understand the options available.
Each Settings Sidebar comes with an “Advanced” section by default. This area houses an “Additional CSS Class” field, and should be used to house other power user controls.
Setup states, sometimes referred to as “placeholders”, can be used to walk users through an initial process before showing the live preview state of the block. The setup process gathers information from the user that is needed to render the block. A block’s setup state is indicated with a grey background to provide clear differentiation for the user. Not all blocks have setup states — for example, the Paragraph block.

A setup state is not necessary if:
Use a setup state if:
For blocks that do have setup states, once the user has gone through the setup process, the placeholder is replaced with the live preview state of that block.

When the block is selected, additional controls may be revealed to customize the block’s contents. For example, when the image gallery is selected, it reveals controls to remove or add images.

In most cases, a block’s setup state is only shown once and then further customization is done via the live preview state. However, in some cases it might be desirable to allow the user to return to the setup state — for example, if all the block content has been deleted or via a link from the block’s toolbar or sidebar.
Group toolbar controls in logical segments. Don’t add a segment for each.

A block should have a straightforward, short name so users can easily find it in the block library. A block named “YouTube” is easy to find and understand. The same block, named “Embedded Video (YouTube)”, would be less clear and harder to find in the block library.
When referring to a block in documentation or UI, use title case for the block title and lowercase for the “block” descriptor. For example:
Blocks should have an identifying icon, ideally using a single color. Try to avoid using the same icon used by an existing block. The core block icons are based on Material Design Icons. Look to that icon set, or to Dashicons for style inspiration.

Do:
Use concise block names.

Don’t:
Avoid long, multi-line block names.
Every block should include a description that clearly explains the block’s function. The description will display in the Settings Sidebar.
You can add a description by using the description attribute in the registerBlockType function.
Stick to a single imperative sentence with an action + subject format. Examples:

Do:
Use a short, simple block description.

Don’t:
Avoid long descriptions and branding.
If your block requires a user to configure some options before you can display it, you should provide an instructive placeholder state.

Do:
Provide an instructive placeholder state.

Don’t:
Avoid branding and relying on the title alone to convey instructions.
When unselected, your block should preview its content as closely to the front-end output as possible.
When selected, your block may surface additional options like input fields or buttons to configure the block directly, especially when they are necessary for basic operation.

Do:
For controls that are essential for the operation of the block, provide them directly inside the block edit view.

Don’t:
Do not put controls that are essential to the block in the sidebar, otherwise the block will appear non-functional to mobile users or desktop users who have dismissed the sidebar.
The “Block” tab of the Settings Sidebar can contain additional block options and configuration. Keep in mind that a user can dismiss the sidebar and never use it. You should not put critical options in the Sidebar.

Do:
Because the Drop Cap feature is not necessary for the basic operation of the block, you can put it to the Block tab as optional configuration.
Check how your block looks, feels, and works on as many devices and screen sizes as you can.
Check how your block looks with dark backgrounds in the editor.
To demonstrate some of these practices, here are a few annotated examples of default Gutenberg blocks:
The most basic unit of the editor. The Paragraph block is a simple input field.

Basic image block.


Future improvements to the Image block could include getting rid of the media modal in place of letting users select images directly from the placeholder itself. In general, try to avoid modals.

Has no placeholder as it works immediately upon insertion. The default inserted state shows the last 5 posts.
Note that the Block Toolbar does not include the Block Chip in this case, since there are no similar blocks to switch to.
Latest Posts is fully functional as soon as it’s inserted because it comes with good defaults.