块编辑器开发文档

使用样式和样式表

💡 云策文档标注

概述

本文档介绍了在 WordPress 块编辑器中应用 CSS 样式的多种方法,包括内联样式和外部样式表,旨在帮助开发者有效管理块的外观和样式。

关键要点

  • 使用 useBlockProps React hook 来设置和应用块的包装元素属性,支持内联样式和自动生成类名。
  • 通过 block.json 文件中的 editorStyle、style 和 viewStyle 属性来排队加载样式表,分别针对编辑器视图、前端或两者。
  • 在开发环境中,需要运行构建步骤(如 npm run build)并更新资产文件以包含依赖项。

代码示例

import { registerBlockType } from '@wordpress/blocks';
import { useBlockProps } from '@wordpress/block-editor';

registerBlockType( 'gutenberg-examples/example-02-stylesheets', {
    edit() {
        const greenBackground = {
            backgroundColor: '#090',
            color: '#fff',
            padding: '20px',
        };

        const blockProps = useBlockProps( { style: greenBackground } );

        return (
            <p { ...blockProps }>Hello World (from the editor, in green).</p>
        );
    },
    save() {
        const redBackground = {
            backgroundColor: '#900',
            color: '#fff',
            padding: '20px',
        };

        const blockProps = useBlockProps.save( { style: redBackground } );

        return (
            <p { ...blockProps }>Hello World (from the frontend, in red).</p>
        );
    },
} );

注意事项

  • 如果使用 @wordpress/scripts,需要在相应的 JavaScript 文件中导入样式表(如 import './editor.scss')以处理样式。
  • 对于多个样式文件,可以使用标准的 wp_enqueue_style 函数,并结合 enqueue_block_editor_assets 和 enqueue_block_assets 钩子来控制在编辑器或前端的加载。

📄 原文内容

Overview

A block typically inserts markup (HTML) into post content that you want to style in some way. This guide walks through a few different ways you can use CSS with the block editor and how to work with styles and stylesheets.

Before you start

You will need a basic block and WordPress development environment to implement the examples shown in this guide. See the Quick Start Guide or block tutorial to get set up.

Methods to add style

The following are different methods you can use to add style to your block, either in the editor or when saved.

Method 1: Inline style

The first method shows adding the style inline. This transforms the defined style into a property on the element inserted.

The useBlockProps React hook is used to set and apply properties on the block’s wrapper element. The following example shows how:

import { registerBlockType } from '@wordpress/blocks';
import { useBlockProps } from '@wordpress/block-editor';

registerBlockType( 'gutenberg-examples/example-02-stylesheets', {
    edit() {
        const greenBackground = {
            backgroundColor: '#090',
            color: '#fff',
            padding: '20px',
        };

        const blockProps = useBlockProps( { style: greenBackground } );

        return (
            <p { ...blockProps }>Hello World (from the editor, in green).</p>
        );
    },
    save() {
        const redBackground = {
            backgroundColor: '#900',
            color: '#fff',
            padding: '20px',
        };

        const blockProps = useBlockProps.save( { style: redBackground } );

        return (
            <p { ...blockProps }>Hello World (from the frontend, in red).</p>
        );
    },
} );

Method 2: Block classname

The inline style works well for a small amount of CSS to apply. If you have much more than the above you will likely find that it is easier to manage with them in a separate stylesheet file.

The useBlockProps hooks includes the classname for the block automatically, it generates a name for each block using the block’s name prefixed with wp-block-, replacing the / namespace separator with a single -.

For example the block name: gutenberg-examples/example-02-stylesheets would get the classname: wp-block-gutenberg-examples-example-02-stylesheets. It might be a bit long but best to avoid conflicts with other blocks.

import { registerBlockType } from '@wordpress/blocks';
import { useBlockProps } from '@wordpress/block-editor';

registerBlockType( 'gutenberg-examples/example-02-stylesheets', {
    edit() {
        const blockProps = useBlockProps();

        return (
            <p { ...blockProps }>Hello World (from the editor, in green).</p>
        );
    },
    save() {
        const blockProps = useBlockProps.save();

        return (
            <p { ...blockProps }>Hello World (from the frontend, in red).</p>
        );
    },
} );

Build or add dependency

In order to include the blockEditor as a dependency, make sure to run the build step, or update the asset php file.

Build the scripts and update the asset file which is used to keep track of dependencies and the build version.

npm run build

Enqueue stylesheets

Like scripts, you can enqueue your block’s styles using the block.json file.

Use the editorStyle property to a CSS file you want to load in the editor view only, use the style property for a CSS file you want to load both in the editor view and on the frontend when the block is used, and use the viewStyle property for a CSS file you want to load only on the frontend when the block is used.

It is worth noting that, if the editor content is iframed, both the style and editorStyle will load in the iframe. editorStyle will also load outside the iframe, so it can be used for editor content as well as UI.

For example:

{
    "apiVersion": 3,
    "name": "gutenberg-examples/example-02-stylesheets",
    "title": "Example: Stylesheets",
    "icon": "universal-access-alt",
    "category": "layout",
    "editorScript": "file:./block.js",
    "editorStyle": "file:./editor.css",
    "style": "file:./style.css"
}

So in your plugin directory, create an editor.css file to load in editor view:

/* green background */
.wp-block-gutenberg-examples-example-02-stylesheets {
    background: #090;
    color: white;
    padding: 20px;
}

And a style.css file to load on the frontend:

/* red background */
.wp-block-gutenberg-examples-example-02-stylesheets {
    background: #900;
    color: white;
    padding: 20px;
}

The files will automatically be enqueued when specified in the block.json.

If you are using `@wordpress/scripts` you will need to import your stylesheet within your corresponding JavaScript file in order for `@wordpress/scripts` to process the stylesheet.

Example:

– In `edit.js` you would place `import ‘./editor.scss’;`
– In `index.js` you would place `import ‘./style.scss’;`
– In `view.js` you would place `import ‘./view.scss’;` (interactive block template)

Note: If you have multiple files to include, you can use standard wp_enqueue_style functions like any other plugin or theme. You will want to use the following hooks for the block editor:

  • enqueue_block_editor_assets – to load only in editor view
  • enqueue_block_assets – loads both on frontend and editor view

Conclusion

This guide showed a couple of different ways to apply styles to your block, by either inline or in its own style sheet. Both of these methods use the useBlockProps hook, see the block wrapper reference documentation for additional details.

See the complete stylesheets-79a4c3 code in the block-development-examples repository.