简体   繁体   English

在不使用 javascript 的情况下自定义下拉菜单中的键盘导航

[英]Keyboard navigation in custom drop down without using javascript

I have an angular app in which there is a component that has div style as button and clicking it open or close another list of div.我有一个 angular 应用程序,其中有一个组件具有 div 样式作为按钮并单击它打开或关闭另一个 div 列表。

I need to make this app accessible.我需要让这个应用程序易于访问。 I can handle enter with angular (keyup.enter) event, when list of divs is opened, I want to move focus to first item in that list.我可以使用 angular (keyup.enter) 事件处理输入,当打开 div 列表时,我想将焦点移动到该列表中的第一项。 Currently I need to press tab to move focus when list is opened.目前我需要在打开列表时按 Tab 键移动焦点。

Is there any way to move focus automatically with using ARIA-roles.有什么方法可以使用 ARIA 角色自动移动焦点。 I don not want to write javascript to handle key down/up and focus shifting code.我不想编写 javascript 来处理按键向下/向上和焦点转移代码。

Also is it ok to use angular (keyup.enter) for both windows and mac machines event instead of writing keydown handler in angualar typescript and matching key code.也可以对 windows 和 mac 机器事件使用 angular (keyup.enter) 而不是在 angualar typescript 中编写 keydown 处理程序并匹配键码。

This is dummy code for reference这是供参考的虚拟代码

<div>
<div role="button" tabindex="0">
</div>

<div *ngIf="showList" role="list">
<div role="listitem" tabindex="0">
</div>
<div role="listitem" tabindex="0">
</div>
</div>
</div>

Tried using aria roles menu, menuitem but could not move focus and could not navigate using arrow keys in list尝试使用 aria roles menu, menuitem 但无法移动焦点并且无法使用列表中的箭头键导航

Sorry, but if you want to go custom, you need to go all the way and implement the keyboard interface yourself.不好意思,如果要自定义go,需要一路go,自己实现键盘接口。

ARIA roles do not implement interaction, they only communicate the nature of interaction to manage users' expectations, or to provide shortcuts to get to the element. ARIA 角色不实现交互,它们仅传达交互的本质以管理用户的期望,或提供获取元素的快捷方式。

There might be some libraries that help you apply the default patterns, as described on the ARIA Authoring Practices Guide (APG)可能有一些库可以帮助您应用默认模式,如ARIA 创作实践指南 (APG)中所述

If you check the Select-Only Combobox Example on APG , you will notice that you also will need some more ARIA attributes to comply with the standard in your dropdown:如果您检查APG 上的 Select-Only Combobox 示例,您会注意到您还需要一些更多的 ARIA 属性以符合下拉列表中的标准:

  • aria-expanded to expose on the button whether the list is currently expanded or not aria-expanded在按钮上显示列表当前是否展开
  • aria-haspopup=listbox to expose that the button opens a list aria-haspopup=listbox显示按钮打开列表
  • an accessible name, of course ( aria-label or real text contents)一个可访问的名称,当然( aria-label或真实文本内容)
  • role=combobox and not button, actually role=combobox而不是按钮,实际上
  • role=option instead of listitem role=option而不是listitem
  • aria-selected on the active option aria-selected在活动选项上

It is generally good advice to use native components whenever you can.尽可能使用本机组件通常是个好建议。 They are optimised for the platform, fast, highly accessible and need less resources.它们针对平台进行了优化,速度快,易于访问且需要的资源更少。

You can apply some styling and tricks like hiding the initial, unfocused input.您可以应用一些样式和技巧,例如隐藏初始的、未聚焦的输入。 The CSS property accent-color also allows some basic styling in modern browsers. CSS 属性accent-color还允许现代浏览器中的一些基本样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM