Overview
A dropmenu is a nds-dropmenu wrapper that holds a trigger button (nds-dropmenu-trigger) and a menu (nds-dropmenu-menu). The menu holds a scroll area (nds-dropmenu-scroll) of items (nds-dropmenu-item), and an optional footer. Many NDS components open their menus through it: Custom Select, Autocomplete, Multiselect, Pagination, Filter and the date and time pickers.
Pick another component when:
- the choice is a field in a form: Custom Select
- the menu holds the site’s main links: Main Navigation
- the content needs more room, or steps: Modal or Drawer
- the element shows a short hint on hover: Tooltip
- the control turns one setting on or off: Switch
Markup
<div class="nds-dropmenu">
<button type="button" class="nds-btn nds-secondary-outline nds-menu-btn nds-dropmenu-trigger">
<span class="nds-label">Actions</span>
</button>
<div class="nds-dropmenu-menu" hidden>
<div class="nds-dropmenu-scroll">
<button type="button" class="nds-btn nds-subtle nds-dropmenu-item">
<i class="nds-icon nds-hgi-edit-02" aria-hidden="true"></i>
<span class="nds-label">Edit</span>
</button>
<button type="button" class="nds-btn nds-subtle nds-dropmenu-item">
<i class="hgi hgi-stroke hgi-copy-01" aria-hidden="true"></i>
<span class="nds-label">Duplicate</span>
</button>
<button type="button" class="nds-btn nds-subtle nds-dropmenu-item">
<i class="hgi hgi-stroke hgi-share-01" aria-hidden="true"></i>
<span class="nds-label">Share</span>
</button>
<hr class="nds-divider">
<button type="button" class="nds-btn nds-subtle nds-destructive nds-dropmenu-item">
<i class="nds-icon nds-hgi-delete-02" aria-hidden="true"></i>
<span class="nds-label">Delete</span>
</button>
</div>
</div>
</div>
Variants
Every option goes on the nds-dropmenu wrapper, except Center items, which goes on the menu. Required works only with a picker (data-select-name).
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Actions (default) | — | — | Actions on the current page |
| Structure | Links | canon #dm-links |
— | Links to other pages |
| Structure | Row menu | canon #dm-row |
— | The actions of one table row or card, behind a three-dot button. The hidden label names the row |
| Structure | Settings (id: settings) | canon #dm-settings |
— | Switches or checkboxes in a group, with a footer of buttons. Copy a checkbox group from Checkbox and a switch group from Switch, and add nds-dropmenu-group to the fieldset. A click on a control never closes the menu |
| Structure | Picker | canon #dm-picker |
— | A button that picks one value, in a small slot: an input prefix or a per-page count. The value submits with the form. For a form field, use Custom Select |
| Structure | Lazy menu | canon #dm-lazy |
— | The menu sits in a <template> and is built on the first click, so a long menu adds no markup at page load. Only for a menu that no other component reads: not Filter, Share or Multiselect. Search engines and the browser’s find do not see the items until then |
| Anchor | Center (default) | — | — | The menu centers under the trigger |
| Anchor | Start (hint: The menu lines up with the trigger’s start edge) | [data-anchor="start"] |
.nds-dropmenu |
The menu lines up with the trigger’s start edge. For a wide trigger |
| Anchor | End (hint: The menu lines up with the trigger’s end edge) | [data-anchor="end"] |
.nds-dropmenu |
The menu lines up with the trigger’s end edge |
| Anchor | Cursor (hint: Opens under the pointer, like a context menu) | [data-anchor-cursor] |
.nds-dropmenu |
The menu opens under the pointer, like a context menu. A keyboard or script open centers it |
| Search | Search (not: settings) | [data-search] |
.nds-dropmenu |
A search box at the top of the menu. For a long list. It filters nds-dropmenu-item rows, so not a settings menu |
| Required | Required | [data-required] |
.nds-dropmenu[data-select-name] |
The form does not submit until a value is picked |
| Center items | Center items (hint: For short labels) | .nds-center |
.nds-dropmenu-menu |
Centers each item’s label. For short values, such as numbers |
| Delay | Delayed open (hint: Waits with a spinner while your script fills the menu) | [data-delay="800"] |
.nds-dropmenu |
The first open waits 800 ms, with a spinner on the trigger, while your script fills the menu |
Behavior
Picker
A picker is a dropmenu that chooses one value, like a small select. Add data-select-name to the wrapper and data-value to each item. The script keeps the picked value in a hidden input, so it submits with the form. The trigger shows the picked item’s label, and the wrapper fires nds:dropmenu:select. Use it in a small slot, such as a country code before a phone number or a page-size count beside a table.
Search
data-search adds a search box above the items, and it gets focus when the menu opens. The list filters as the user types, and a message shows when nothing matches. The search ignores case and accents, so “cafe” finds “café” and «مطار» finds «المَطار». data-search-value adds words an item is also found by, such as its name in another language. Add it to a long list, such as countries. data-search="20" adds the box only when the menu has 20 items or more. Items a script adds later, such as a Filter’s options, count when the menu opens. A group with no matching row hides, and so does a Filter group shown as an accordion item, and so does the divider left beside it. A menu nested in the group is not counted.
Settings Menu
A settings menu holds switches or checkboxes, so the user changes several things before the menu closes. A click on a control never closes the menu. The footer buttons are items: a click on Save closes the menu, and Reset keeps it open with data-no-auto-close. Enter on a switch or a checkbox clicks the item with data-dropmenu-primary, here Save.
Anchor
The menu centers under its trigger by default. On a wide trigger, such as a full-width button, line the menu up with the start or the end edge instead. data-anchor-cursor opens the menu where the user clicked, like a context menu.
Delayed Open
data-delay gives your script time to fill a menu the first time it opens, for items that come from a server or cost time to build. On the first click, the trigger shows a spinner for the given milliseconds. Then the dropmenu fires nds:dropmenu:prepare, and your script adds the items in that event. Later opens are immediate.
For items that come from a request, pass the request to event.detail.waitUntil() in that event. The spinner stays until the request ends, whether it passed or failed, and then the menu opens. Use data-delay="0" to wait for the request only. If the user clicks away while the request runs, the menu does not open; the next click waits for the same request.
Lazy Menu
A lazy menu keeps its items in a <template>, so they are not in the page until the first click builds them. Use it where a page has many menus, such as one in every row of a long table. Other components cannot read a lazy menu, and search engines do not see its items.
Built-in Features
-
Auto-initialization
Every
.nds-dropmenuon the page starts by itself. CallNDS.Dropmenu.init()after you add new ones. -
Menu Position
The menu opens below the trigger, and above it when there is more room there. A menu that fits on neither side shrinks its scroll area, so a search box or a footer stays in view. A menu with no scroll area, such as a calendar, keeps its size: the page scrolls to show it whole. Only where the page cannot, in a modal, in a fixed panel or on a very short screen, the menu scrolls instead. It never runs past the side of the screen. It stays on its trigger while the page or a box around it scrolls, inside a fixed panel too. When search or new content changes its height, it keeps its side, and a menu above the trigger moves so its bottom stays on the trigger.
-
No Clipping
The open menu shows in the browser's top layer, so a modal, a scrolling table or a drawer never cuts it off. It stays inside its wrapper in the page, so a dark area, a click listener on a parent and
closest()still reach it. It moves with the page as you scroll, over the sticky main navigation too, and closes when its trigger scrolls out of view. -
Keyboard Navigation
The arrow keys open the menu and move between items, and Escape closes it. The API lists every key.
-
Accessibility
The script sets the menu roles and
aria-expandedon the trigger. Focus returns to the trigger when the menu closes. The open animation stops when the user asks for reduced motion. -
Programmatic Control
Open, close and remove a dropmenu from a script, and listen to its events. A component built on it can turn off the trigger click or the keys, and drive the menu itself.
Best Practices
- Use a dropmenu for actions or links that do not need to show all the time, such as the actions of one row.
- Keep a menu to 3 to 8 items. Split a longer list into groups, or add a search box.
- Give every button item
type="button". In a form, a button without it submits the form. - Use
<button>for an action and<a>for a link to another page. - Put a destructive action last, after a divider, and give it
nds-destructive. - Give an icon-only trigger a label that names what it acts on, such as “Actions for request 20481”. The label is hidden on screen.
- Add
data-no-auto-closeto an item that must keep the menu open, such as a Reset button. - Do not fix a clipped menu with
overfloworz-index. The open menu is in the top layer, where nothing clips it. - Call
NDS.Dropmenu.destroy()before you remove a dropmenu from the page, such as a table row you replace.
API
Other Classes
| Class | Element | Effect |
|---|---|---|
nds-menu-btn |
.nds-dropmenu-trigger |
A chevron after the label. It turns while the menu is open |
nds-dropmenu-group |
a fieldset or div in the menu |
Groups related controls, with a bold legend and the same spacing as items |
nds-dropmenu-footer |
div in .nds-dropmenu-menu, after .nds-dropmenu-scroll |
Holds the footer buttons. It sits outside the scroll area, so it stays in view |
nds-dropmenu-action |
div in .nds-dropmenu-footer |
Puts its buttons side by side, sharing the width. A button that does not fit moves to the next line |
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-select-name |
.nds-dropmenu |
Makes the dropmenu a picker. A hidden input with this name holds the picked value, and fires input and change when it changes |
data-select-value |
.nds-dropmenu |
The value picked at load. It wins over an item with data-state="selected" |
data-required |
.nds-dropmenu[data-select-name] |
The form does not submit until a value is picked. The error shows on the field that holds the picker |
data-search |
.nds-dropmenu |
Adds a search box. data-search="20" adds it only when the menu has 20 items or more |
data-search-empty |
.nds-dropmenu |
The text shown when nothing matches. The default is “No results” |
data-anchor, data-anchor-cursor |
.nds-dropmenu |
Where the menu lines up with the trigger. See Anchor in the builder |
data-delay |
.nds-dropmenu |
The first open waits this many milliseconds and fires nds:dropmenu:prepare. The attribute is removed after it, so later opens are immediate |
data-dropmenu-no-click |
.nds-dropmenu |
The trigger does not open the menu. A script opens it |
data-dropmenu-no-keys |
.nds-dropmenu |
Turns off the arrow, Home, End and Tab keys, for a component with its own keyboard. Escape still closes the menu |
data-state |
.nds-dropmenu, trigger, menu, .nds-dropmenu-item |
The script writes open on the wrapper, the trigger and the menu, and selected on the picked item. An item with active gets focus first when the menu opens from the keyboard |
popover |
.nds-dropmenu-menu |
The script sets manual at init, so the open menu shows in the browser’s top layer. It removes it on destroy() |
data-position-vertical |
.nds-dropmenu, .nds-dropmenu-menu |
The script writes top when the menu opens above the trigger, and removes it on close. Do not set it yourself |
data-value |
.nds-dropmenu-item |
The value the item picks. An item without it picks nothing |
data-trigger-label |
.nds-dropmenu-item |
A short label for the trigger when the item is picked, such as “+966” for “Saudi Arabia (+966)”. The default is the item’s .nds-label |
data-search-value |
.nds-dropmenu-item |
More words the item is found by |
data-search-item |
an item or a row in the menu | data-search-item makes a row that is not a .nds-dropmenu-item searchable. data-search-item="false" keeps an item always in view |
data-no-auto-close |
.nds-dropmenu-item |
A click on the item keeps the menu open |
data-dropmenu-primary |
.nds-dropmenu-item |
Enter on a control that is not a button or a link clicks the first item with this attribute |
Keyboard
| Key | Where | Effect |
|---|---|---|
| Enter, Space | trigger | Opens or closes the menu, and focuses the first item |
| Arrow Down, Arrow Up | trigger | Opens the menu and focuses the first or the last item. An item with data-state="active" gets focus first |
| Arrow Down, Arrow Up | menu | Moves to the next or the previous item |
| Home, End | menu | Moves to the first or the last item |
| Tab, Shift + Tab | menu | Moves through the items. Past the last or the first, the menu closes |
| Escape | trigger, menu | Closes the menu and returns focus to the trigger. In a search box, the first press clears it |
| Alt + Arrow, Ctrl + Home, Ctrl + End | a text field in the menu | Moves between items, since the plain keys stay with the field |
CSS Custom Properties
Set these on the .nds-dropmenu wrapper. The menu inherits them.
| Property | Default | Controls |
|---|---|---|
--dropmenu-width |
min-content |
Width of the menu |
--dropmenu-min-width |
max-content |
Least width of the menu. The default keeps each item on one line. 100% makes the menu as wide as the wrapper |
--dropmenu-max-width |
calc(100vw - 2 * var(--nds-viewport-padding)) |
Most width of the menu: the screen less the page gutter on each side |
--menu-padding |
var(--spacing-md) |
Space inside the menu |
--dropmenu-slide |
8px |
How far the menu slides as it opens. It slides the other way when the menu opens above the trigger |
Tokens
It reads these shared tokens. A change to one also changes other components: see Tokens.
Source: the var() reads in _sass/components/_dropmenu.scss.
| Token | Preview | Value |
|---|---|---|
--background-menuRead by background-color | var(--colors-base-white)Dark mode var(--background-default) | |
--text-displayRead by color | var(--colors-neutral-800)Dark mode var(--colors-neutral-100) | |
--border-neutral-secondaryRead by border | var(--colors-neutral-200)Dark mode var(--colors-neutral-700) |
JavaScript
| Method | Effect |
|---|---|
NDS.Dropmenu.init() |
Starts every .nds-dropmenu on the page that has not started yet |
NDS.Dropmenu.create(wrapper, options) |
Starts one wrapper and returns its instance, or null when it has no menu. options is for a field that another component owns: menu, menuClass, click: false, keys: false and anchor. The script skips a class or flag you wrote |
NDS.Dropmenu.destroy(wrapper) |
Removes its listeners, and takes back what the script wrote: the class and flags from the create() options, and the ARIA attributes (the ones you wrote stay). On a picker it also removes the hidden field the script built and puts the trigger text back; the pick stays on its item. Call it before you remove the wrapper from the page: otherwise a listener on the document (while the menu is open) or on the form (a picker) keeps the removed wrapper in memory |
NDS.Dropmenu.from(el) |
Returns the wrapper of any element in the dropmenu |
NDS.Dropmenu.getMenu(wrapper) |
Returns the menu of a wrapper |
NDS.Dropmenu.validate(el, options) |
Checks a select-mode picker and returns { valid, message, value }. Pass { showMessage: false } to check without a message |
NDS.Dropmenu.getValue(el) |
Returns the data-value of the picked item in a picker, or '' when none is picked or el is not a picker. el is the wrapper, an element in it, its id or a selector |
NDS.Dropmenu.setValue(el, value) |
Picks the item with that data-value and returns true. It returns false, and changes nothing, when no item has the value or el is not a picker |
NDS.Dropmenu.clear(el) |
Picks nothing: the hidden field is empty and the trigger label goes back to its placeholder. Returns true, or false when el is not a picker |
NDS.Dropmenu.reset(el) |
Picks the item picked at load again, or nothing when there was none, as a form reset does. It fires no events. Returns true, or false when el is not a picker |
wrapper.ndsDropmenu.getValue(), .setValue(value), .clear(), .reset() |
The same, on a picker that has started |
wrapper.ndsDropmenu.open(), .close(), .toggle(event) |
Opens or closes the menu. Pass the click event to toggle(): on a data-anchor-cursor menu, the menu then opens under the pointer |
wrapper.ndsDropmenu.syncPosition() |
Places the open menu again |
wrapper.ndsDropmenu.setSelection(item) |
Picks an item in a picker |
wrapper.ndsDropmenu.isOpen |
true while the menu is open. A property, not a method |
wrapper.ndsDropmenu.trigger, .dropmenu |
The trigger, and the wrapper. Read-only |
wrapper.ndsDropmenu.destroy() |
The same as NDS.Dropmenu.destroy(wrapper) |
| Event | Fired on | Detail |
|---|---|---|
nds:dropmenu:opened |
.nds-dropmenu |
{ dropmenu, trigger, menu, isOpen }, after the menu is placed |
nds:dropmenu:closed |
.nds-dropmenu |
{ dropmenu, trigger, menu, isOpen } |
nds:dropmenu:select |
.nds-dropmenu |
{ dropmenu, item, value }, when a picker picks a value |
nds:dropmenu:prepare |
.nds-dropmenu |
{ dropmenu, trigger, menu, isOpen, waitUntil(promise) }, before a delayed first open. Fill the menu here. waitUntil() keeps the menu closed until the promise ends |
var picker = document.querySelector('[data-select-name="country"]');
picker.addEventListener('nds:dropmenu:select', function (e) {
console.log('Country:', e.detail.value);
});
// A menu with data-delay="0": fill it from a request on the first open
var branches = document.querySelector('#branches-menu');
branches.addEventListener('nds:dropmenu:prepare', function (e) {
var scroll = e.detail.menu.querySelector('.nds-dropmenu-scroll');
e.detail.waitUntil(NDS.request('/api/branches').then(function (res) {
res.data.forEach(function (name) {
var item = document.createElement('button');
item.type = 'button';
item.className = 'nds-btn nds-subtle nds-dropmenu-item';
item.innerHTML = '<span class="nds-label"></span>';
item.firstChild.textContent = name;
scroll.appendChild(item);
});
}));
});
The full API is in the banner of _js/nds-dropmenu.js.
Related
- Pagination: a picker with
data-per-page-targetsets the number of items per page. - Forms: a picker in an input prefix, for a phone country code.
- Contact Us Template: the phone field with a country-code picker.
- Manage Records: a per-page picker beside a table.
- Search Template: the filter menu is a dropmenu with groups and a footer.