A menu opened with
.
Перейти к файлу
Armağan 926aedc6e3
Merge pull request #72 from github/dependabot/npm_and_yarn/ansi-regex-3.0.1
Bump ansi-regex from 3.0.0 to 3.0.1
2024-10-11 15:52:40 +10:00
.devcontainer Install chrome in codespaces via Dockerfile rather than use node-chromium package 2023-04-11 23:26:23 +00:00
.github/workflows Create .github/workflows/publish.yml 2022-03-28 14:13:04 +01:00
examples Add example 2022-10-24 10:03:38 -04:00
src Passing tests 2022-10-24 09:36:17 -04:00
test Install chrome in codespaces via Dockerfile rather than use node-chromium package 2023-04-11 23:26:23 +00:00
.eslintrc.json Convert to TypeScript 2020-05-28 15:07:26 -04:00
.gitignore Add <details-menu> custom element 2018-06-27 15:47:23 -06:00
CODEOWNERS move AOR to primer 2022-09-23 18:32:44 +01:00
LICENSE Open menu and focus item on arrow navigation 2019-01-16 17:09:17 -07:00
README.md Apply suggestions from code review 2020-06-03 16:21:04 -04:00
package-lock.json Merge branch 'main' into dependabot/npm_and_yarn/ansi-regex-3.0.1 2024-10-11 15:33:44 +10:00
package.json Install chrome in codespaces via Dockerfile rather than use node-chromium package 2023-04-11 23:26:23 +00:00
tsconfig.json Convert to TypeScript 2020-05-28 15:07:26 -04:00

README.md

<details-menu> element

A menu that's opened with a <details> button.

Installation

$ npm install @github/details-menu-element

Usage

Script

Import as a module:

import '@github/details-menu-element'

With a script tag:

<script type="module" src="./node_modules/@github/details-menu-element/dist/index.js">

Markup

<details>
  <summary>Robots</summary>
  <details-menu role="menu">
    <button type="button" role="menuitem">Hubot</button>
    <button type="button" role="menuitem">Bender</button>
    <button type="button" role="menuitem">BB-8</button>
  </details-menu>
</details>

Use data-menu-button and data-menu-button-text to update the button's text on menu item activation.

<details>
  <summary>Preferred robot: <span data-menu-button>None</span></summary>
  <details-menu role="menu">
    <button type="button" role="menuitem" data-menu-button-text>Hubot</button>
    <button type="button" role="menuitem" data-menu-button-text>Bender</button>
    <button type="button" role="menuitem" data-menu-button-text>BB-8</button>
  </details-menu>
</details>

Use data-menu-button and data-menu-button-contents to update the button's HTML content on menu item activation.

<details>
  <summary>Preferred robot: <span data-menu-button>None</span></summary>
  <details-menu role="menu">
    <button type="button" role="menuitem" data-menu-button-contents><img src="hubot.png"> Hubot</button>
    <button type="button" role="menuitem" data-menu-button-contents><img src="bender.png"> Bender</button>
    <button type="button" role="menuitem" data-menu-button-contents><img src="bb8.png"> BB-8</button>
  </details-menu>
</details>

Use label[tabindex="0"][role=menuitemradio/menuitemcheckbox] when dealing with radio and checkbox inputs menu items. Check states of the input element and the label will be synchronized.

<details>
  <summary>Preferred robot</summary>
  <details-menu role="menu">
    <label tabindex="0" role="menuitemradio">
      <input type="radio" name="robot" value="Hubot"> Hubot
    </label>
    <label tabindex="0" role="menuitemradio">
      <input type="radio" name="robot" value="Bender"> Bender
    </label>
    <label tabindex="0" role="menuitemradio">
      <input type="radio" name="robot" value="BB-8"> BB-8
    </label>
  </details-menu>
</details>

Events

  • details-menu-select (cancelable) - fired on <details-menu> with event.detail.relatedTarget being the item to be selected.
  • details-menu-selected - fired on <details-menu> with event.detail.relatedTarget being the item selected, after label is updated and menu is closed.

Deferred loading

Menu content can be loaded from a server by embedding an <include-fragment> element.

<details>
  <summary>Robots</summary>
  <details-menu src="/robots" preload>
    <include-fragment>Loading…</include-fragment>
  </details-menu>
</details>

The src attribute value is copied to the <include-fragment> the first time the <details> button is toggled open, which starts the server fetch.

If the preload attribute is present, the server fetch will begin on mouse hover over the <details> button, so the content may be loaded by the time the menu is opened.

Browser support

Browsers without native custom element support require a polyfill.

  • Chrome
  • Firefox
  • Safari
  • Microsoft Edge

Development

npm install
npm test

License

Distributed under the MIT license. See LICENSE for details.