A (native) web component for a menu that scrolls horizontally or vertically.
- Based on Shadow DOM v1 and Custom Elements v1.
- Menu items (
<option>) are virtualized, meaning that only those within visible boundaries are added to the DOM tree, thereby supporting very long lists. - Mostly unstyled, making customization simpler.
Check out the feature demo.
Node.js >= 10 is required. To install, type this at the command line:
npm install scrolling-menuImport as an ES Module:
import 'scrolling-menu';Import as a CommonJS Module:
require('scrolling-menu');Instantiate via HTML:
<scrolling-menu>
<option value="1">Label 1</option>
<option value="2" selected>Label 2</option>
<option value="3" disabled>Label 3</option>
</scrolling-menu>Instantiate via JavaScript:
document.createElement('scrolling-menu').append(document.createElement('option'));Type: String
Default value: 'vertical'
The axis with which menu options follow. Possible values: 'horizontal', 'vertical'.
Type: Boolean
Default value: false
Sets the state of user interactivity. Inspired by HTMLElement::disabled.
Type: Number
The index of the last selected option, where multiple selections are not possible. Inspired by HTMLSelectElement::selectedIndex.
See direction property.
See disabled property.
Custom HTML content for the UI control that decrements the selected option.
<scrolling-menu>
<span slot="decrement">
<i class="some-icon"></i>
Decrement
</span>
</scrolling-menu>Custom HTML content for the UI control that increments the selected option.
<scrolling-menu>
<span slot="increment">
<i class="some-icon"></i>
Increment
</span>
</scrolling-menu>Custom HTML content for each menu item. Instances of {{label}} and {{value}} will be interpolated.
<scrolling-menu>
<a href="path/{{value}}" slot="option">
<i class="some-icon"></i>
{{label}}
</a>
</scrolling-menu>The disabled-state option part.
scrolling-menu::part(disabled-option) {
/* … */
}The element that contains the option slot.
scrolling-menu::part(option) {
/* … */
}The element that contains [elements that contain] option parts.
scrolling-menu::part(options-container) {
/* … */
}The selected-state option part.
scrolling-menu::part(selected-option) {
/* … */
}Depending on your target browsers, you may need polyfills/shims for the following:
Array::find,Array.from,Array::includesattachShadow,customElementsclassList,toggleAttributedispatchEvent,new Event()Math.trunc,Number.isNaNMutationObserver,ResizeObserverrequestAnimationFrame<template>
- Why not add
optionsandselectedOptionsproperties fromHTMLSelectElement?
Unfortunately, they're liveHTMLElementcollections that cannot be extended.
