块编辑器开发文档

KeyboardShortcuts

💡 云策文档标注

概述

是一个 React 组件,用于在渲染元素生命周期内处理键盘序列。它基于 Mousetrap 库实现键盘绑定,可捕获子元素或文档上的按键事件。

关键要点

  • 组件通过 shortcuts prop 对象定义键盘组合键与回调函数的绑定。
  • 支持 children prop 来指定事件监听的目标元素,若无 children 则监听整个文档。
  • 提供 bindGlobal prop 以控制是否在可编辑字段中全局监听按键事件。
  • 使用 eventName prop 可自定义触发回调的键盘事件类型,默认为 keydown。

代码示例

import { useState } from 'react';
import { KeyboardShortcuts } from '@wordpress/components';

const MyKeyboardShortcuts = () => {
    const [ isAllSelected, setIsAllSelected ] = useState( false );
    const selectAll = () => {
        setIsAllSelected( true );
    };

    return (
        <div>
            <KeyboardShortcuts
                shortcuts={ {
                    'mod+a': selectAll,
                } }
            />
            [cmd/ctrl + A] Combination pressed? { isAllSelected ? 'Yes' : 'No' }
        </div>
    );
};

注意事项

  • shortcuts 对象中的回调函数应使用一致的函数引用,避免匿名函数,以确保组件卸载时正确解绑。
  • KeyboardShortcuts 组件不会根据 shortcuts prop 的变化更新,如需更改快捷键,需通过唯一 key prop 挂载新组件。
  • 若需部分快捷键全局监听,可渲染两个独立的 KeyboardShortcuts 元素,分别设置 bindGlobal prop。

📄 原文内容

<KeyboardShortcuts /> is a component which handles keyboard sequences during the lifetime of the rendering element.

When passed children, it will capture key events which occur on or within the children. If no children are passed, events are captured on the document.

It uses the Mousetrap library to implement keyboard sequence bindings.

Example

Render <KeyboardShortcuts /> with a shortcuts prop object:

import { useState } from 'react';
import { KeyboardShortcuts } from '@wordpress/components';

const MyKeyboardShortcuts = () => {
    const [ isAllSelected, setIsAllSelected ] = useState( false );
    const selectAll = () => {
        setIsAllSelected( true );
    };

    return (
        <div>
            <KeyboardShortcuts
                shortcuts={ {
                    'mod+a': selectAll,
                } }
            />
            [cmd/ctrl + A] Combination pressed? { isAllSelected ? 'Yes' : 'No' }
        </div>
    );
};

Props

The component accepts the following props:

children

Elements to render, upon whom key events are to be monitored.

  • Type: ReactNode
  • Required: No

shortcuts

An object of shortcut bindings, where each key is a keyboard combination, the value of which is the callback to be invoked when the key combination is pressed.

  • Type: Object
  • Required: Yes

Note: The value of each shortcut should be a consistent function reference, not an anonymous function. Otherwise, the callback will not be correctly unbound when the component unmounts.

Note: The KeyboardShortcuts component will not update to reflect a changed shortcuts prop. If you need to change shortcuts, mount a separate KeyboardShortcuts element, which can be achieved by assigning a unique key prop.

bindGlobal

By default, a callback will not be invoked if the key combination occurs in an editable field. Pass bindGlobal as true if the key events should be observed globally, including within editable fields.

  • Type: Boolean
  • Required: No

Tip: If you need some but not all keyboard events to be observed globally, simply render two distinct KeyboardShortcuts elements, one with and one without the bindGlobal prop.

eventName

By default, a callback is invoked in response to the keydown event. To override this, pass eventName with the name of a specific keyboard event.

  • Type: String
  • Required: No

References