块编辑器开发文档

NavigateRegions

💡 云策文档标注

概述

navigateRegions 是一个 React 高阶组件,用于为标记为“区域”(role="region")的 DOM 元素添加键盘导航功能,允许用户在不同区域间切换。这些区域需可聚焦(例如通过添加 tabIndex 属性),并应正确标注以描述内容目的,以提升可访问性。

关键要点

  • navigateRegions 是一个 React 高阶组件,用于增强键盘导航。
  • 区域元素需设置 role="region" 和 tabIndex 属性以实现可聚焦。
  • 为提升可访问性,区域元素应使用 aria-label 等属性进行适当标注。
  • role="region" 是 ARIA 地标角色,应仅用于页面的主要部分。

代码示例

import { navigateRegions } from '@wordpress/components';

const MyComponentWithNavigateRegions = navigateRegions( () => (
    <div>
        <div role="region" tabIndex="-1" aria-label="Header">
            Header
        </div>
        <div role="region" tabIndex="-1" aria-label="Content">
            Content
        </div>
        <div role="region" tabIndex="-1" aria-label="Sidebar">
            Sidebar
        </div>
    </div>
) );

注意事项

  • role="region" 是 ARIA 地标角色,应仅用于页面中足够重要的部分,以便在页面摘要中列出。
  • 所有可感知内容应位于语义上有意义的地标中,以避免用户遗漏内容。

📄 原文内容

navigateRegions is a React higher-order component adding keyboard navigation to switch between the different DOM elements marked as “regions” (role=”region”). These regions should be focusable (By adding a tabIndex attribute for example). For better accessibility, these elements must be properly labelled to briefly describe the purpose of the content in the region. For more details, see “Landmark Roles” in the WAI-ARIA specification and “Landmark Regions” in the ARIA Authoring Practices Guide.

Example:

import { navigateRegions } from '@wordpress/components';

const MyComponentWithNavigateRegions = navigateRegions( () => (
    <div>
        <div role="region" tabIndex="-1" aria-label="Header">
            Header
        </div>
        <div role="region" tabIndex="-1" aria-label="Content">
            Content
        </div>
        <div role="region" tabIndex="-1" aria-label="Sidebar">
            Sidebar
        </div>
    </div>
) );

Notes:

It’s important to note that an ARIA role="region" is an ARIA landmark role. It should be reserved for sections of content sufficiently important to have it listed in a summary of the page. Only use this ARIA role for the main sections of a page. All perceivable content should reside in a semantically meaningful landmark in order that content is not missed by the user.