Partwise
Overlays

Dropdown menu

A list of actions that opens from a button, or from a right click anywhere in an area.

From a button

HTML
<button class="pw-btn" data-pw-menu="#demo-menu">Options</button>

<div class="pw-menu" id="demo-menu" hidden>
  <button>Rename <kbd>F2</kbd></button>
  <button>Duplicate</button>
  <a href="#demo-menu">Open in a new tab</a>
  <hr>
  <div class="pw-menu-label">View</div>
  <button role="menuitemcheckbox" aria-checked="true" data-keep-open>Show grid</button>
  <button role="menuitemradio" aria-checked="true" data-group="size" data-keep-open>Small</button>
  <button role="menuitemradio" aria-checked="false" data-group="size" data-keep-open>Large</button>
  <hr>
  <button disabled>Archive</button>
  <button class="pw-menu-danger">Delete</button>
</div>
<p class="demo-out" id="demo-menu-out"></p>
JavaScript
document.getElementById('demo-menu').addEventListener('pw:select', function (e) {
  document.getElementById('demo-menu-out').textContent = 'You chose: ' + e.detail.value;
});

Context menu

Right click in the area, or press and hold on a touch screen. With the keyboard: focus the area and press the Menu key or Shift + F10.

Right click here
HTML
<div class="demo-area" tabindex="0" data-pw-context="#demo-context">Right click here</div>

<div class="pw-menu" id="demo-context" aria-label="File actions" hidden>
  <button>Copy</button>
  <button>Paste</button>
  <hr>
  <button>Download</button>
</div>

Where it opens

data-place on the menu sets the side. If there is no room, it flips to the other side.

HTML
<button class="pw-btn" data-pw-menu="#demo-menu-end">Bottom end</button>
<button class="pw-btn" data-pw-menu="#demo-menu-top">Top start</button>
<button class="pw-btn" data-pw-menu="#demo-menu-right">Right</button>

<div class="pw-menu" id="demo-menu-end" data-place="bottom-end" hidden><button>Profile</button><button>Settings</button><button>Sign out</button></div>
<div class="pw-menu" id="demo-menu-top" data-place="top-start" hidden><button>Profile</button><button>Settings</button><button>Sign out</button></div>
<div class="pw-menu" id="demo-menu-right" data-place="right-start" hidden><button>Profile</button><button>Settings</button><button>Sign out</button></div>

Options

Set them as attributes on the element.

AttributeDefaultWhat it does
data-pw-menuOn the button: the selector of the menu.
data-pw-contextOn an area: the selector of its context menu.
data-placebottom-startOn the menu: top, bottom, left or right, plus -start or -end.
data-keep-openOn an item: the menu stays open after a click.
data-valueOn an item: the value sent with pw:select. Without it, the text of the item.
data-groupOn radio items: items with the same group switch each other off.

Events

Listen with addEventListener. Details are in event.detail.

EventWhen
pw:selectOn the menu, with item, value and checked.
pw:open, pw:closeOn the menu.
pw:contextOn the area, with the point x, y.

From your script

Partwise.menu.open('#demo-menu', buttonElement);
Partwise.menu.open('#demo-context', { x: 200, y: 120 });   // at a point
Partwise.menu.close();

Keyboard

KeyAction
Down UpNext and previous item. On the button: open the menu.
Home EndFirst and last item
A-ZJump to the item that starts with the letters you type
Enter SpaceChoose
EscClose and go back to the button
TabClose and move on

Good to know

  • Items are plain <button> and <a> elements. The script adds the menu roles.
  • One menu is open at a time.
  • Menus have one level. There are no submenus.

Files

Everything is in dist/partwise.css and dist/partwise.js. To load only this component, use src/css/00-base.css and src/js/00-core.js plus: css/13-menu.css, js/13-menu.js.