@anypoint-web-components/anypoint-dropdown-menu

中文文档
0.1.24发布于 2023/06/17Apache-2.0ESM + CJS近 30 天下载 3422

Accessible dropdown menu for Anypoint platform

原始使用文档
文档目录13 节
  1. Styling options
  2. Usage
  3. Installation
  4. In an HTML file
  5. Replacement for the anypoint-listbox element
  6. Value selection
  7. Assistive text
  8. Form-associated custom elements
  9. Validation
  10. Accessibility
  11. Development
  12. Running the demo locally
  13. Running the tests

Deprecated

This component has been moved to anypoint-web-components/awc.


This component is based on Material Design menu and adjusted for Anypoint platform components.

Anypoint web components are set of components that allows to build Anypoint enabled UI in open source projects.

Exposed dropdown menus display the currently selected menu item above the menu. They can be used only when a single menu item can be chosen at a time.

Styling options

The element has three built-in themes:

  • Material Design - Filled
  • Material Design - Outlined
  • Anypoint Design - Compatibility

By default the input renders filled dropdown list.

Filled menu

Outlined style is rendered when outlined property is set.

Outlined menu

Anypoint ready styles are rendered when compatibility property is set.

Compatibility menu

OSS application should not use Anypoint based styling as it's protected by MuleSoft copyrights. This property is reserved for OSS applications embedded in the Anypoint platform.

Published on NPM

Usage

Installation

npm install --save @anypoint-web-components/anypoint-dropdown

In an HTML file

<html>
  <head>
    <script type="module">
      import "@anypoint-web-components/anypoint-dropdown/anypoint-dropdown.js";
      import "@anypoint-web-components/anypoint-listbox/anypoint-listbox.js";
      import "@anypoint-web-components/anypoint-item/anypoint-item.js";
    </script>
  </head>
  <body>
    <anypoint-dropdown-menu
      aria-label="Select dinosaur from the list of available options"
    >
      <label slot="label">Selected dinosaur</label>
      <anypoint-listbox slot="dropdown-content" tabindex="-1">
        <anypoint-item>item 1</anypoint-item>
        <anypoint-item>item 2</anypoint-item>
        <anypoint-item>item 3</anypoint-item>
      </anypoint-listbox>
    </anypoint-dropdown-menu>
  </body>
</html>

Replacement for the anypoint-listbox element

It is possible to use different element than anypoint-listbox. The replacement should use AnypointMenuMixin which provides API for the dropdown element to work.

Value selection

When the user selects one of the options in the dropdown menu the list is closed and the rendered label is one of the following (in order):

  • label property of the selected item
  • label or data-label attribute io the selected item
  • innerText of the selected item

The same value is set as value property.

The selection can be controlled by setting selected attribute/property on the anypoint-listbox or the element implementing AnypointMenuMixin.

<anypoint-dropdown-menu
  aria-label="Select dinosaur from the list of available options"
>
  <label slot="label">Selected dinosaur</label>
  <anypoint-listbox slot="dropdown-content" tabindex="-1" selected="0">
    <anypoint-item label="My item 1">item 1</anypoint-item>
    <anypoint-item label="My item 2">item 2</anypoint-item>
    <anypoint-item>item 3</anypoint-item>
  </anypoint-listbox>
</anypoint-dropdown-menu>

By default the selected value is set to My item 1. When selecting option 3 then the label value becomes item 3.

Assistive text

Assistive text allows the user to better understand what kind of input is required. It can be an info message or invalid message when invalid input has been detected.

Info message

Info message provides the user with additional description for the field. It should be used when the label can be confusing or to ensure the user about the reason of collecting the input.

<anypoint-dropdown-menu infomessage="Will be added to your order.">
  <label slot="label">Select a dinosaur</label>
  <anypoint-listbox slot="dropdown-content" tabindex="-1">
    <anypoint-item>item 1</anypoint-item>
    <anypoint-item>item 2</anypoint-item>
    <anypoint-item>item 3</anypoint-item>
  </anypoint-listbox>
</anypoint-dropdown-menu>

Info message

Do not try to put too detailed information. The user should be able to scan the message in a fraction of a second. Treat it as an additional text for the label.

Invalid message

Error message should help the user recover from the error state. Use clear message with simple instructions of how to fix the problem, for example Only letters are allowed.

<anypoint-dropdown-menu
  invalidmessage="This value is required"
  invalid
  required
>
  <label slot="label">Select a dinosaur</label>
  <anypoint-listbox slot="dropdown-content" tabindex="-1">
    <anypoint-item>item 1</anypoint-item>
    <anypoint-item>item 2</anypoint-item>
    <anypoint-item>item 3</anypoint-item>
  </anypoint-listbox>
</anypoint-dropdown-menu>

Invalid message

Form-associated custom elements

The form-associated custom elements allows to associate a custom element with a <form> element. Original custom elements spec does not allow this. The form-associated custom elements may not be supported in some browsers so custom form elements may be required to be used (for example iron-form).

The element supports this API in browser that has this API implemented. This means that the element behaves like a <select> element when inserted into the form element. See demo page for an example.

Validation

The element support required and autoValidate properties. When the element is required then it renders invalid state when validate() or checkValidity() function is called. The validate() function is provided by ValidatableMixin for custom elements. checkValidity() function calls validate() function, and, if available, internal checkValidity() function provided by form-associated custom elements API.

The autoValidate option allows to automatically call validate function when value change. It can be used with custom validators implementing ValidatorMixin.

<anypoint-dropdown-menu required autovalidate>
  <label slot="label">Selected dinosaur</label>
  <anypoint-listbox slot="dropdown-content">
    <anypoint-item>item 1</anypoint-item>
    <anypoint-item>item 2</anypoint-item>
    <anypoint-item>item 3</anypoint-item>
  </anypoint-listbox>
</anypoint-dropdown-menu>

Accessibility

The element works perfectly with anypoint-listbox which together creates an accessible list of options. The listbox can be replaced by any other element that support similar functionality but make sure it has an appropriate aria support.

Development

git clone https://github.com/anypoint-web-components/anypoint-dropdown-menu
cd anypoint-dropdown-menu
npm install

Running the demo locally

npm start

Running the tests

npm test

版本与发布记录

@anypoint-web-components/anypoint-dropdown-menu 共发布 25 个版本,首次发布于 2019/08/06;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.24latest2023/06/173 年前
0.1.232021/11/204 年前
0.1.222021/10/165 年前
0.1.212021/05/195 年前
0.1.202020/11/275 年前
0.1.192020/11/265 年前

关于 @anypoint-web-components/anypoint-dropdown-menu 的常见问题

@anypoint-web-components/anypoint-dropdown-menu 是什么?

Accessible dropdown menu for Anypoint platform

如何安装 @anypoint-web-components/anypoint-dropdown-menu?

在项目目录运行 npm install @anypoint-web-components/anypoint-dropdown-menu,或使用 pnpm add / yarn add / bun add。

@anypoint-web-components/anypoint-dropdown-menu 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/anypoint-web-components__anypoint-dropdown-menu 是否存在。

@anypoint-web-components/anypoint-dropdown-menu 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@anypoint-web-components/anypoint-dropdown-menu 可以商用吗?

@anypoint-web-components/anypoint-dropdown-menu 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@anypoint-web-components/anypoint-dropdown-menu 的下载量有多少?

根据 npm 官方下载统计,@anypoint-web-components/anypoint-dropdown-menu 在 2026/10/06 – 2026/11/04 这 30 天内被下载 3,422 次,日均约 114 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@anypoint-web-components/anypoint-dropdown-menu 还在维护吗?

最新版本 0.1.24 发布于 2023/06/17(3 年前);至今共发布 25 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/08/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。