组合框导航
附加 组合框导航行为( ARIA 1.2) 改为 <input>。 。 。 .
安装
$ npm install @github/combobox-nav
使用量
HTML 语言
<label>
Robot
<input id="robot-input" type="text" />
</label>
<ul role="listbox" id="list-id" hidden>
<li id="baymax" role="option">Baymax</li>
<li><del>BB-8</del></li>
<!-- `role=option` needs to be present for item to be selectable -->
<li id="hubot" role="option">Hubot</li>
<li id="r2-d2" role="option">R2-D2</li>
</ul>
标记要求 :
- 每个选项都需要有
role="option"和一个独特的id - 名单应该有
role="listbox"
联合来文
import Combobox from '@github/combobox-nav'
const input = document.querySelector('#robot-input')
const list = document.querySelector('#list-id')
// install combobox pattern on a given input and listbox
const combobox = new Combobox(input, list)
// when options appear, start intercepting keyboard events for navigation
combobox.start()
// when options disappear, stop intercepting keyboard events for navigation
combobox.stop()
// move selection to the nth+1 item in the list
combobox.navigate(1)
// reset selection
combobox.clearSelection()
// uninstall combobox pattern from the input
combobox.destroy()
活动
A combobox-commit 当通过键盘或单击选择一个选项时,事件会在列表元素上开火.
例如,在选择一个选项时自动完成 :
list.addEventListener('combobox-commit', function (event) {
console.log('Element selected: ', event.target)
})
说明 使用时
<label>+ 键<input>作为选项,请听change改为combobox-commit。 。 。 .
当标签被点击时, click 事件均被发射 <label> 及其相关投入 label.control由于组合框不知道控制, combobox-commit 无法用作项目选择状态的指标.
A combobox-select 当选定一个选项但尚未执行时,该事件会被点入列表元素.
例如,在选择一个选项但尚未执行时自动完成 :
list.addEventListener('combobox-select', function (event) {
console.log('Element selected : ', event.target)
})
设置
对于高级配置,构造器会选择第三个参数. 例如:
const combobox = new Combobox(input, list, {tabInsertsSuggestions: true})
这些设置可用 :
tabInsertsSuggestions: boolean = true- 控制在何时插入突出建议 标签 已按下 (输入 将始终插入一个建议,不论设置如何)。 何时true,标签导航会在打开时被劫持(这可能会对无障碍性产生不利影响),但组合框会更紧密地模仿本土的IDE体验.firstOptionSelectionMode: FirstOptionSelectionMode = 'none'- 此选项指定了默认行为, 当尚未选择任何选项, 而用户按下 输入下列数值:FirstOptionSelectionMode将采取以下行动:'none':不要自动选择第一个选项.'active': 将第一个选项置于未选中的“ 活动” 状态( 不是aria-activedescendant),但如果用户按下Enter。要选择第二个项目,用户需要按下两次箭头。 这种方法可以在不干扰屏幕阅读器用户的情况下快速应用选择.
3⁄4 ̄ ̧漯B 屏幕阅读器不会宣布第一个项目是默认的. 这一点应明确宣布,使用
aria-live状态'selected':通过导航选择第一个项目。 这使得可以快速应用选择,并使得选择第二个项目的速度更快,但对于屏幕阅读器用户来说可能具有干扰性或混乱.
scrollIntoViewOptions?: boolean | ScrollIntoViewOptions = undefined- 当 控制标记的元素[aria-selected="true"]键盘导航,选中的元素将被调用到 元素. 滚动到视频。配置此值以控制滚动行为(或boolean或一个 滚动到视频选项 对象.
发展
npm install
npm test