块编辑器开发文档

@wordpress/create-block-interactive-template

💡 云策文档标注

概述

本文档介绍了 @wordpress/create-block-interactive-template,这是一个用于创建交互式块的模板,通过 @wordpress/create-block 工具使用。它支持多个变体,适用于 WordPress 6.5 或 Gutenberg 17.7 及以上版本。

关键要点

  • 使用命令 npx @wordpress/create-block --template @wordpress/create-block-interactive-template 来创建交互式块。
  • 模板提供三个变体:default(标准交互块,使用 Interactivity API)、typescript(TypeScript 版本)和 client-side-navigation(客户端导航示例)。
  • client-side-navigation 变体添加了 @wordpress/interactivity-router 作为额外依赖。
  • 此包是 Gutenberg 项目的一部分,采用 monorepo 结构,贡献者需参考项目的主要贡献指南。

📄 原文内容

This is a template for @wordpress/create-block to create interactive blocks.

Usage

This block template can be used by running the following command:

npx @wordpress/create-block --template @wordpress/create-block-interactive-template

It requires at least WordPress 6.5 or Gutenberg 17.7.

Variants

This template exposes three variants. Use the --variant flag to select one:

default

The standard interactive block scaffold. Uses the Interactivity API to demonstrate reactive state, context, and DOM event handling.

npx @wordpress/create-block --template @wordpress/create-block-interactive-template --variant default

This is also the variant used when no --variant flag is provided.

typescript

Same as default but the view script (view.ts) is written in TypeScript, with fully typed store state and context.

npx @wordpress/create-block --template @wordpress/create-block-interactive-template --variant typescript

client-side-navigation

Demonstrates client-side navigation powered by @wordpress/interactivity-router. The block renders a quote navigator whose Prev / Next links swap page content without a full reload. A client-side stopwatch running outside the router region proves that no full page reload occurred.

npx @wordpress/create-block --template @wordpress/create-block-interactive-template --variant client-side-navigation

This variant adds @wordpress/interactivity-router as an additional npm dependency.

Contributing to this package

This is an individual package that’s part of the Gutenberg project. The project is organized as a monorepo. It’s made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to npm and used by WordPress as well as other software projects.

To find out more about contributing to this package or Gutenberg as a whole, please read the project’s main contributor guide.