title: "关于交互性 API" post_status: publish comment_status: open taxonomy: category: - gutenberg-docs post_tag: - Interactivity Api - Reference Guides - Repos


关于交互性 API

交互性 API 是一套基于声明式代码的指令标准系统,用于为区块添加前端交互功能

指令通过特殊属性扩展 HTML,告知交互性 API 将指定行为附加到 DOM 元素甚至对其进行转换。对于熟悉 Alpine.js 的用户来说,这是类似的方法,但明确设计为与 WordPress 无缝协作。

API 目标

Interactivity API 的主要目标是为 Gutenberg 区块的前端交互提供一种标准且简单的处理方式

标准化使开发者能够更轻松地创建丰富的交互式用户体验,从简单的计数器或弹窗,到更复杂的功能,如即时页面导航、即时搜索或购物车和结账流程。

目前,即使没有 Interactivity API,所有这些用户体验在技术上都是可以实现的。然而,用户体验越复杂,区块之间的交互越多,开发者构建和维护网站的难度就越大。他们必须自行解决许多挑战。该 API 旨在为支持这类交互提供开箱即用的方法。

为解决这一挑战,Interactivity API 定义了以下要求/目标:

除了所有这些要求之外,在任何解决方案之上集成客户端导航都应该简单且高效。客户端导航是在网站页面之间导航而不重新加载整个页面的过程,这是 Web 开发者最需要的令人印象深刻的用户体验之一。因此,此功能应与这个新系统兼容。

为何选择指令?

指令是经过对不同可能性与实现方案的深入研究后得出的成果。我们发现这一设计能最高效地满足各项需求。

区块优先且 PHP 友好

该 API 专为区块世界设计,并秉承 WordPress 紧密遵循 Web 标准的历史传统。

由于指令是 HTML 属性,它们非常适合动态区块和 PHP。

动态区块示例

<div
  data-wp-interactive='wpmovies'
  <?php echo wp_interactivity_data_wp_context( array( 'isOpen' => false ) ); ?>
  data-wp-watch="callbacks.logIsOpen"
>
  <button
    data-wp-on--click="actions.toggle"
    data-wp-bind--aria-expanded="context.isOpen"
    aria-controls="p-1"
  >
    Toggle
  </button>

  <p id="p-1" data-wp-bind--hidden="!context.isOpen">
    This element is now visible!
  </p>
</div>

如您所见,像 data-wp-on--clickdata-wp-bind--hidden 这样的指令是作为自定义 HTML 属性添加的。WordPress 可以在服务器上处理此 HTML,执行指令逻辑并生成相应的标记。

向后兼容

由于交互性 API 与服务器端渲染完美配合,您可以使用所有 WordPress API,包括:

可选与渐进式采用

Interactivity API 管道基于 WordPress 的坚实基础和模式构建,提倡渐进式增强

例如,包含指令的区块可以与其他(交互式或非交互式)区块共存。这意味着如果页面上有其他使用 jQuery 等框架的区块,所有功能仍将按预期工作。

使用 Interactivity API 进行全页面客户端导航时,与其他库的兼容性规则将存在例外。详见客户端导航部分。

Declarative and reactive

The Interactivity API follows an approach similar to other popular JS frameworks by separating state, actions, and callbacks and defining them declaratively. Why declaratively?

Declarative code describes what a program should do, while imperative code describes how the program should do it. Using a declarative approach, the UI automatically updates in response to changes in the underlying data. With an imperative approach, you must manually update the UI whenever the data changes. Compare the two code examples:

Imperative code

<button id="toggle-button">Toggle Element</button>
<p>This element is now visible!</p>
<script>
  const button = document.getElementById("toggle-button");

  button.addEventListener("click", () => {
    const element = document.getElementById("element");
    if(element) {
      element.remove();
    } else {
      const newElement = document.createElement("p");
      newElement.textContent = "This element is visible";
      document.body.appendChild(newElement);
    }
});
</script>

Declarative code

This is the same use case shared above but serves as an example of declarative code using this new system. The JavaScript logic is defined in the view.js file of the block, and add the directives to the markup in the render.php.

// view.js file

import { store, getContext } from "@wordpress/interactivity";

store( 'wpmovies', {
  actions: {
    toggle: () => {
      const context = getContext();
      context.isOpen = !context.isOpen;
    },
  },
});
<!-- Render.php file -->

<div
  data-wp-interactive='wpmovies'
  <?php echo wp_interactivity_data_wp_context( array( 'isOpen' => true ) ); ?>
>
  <button
    data-wp-on--click="actions.toggle"
    data-wp-bind--aria-expanded="context.isOpen"
    aria-controls="p-1"
  >
    Toggle
  </button>

  <p id="p-1" data-wp-bind--hidden="!context.isOpen">
    This element is now visible!
  </p>
</div>

Using imperative code may be easier when creating simple user experiences, but it becomes much more difficult as applications become more complex. The Interactivity API must cover all use cases, from the simplest to the most challenging. That’s why a declarative approach using directives better fits the Interactivity API.

高性能

该 API 的设计旨在实现最佳性能:

可扩展性

指令可以直接在 HTML 中添加、移除或修改。例如,用户可以使用 render_block 过滤器 来修改 HTML 及其行为。

原子化与可组合性

每个指令控制 DOM 的一小部分,您可以组合多个指令来创建丰富、交互式的用户体验。

兼容现有区块开发工具链

该 API 可与标准区块构建工具(如 wp-scripts)开箱即用。要让 wp-scripts 正确构建使用交互性 API 的脚本模块,唯一要求是在 buildstart 脚本中使用 --experimental-modules 标志。

客户端导航

Interactivity API 内置了为网站添加客户端导航的基础功能。此功能完全可选,但它提供了在不脱离 WordPress 渲染系统的前提下创建此类用户体验的可能性。

请访问客户端导航指南,了解更多关于如何使用 Interactivity Router 并在您的区块中实现客户端导航的信息。

Why a standard?

Blocks using the Interactivity API and interactive blocks using other approaches like jQuery can coexist, and everything will work as expected. However, the Interactivity API comes with some benefits for your interactive blocks:

Additionally, with a standard, WordPress can absorb the maximum amount of complexity from the developer because it will handle most of what’s needed to create an interactive block.

Complexities absorbed by the standard

Two columns table comparing some aspects with and without a standard. Without a standard, block developers have to take care of everything, while having a standard. Totally handled by the standard: Tooling, hydration, integrating it with WordPress, SSR of the interactive parts, inter-block communication, and frontend performance. Partially handled: Security, accessibility, and best practices. Developer responsibility: Block logic. In the without a standard column, everything is under the developer responsibility.

With this absorption, less knowledge is required to create interactive blocks, and developers have fewer decisions to worry about.

By adopting a standard, learning from other interactive blocks is simpler, and fosters collaboration and code reusability. As a result, the development process is leanier and friendlier to less experienced developers.