百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
I

inclusive-menu-button

> 编程语言
开源

一个键盘和屏幕阅读器可以访问ARIA菜单按钮执行.

195 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

一个键盘和屏幕阅读器可以访问ARIA菜单按钮执行.

Inclusive Menu Button

A menu button module that implements the correct ARIA semantics and keyboard behavior.

Installation

bash
npm i inclusive-menu-button --save

Expected markup

In the following example, three menu items are provided.

xml
  • The parent element must take data-inclusive-menu.
  • data-inclusive-menu-opens takes a value that must match the menu element's id. In this case, it is difficulty.
  • data-inclusive-menu-from defines from which side of the button the menu will grow. Any value but "right" will mean it grows from the left.
  • The menu items must be sibling buttons. The script adds the menuitem role (as well as the menu role to the parent menu element).

After initialization

Once you've initialized the menu button, this will be the resulting markup, including all of the necessary ARIA attribution:

xml

CSS

The following functional styling is provided for the basic layout of an archetypal "dropdown" menu appearance. You can either override and add to these styles in the cascade or remove them altogether and start from scratch.

css
[data-inclusive-menu] {
  position: relative;
  display: inline-block;
}

[data-inclusive-menu-opens],
[data-inclusive-menu] [role^="menuitem"] {
  text-align: left;
  border: 0;
}

[data-inclusive-menu] [role="menu"] {
  position: absolute;
  left: 0;
}

[data-inclusive-menu] [data-inclusive-menu-from="right"] {
  left: auto;
  right: 0;
}

[data-inclusive-menu] [role^="menuitem"] {
  display: block;
  min-width: 100%;
  white-space: nowrap;
}

[data-inclusive-menu] [role^="menuitem"][aria-checked="true"]::before {
  content: '\2713\0020';
}

Initialization

Initialize the menu button / menu like so:

javascript
// get a menu button
const exampleButton = document.querySelector('[data-inclusive-menu-opens]')

// Make it a menu button
const exampleMenuButton = new MenuButton(exampleButton)

Checked items

Sometimes you'd like to persist the selected menu item, using a checked state. WAI-ARIA provides menuitemradio (allowing the checking of just one item) and menuitemcheckbox (allowing the checking of multiple items). Checked items are marked with aria-checked="true".

You can supply the constructor with a checkable value of 'none' (default), 'one', or 'many'. In the following example, 'one' is chosen, implementing menuitemradio. See the examples folder for working demonstrations.

javascript
// Make it a menu button with menuitemradio buttons
const exampleMenuButton = new MenuButton(exampleButton, { checkable: 'one' })

If you want to set default checked items, just do that in the HTML:

xml

The basic CSS (see above) prefixes the checked item with a check mark. This declaration can be removed safely and replaced with a different form of indication.

API methods

You can open and close the menu programmatically.

javascript
// Open
exampleMenuBtn.open()

// Close
exampleMenuBtn.close()

// Toggle
exampleMenuBtn.toggle()

Event subscription

You can subscribe to emitted open, close, and choose events.

open and close examples

javascript
exampleMenuButton.on('open', function () {
  // Do something when the menu gets open
})

exampleMenuButton.on('close', function () {
  // Do something when the menu gets closed
})

choose example

The choose event is passed the chosen item’s DOM node.

javascript
exampleMenuButton.on('choose', function (choice) {
  // Do something with `choice` DOM node
})

Unsubscribing

There is an off method included for terminating event listeners.

javascript
exampleMenuButton.off('choose', exampleHandler)

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言