accessibility
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAccessibility in UI5 Web Components
UI5 Web Components中的无障碍访问
UI5 Web Components have built-in accessibility: ARIA roles, keyboard navigation, screen reader support, and high contrast themes are handled automatically in the shadow DOM. Applications should use the accessibility APIs described here to provide additional context that only the app can know (labels, descriptions, relationships).
UI5 Web Components内置无障碍访问能力:ARIA角色、键盘导航、屏幕阅读器支持以及高对比度主题会在shadow DOM中自动处理。应用应使用本文所述的无障碍API来提供只有应用本身才能知晓的额外上下文信息(标签、描述、关联关系)。
Built-in Accessibility (No App Code Needed)
内置无障碍能力(无需编写应用代码)
Components automatically provide:
- ARIA roles and attributes mapped in the shadow DOM (e.g., renders
ui5-comboboxinternally)role="combobox" - Keyboard navigation within complex components (arrow keys in lists, tables, date pickers, etc.)
- Focus management with visible focus indicators
- State mapping — ,
disabled,readonly,requiredare automatically mapped to their ARIA equivalents (checked,aria-disabled,aria-readonly,aria-required)aria-checked
组件会自动提供:
- ARIA角色与属性:在shadow DOM中映射(例如,内部会渲染
ui5-combobox)role="combobox" - 键盘导航:在复杂组件内支持(列表、表格、日期选择器等中的箭头键操作)
- 焦点管理:带有可见的焦点指示器
- 状态映射:、
disabled、readonly、required会自动映射为对应的ARIA等效属性(checked、aria-disabled、aria-readonly、aria-required)aria-checked
Accessibility APIs
无障碍API
Use these properties to enrich the accessibility context for your application.
使用以下属性丰富应用的无障碍上下文信息。
accessibleName
accessibleName
Maps to . Provides a text alternative when the visual label is insufficient or absent.
aria-labelhtml
<ui5-button icon="sap-icon://edit" accessible-name="Edit document"></ui5-button>
<ui5-combobox accessible-name="Select country">
<ui5-cb-item text="Germany"></ui5-cb-item>
</ui5-combobox>Use when there is no visible text label, for example icon-only buttons or inputs without a .
accessibleName<ui5-label>映射至。当视觉标签不足或不存在时,提供文本替代方案。
aria-labelhtml
<ui5-button icon="sap-icon://edit" accessible-name="Edit document"></ui5-button>
<ui5-combobox accessible-name="Select country">
<ui5-cb-item text="Germany"></ui5-cb-item>
</ui5-combobox>当没有可见文本标签时使用,例如纯图标按钮或没有的输入框。
accessibleName<ui5-label>accessibleNameRef
accessibleNameRef
Alternative to that works across shadow DOM boundaries. Takes one or more IDs of elements whose text content serves as the label.
aria-labelledbyhtml
<ui5-label id="lblName">Full name</ui5-label>
<ui5-input accessible-name-ref="lblName"></ui5-input>
<!-- Multiple refs -->
<span id="prefix">Shipping</span>
<ui5-label id="lblAddr">Address</ui5-label>
<ui5-input accessible-name-ref="prefix lblAddr"></ui5-input>Prefer over when a visible label exists — this keeps the label and the ARIA text in sync automatically.
accessibleNameRefaccessibleName是的替代方案,可跨shadow DOM边界工作。接收一个或多个元素的ID,这些元素的文本内容将作为标签。
aria-labelledbyhtml
<ui5-label id="lblName">Full name</ui5-label>
<ui5-input accessible-name-ref="lblName"></ui5-input>
<!-- 多个引用 -->
<span id="prefix">Shipping</span>
<ui5-label id="lblAddr">Address</ui5-label>
<ui5-input accessible-name-ref="prefix lblAddr"></ui5-input>当存在可见标签时,优先使用而非——这能让标签与ARIA文本自动保持同步。
accessibleNameRefaccessibleNameaccessibleDescription / accessibleDescriptionRef
accessibleDescription / accessibleDescriptionRef
Maps to . Provides additional descriptive text beyond the label.
aria-descriptionhtml
<ui5-input accessible-name="Password"
accessible-description="Must be at least 8 characters">
</ui5-input>
<!-- Or reference an existing element -->
<p id="hint">Must be at least 8 characters</p>
<ui5-input accessible-name="Password"
accessible-description-ref="hint">
</ui5-input>映射至。提供超出标签范围的额外描述文本。
aria-descriptionhtml
<ui5-input accessible-name="Password"
accessible-description="Must be at least 8 characters">
</ui5-input>
<!-- 或引用现有元素 -->
<p id="hint">Must be at least 8 characters</p>
<ui5-input accessible-name="Password"
accessible-description-ref="hint">
</ui5-input>accessibleRole
accessibleRole
Overrides the default ARIA role of a component.
html
<ui5-panel accessible-role="Complementary">...</ui5-panel>
<ui5-list accessible-role="Menu">...</ui5-list>
<ui5-button accessible-role="Link">Navigate</ui5-button>Only change the role when the component is used in a non-standard way (e.g., a List used as a menu).
覆盖组件的默认ARIA角色。
html
<ui5-panel accessible-role="Complementary">...</ui5-panel>
<ui5-list accessible-role="Menu">...</ui5-list>
<ui5-button accessible-role="Link">Navigate</ui5-button>仅当组件以非标准方式使用时才更改角色(例如,将List用作菜单)。
accessibilityAttributes
accessibilityAttributes
An object that sets additional ARIA attributes on the component's root element. Use for , , , and similar relationship attributes.
aria-expandedaria-haspopuparia-controlshtml
<ui5-button id="menuBtn">Open Menu</ui5-button>
<ui5-menu id="menu">...</ui5-menu>
<script>
document.getElementById("menuBtn").accessibilityAttributes = {
expanded: false,
hasPopup: "menu",
controls: "menu"
};
</script>For composite components like , the object contains nested objects for different internal elements — check the component's API docs.
ui5-shellbar一个对象,用于在组件的根元素上设置额外的ARIA属性。适用于、、等关联属性。
aria-expandedaria-haspopuparia-controlshtml
<ui5-button id="menuBtn">Open Menu</ui5-button>
<ui5-menu id="menu">...</ui5-menu>
<script>
document.getElementById("menuBtn").accessibilityAttributes = {
expanded: false,
hasPopup: "menu",
controls: "menu"
};
</script>对于等复合组件,该对象包含针对不同内部元素的嵌套对象——请查看组件的API文档。
ui5-shellbarLabel-Input Relationships
标签与输入框的关联
Due to shadow DOM, standard HTML does not work with custom elements. Use these patterns instead:
<label for="...">Using Label's property (preferred for forms):
forhtml
<ui5-label for="nameInput" required show-colon>Name</ui5-label>
<ui5-input id="nameInput" required></ui5-input>The property connects the label to the input. Clicking the label focuses the input. Setting on the label shows an asterisk; setting on the input sets .
forrequiredrequiredaria-requiredUsing :
accessibleNameRefhtml
<ui5-label id="dateLabel">Date of birth</ui5-label>
<ui5-date-picker accessible-name-ref="dateLabel"></ui5-date-picker>由于shadow DOM的存在,标准HTML的无法与自定义元素配合使用。请改用以下模式:
<label for="...">使用Label的属性(表单优先选择):
forhtml
<ui5-label for="nameInput" required show-colon>Name</ui5-label>
<ui5-input id="nameInput" required></ui5-input>forrequiredrequiredaria-required使用:
accessibleNameRefhtml
<ui5-label id="dateLabel">Date of birth</ui5-label>
<ui5-date-picker accessible-name-ref="dateLabel"></ui5-date-picker>Headings and Semantic Levels
标题与语义层级
Use the property on and on to set the correct heading level for the document outline.
levelui5-titleheader-levelui5-panelhtml
<ui5-title level="H1">Page Title</ui5-title>
<ui5-panel header-text="Settings" header-level="H2">
<ui5-title level="H3">General</ui5-title>
</ui5-panel>Screen readers use heading levels to build a page outline. Skipping levels (e.g., H1 to H4) is an accessibility violation.
在上使用属性,在上使用属性,为文档大纲设置正确的标题层级。
ui5-titlelevelui5-panelheader-levelhtml
<ui5-title level="H1">Page Title</ui5-title>
<ui5-panel header-text="Settings" header-level="H2">
<ui5-title level="H3">General</ui5-title>
</ui5-panel>屏幕阅读器会使用标题层级构建页面大纲。跳过层级(例如从H1直接到H4)属于无障碍访问违规。
Icon Accessibility
图标无障碍访问
The component has a property that controls its accessible behavior:
ui5-iconmode| Mode | Behavior | Use when |
|---|---|---|
| | Icon conveys meaning (needs |
| | Icon is clickable |
| | Icon is purely visual, no meaning |
html
<!-- Meaningful icon — needs accessible-name -->
<ui5-icon name="sap-icon://warning" mode="Image"
accessible-name="Warning"></ui5-icon>
<!-- Clickable icon -->
<ui5-icon name="sap-icon://delete" mode="Interactive"
accessible-name="Delete item"></ui5-icon>
<!-- Decorative icon — hidden from screen readers -->
<ui5-icon name="sap-icon://favorite" mode="Decorative"></ui5-icon>Always set on icons in or mode.
accessible-nameImageInteractiveui5-iconmode| 模式 | 行为 | 适用场景 |
|---|---|---|
| | 图标传递含义(需要 |
| | 图标可点击 |
| | 图标仅为视觉装饰,无实际含义 |
html
<!-- 有含义的图标 —— 需要设置accessible-name -->
<ui5-icon name="sap-icon://warning" mode="Image"
accessible-name="Warning"></ui5-icon>
<!-- 可点击的图标 -->
<ui5-icon name="sap-icon://delete" mode="Interactive"
accessible-name="Delete item"></ui5-icon>
<!-- 装饰性图标 —— 对屏幕阅读器隐藏 -->
<ui5-icon name="sap-icon://favorite" mode="Decorative"></ui5-icon>对于处于或模式的图标,务必设置。
ImageInteractiveaccessible-nameInvisible Messaging
隐形消息
Use to announce dynamic content changes to screen readers (e.g., search results count, form validation, live updates).
InvisibleMessagejavascript
import announce from "@ui5/webcomponents-base/dist/util/InvisibleMessage.js";
import InvisibleMessageMode from "@ui5/webcomponents-base/dist/types/InvisibleMessageMode.js";
// Polite: announced at the next pause (e.g., search results)
announce("5 results found", InvisibleMessageMode.Polite);
// Assertive: announced immediately (e.g., errors)
announce("Invalid email address", InvisibleMessageMode.Assertive);- Use Polite for non-urgent updates (search results, status changes)
- Use Assertive for urgent notifications (errors, warnings)
使用向屏幕阅读器播报动态内容变化(例如搜索结果数量、表单验证、实时更新)。
InvisibleMessagejavascript
import announce from "@ui5/webcomponents-base/dist/util/InvisibleMessage.js";
import InvisibleMessageMode from "@ui5/webcomponents-base/dist/types/InvisibleMessageMode.js";
// Polite:在下次停顿时分播报(例如搜索结果)
announce("5 results found", InvisibleMessageMode.Polite);
// Assertive:立即播报(例如错误信息)
announce("Invalid email address", InvisibleMessageMode.Assertive);- 对于非紧急更新(搜索结果、状态变化),使用Polite模式
- 对于紧急通知(错误、警告),使用Assertive模式
Tooltip
工具提示
Use the property instead of the native attribute.
tooltiptitlehtml
<ui5-button icon="sap-icon://edit" tooltip="Edit document"></ui5-button>Do not set directly on custom elements — this can cause repetitive speech output or incorrect accessibility tree mapping.
title使用属性替代原生的属性。
tooltiptitlehtml
<ui5-button icon="sap-icon://edit" tooltip="Edit document"></ui5-button>请勿直接在自定义元素上设置——这可能导致重复语音输出或无障碍树映射错误。
titleHigh Contrast and Theming
高对比度与主题
UI5 Web Components ship with high contrast themes:
| Theme | Use case |
|---|---|
| Default light theme |
| Dark theme |
| High Contrast Black |
| High Contrast White |
Detect OS preferences and apply the appropriate theme:
javascript
import { setTheme } from "@ui5/webcomponents-base/dist/config/Theme.js";
const darkMode = window.matchMedia("(prefers-color-scheme: dark)");
const highContrast = window.matchMedia("(prefers-contrast: more)");
function applyTheme() {
if (highContrast.matches) {
setTheme(darkMode.matches ? "sap_horizon_hcb" : "sap_horizon_hcw");
} else {
setTheme(darkMode.matches ? "sap_horizon_dark" : "sap_horizon");
}
}
darkMode.onchange = applyTheme;
highContrast.onchange = applyTheme;
applyTheme();UI5 Web Components附带高对比度主题:
| 主题 | 适用场景 |
|---|---|
| 默认浅色主题 |
| 深色主题 |
| 高对比度黑色主题 |
| 高对比度白色主题 |
检测系统偏好并应用相应主题:
javascript
import { setTheme } from "@ui5/webcomponents-base/dist/config/Theme.js";
const darkMode = window.matchMedia("(prefers-color-scheme: dark)");
const highContrast = window.matchMedia("(prefers-contrast: more)");
function applyTheme() {
if (highContrast.matches) {
setTheme(darkMode.matches ? "sap_horizon_hcb" : "sap_horizon_hcw");
} else {
setTheme(darkMode.matches ? "sap_horizon_dark" : "sap_horizon");
}
}
darkMode.onchange = applyTheme;
highContrast.onchange = applyTheme;
applyTheme();Quick Reference
快速参考
| Goal | Property / API |
|---|---|
| Label a component without visible text | |
| Link a visible label to a component | |
| Add a description | |
| Change ARIA role | |
| Set expanded/haspopup/controls | |
| Set heading level | |
| Make icon accessible | |
| Hide decorative icon | |
| Announce dynamic changes | |
| Set tooltip | |
| Respond to OS contrast settings | Use |
| 目标 | 属性/API |
|---|---|
| 为无可见文本的组件设置标签 | |
| 将可见标签与组件关联 | |
| 添加描述 | |
| 修改ARIA角色 | |
| 设置expanded/haspopup/controls | |
| 设置标题层级 | 在Title上使用 |
| 让图标具备无障碍访问能力 | |
| 隐藏装饰性图标 | |
| 播报动态变化 | |
| 设置工具提示 | |
| 响应系统对比度设置 | 使用 |