块编辑器开发文档

💡 云策文档标注

概述

@wordpress/nux 模块提供用于 WordPress 管理员界面新用户引导的组件和 wp.data 方法,主要包括提示(tips)和指南(guides)。提示是解释 UI 元素功能的组件,可被用户关闭或全局禁用;指南允许按顺序展示一系列提示。

关键要点

  • 模块提供 DotTip 组件用于渲染单个提示,通过 tipId 唯一标识
  • 提示状态通过 localStorage 持久化,支持用户关闭或全局禁用
  • 指南功能允许将多个提示分组并按顺序展示,使用 triggerGuide 方法触发
  • 提供 wp.data 方法如 isTipVisible、dismissTip、areTipsEnabled 等用于程序化控制
  • 安装需 npm install @wordpress/nux,并确保 ES2015+ 环境或包含 polyfill

代码示例

// DotTip 组件使用示例
<button onClick={ ... }>
    Add to Cart
    <DotTip tipId="acme/add-to-cart">
        Click here to add the product to your shopping cart.
    </DotTip>
</button>

// 检查提示是否可见
const isVisible = select( 'core/nux' ).isTipVisible( 'acme/add-to-cart' );

// 手动关闭提示
dispatch( 'core/nux' ).dismissTip( 'acme/add-to-cart' );

// 启用或禁用提示
const areTipsEnabled = select( 'core/nux' ).areTipsEnabled();
dispatch( 'core/nux' ).disableTips();

// 触发指南
dispatch( 'core/nux' ).triggerGuide( [
    'acme/product-info',
    'acme/add-to-cart',
    'acme/checkout',
] );

// 获取指南信息
const guide = select( 'core/nux' ).getAssociatedGuide( 'acme/add-to-cart' );

注意事项

  • 提示不能同时属于多个指南
  • 启用提示(enableTips)会恢复所有之前关闭的提示
  • 此包是 Gutenberg 项目的一部分,采用 monorepo 结构,贡献需参考项目指南

📄 原文内容

The NUX module exposes components, and wp.data methods useful for onboarding a new user to the WordPress admin interface. Specifically, it exposes tips and guides.

A tip is a component that points to an element in the UI and contains text that explains the element’s functionality. The user can dismiss a tip, in which case it never shows again. The user can also disable tips entirely. Information about tips is persisted between sessions using localStorage.

A guide allows a series of tips to be presented to the user one by one. When a user dismisses a tip that is in a guide, the next tip in the guide is shown.

Installation

Install the module

npm install @wordpress/nux --save

This package assumes that your code will run in an ES2015+ environment. If you’re using an environment that has limited or no support for such language features and APIs, you should include the polyfill shipped in @wordpress/babel-preset-default in your code.

DotTip

DotTip is a React component that renders a single tip on the screen. The tip will point to the React element that DotTip is nested within. Each tip is uniquely identified by a string passed to tipId.

See the component’s README for more information.

<button onClick={ ... }>
    Add to Cart
    <DotTip tipId="acme/add-to-cart">
        Click here to add the product to your shopping cart.
    </DotTip>
</button>
}

Determining if a tip is visible

You can programmatically determine if a tip is visible using the isTipVisible select method.

const isVisible = select( 'core/nux' ).isTipVisible( 'acme/add-to-cart' );
console.log( isVisible ); // true or false

Manually dismissing a tip

dismissTip is a dispatch method that allows you to programmatically dismiss a tip.

<button
    onClick={ () => {
        dispatch( 'core/nux' ).dismissTip( 'acme/add-to-cart' );
    } }
>
    Dismiss tip
</button>

Disabling and enabling tips

Tips can be programmatically disabled or enabled using the disableTips and enableTips dispatch methods. You can query the current setting by using the areTipsEnabled select method.

Calling enableTips will also un-dismiss all previously dismissed tips.

const areTipsEnabled = select( 'core/nux' ).areTipsEnabled();
return (
    <button
        onClick={ () => {
            if ( areTipsEnabled ) {
                dispatch( 'core/nux' ).disableTips();
            } else {
                dispatch( 'core/nux' ).enableTips();
            }
        } }
    >
        { areTipsEnabled ? 'Disable tips' : 'Enable tips' }
    </button>
);

Triggering a guide

You can group a series of tips into a guide by calling the triggerGuide dispatch method. The given tips will then appear one by one.

A tip cannot be added to more than one guide.

dispatch( 'core/nux' ).triggerGuide( [
    'acme/product-info',
    'acme/add-to-cart',
    'acme/checkout',
] );

Getting information about a guide

getAssociatedGuide is a select method that returns useful information about the state of the guide that a tip is associated with.

const guide = select( 'core/nux' ).getAssociatedGuide( 'acme/add-to-cart' );
console.log( 'Tips in this guide:', guide.tipIds );
console.log( 'Currently showing:', guide.currentTipId );
console.log( 'Next to show:', guide.nextTipId );

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.