HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<label for="select-el" class="combo-label">Native select element</label>
<div class="combo">
<select id="select-el" class="combo-input">
<option>Apple</option>
<option>Banana</option>
<option>Cherry</option>
<option>Durian</option>
<option>Eggplant</option>
<option>Fig</option>
<option>Guava</option>
<option>Huckleberry</option>
</select>
</div>
<label for="combo1" class="combo-label">ARIA 1.1 combobox with readonly input</label>
<div class="combo js-aria11">
<div role="combobox" aria-haspopup="listbox" aria-expanded="false" aria-owns="listbox1" class="input-wrapper">
<input
aria-activedescendant=""
aria-autocomplete="none"
id="combo1"
class="combo-input"
readonly
type="text">
<div class="combo-menu" role="listbox" id="listbox1"></div>
</div>
</div>
<label id="combo2-label" class="combo-label">Read-only combobox with no input</label>
<div class="combo js-select">
<div
aria-activedescendant=""
aria-autocomplete="none"
aria-owns="listbox2"
aria-expanded="false"
aria-haspopup="listbox"
aria-labelledby="combo2-label"
id="combo2"
class="combo-input"
role="combobox"
tabindex="0"
></div>
<div class="combo-menu" role="listbox" id="listbox2"></div>
</div>
<label id="popup-label" class="combo-label">Popup Button + listbox</label>
<div class="combo js-popup-button">
<button
aria-expanded="false"
aria-haspopup="listbox"
aria-labelledby="popup-label popup"
class="combo-input"
id="popup"
type="button"
></button>
<div aria-activedescendant="" class="combo-menu" role="listbox" tabindex="-1"></div>
</div>
<label id="listbox-label" class="combo-label">Listbox with aria-expanded</label>
<div class="combo js-listbox">
<div
aria-activedescendant=""
aria-expanded="false"
aria-labelledby="listbox-label listbox-current"
id="dropdown-listbox"
class="input-wrapper"
role="listbox"
tabindex="0"
>
<div class="combo-input" role="presentation" id="listbox-current"></div>
<div class="combo-menu" role="presentation"></div>
</div>
</div>
*, *::before, *::after {
box-sizing: border-box;
}
body {
background-color: #f5f5f5;
font-family: "Segoe UI", SegoeUI, "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 120%;
line-height: 1.4;
margin: 0;
padding: 2em;
}
.combo {
display: block;
margin-bottom: 1.5em;
max-width: 400px;
position: relative;
}
.combo::after {
border-bottom: 2px solid rgba(0,0,0,.5);
border-right: 2px solid rgba(0,0,0,.5);
content: '';
display: block;
height: 12px;
pointer-events: none;
position: absolute;
right: 16px;
top: 50%;
transform: translate(0, -65%) rotate(45deg);
width: 12px;
}
.input-wrapper {
border-radius: 4px;
}
.combo-input {
background-color: #f5f5f5;
border: 2px solid rgba(0,0,0,.5);
border-radius: 4px;
display: block;
font-size: 1em;
min-height: calc(1.4em + 26px);
padding: 12px 16px 14px;
text-align: left;
width: 100%;
}
select.combo-input {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
.open .combo-input {
border-radius: 4px 4px 0 0;
}
.combo-input:focus,
.combo-menu:focus,
.input-wrapper:focus {
border-color: #0067b8;
box-shadow: 0 0 4px 2px #0067b8;
outline: 5px solid transparent;
}
.combo-label {
display: block;
font-size: 20px;
font-weight: 100;
margin-bottom: 0.25em;
}
.combo-menu {
background-color: #f5f5f5;
border: 1px solid rgba(0,0,0,.42);
border-radius: 0 0 4px 4px;
display: none;
max-height: 300px;
overflow-y:scroll;
left: 0;
position: absolute;
top: 100%;
width: 100%;
z-index: 100;
}
.open .combo-menu {
display: block;
}
.combo-option {
padding: 10px 12px 12px;
}
.combo-option.option-current,
.combo-option:hover {
background-color: rgba(0,0,0,0.1);
}
.combo-option.option-selected {
padding-right: 30px;
position: relative;
}
.combo-option.option-selected::after {
border-bottom: 2px solid #000;
border-right: 2px solid #000;
content: '';
height: 16px;
position: absolute;
right: 15px;
top: 50%;
transform: translate(0, -50%) rotate(45deg);
width: 8px;
}
/* multiselect list of selected options */
.selected-options {
list-style-type: none;
margin: 0;
max-width: 400px;
padding: 0;
}
.selected-options li {
display: inline-block;
margin-bottom: 5px;
}
/*
* Helper constants and functions
*/
// make it easier for ourselves by putting some values in objects
// in TypeScript, these would be enums
const Keys = {
Backspace: 'Backspace',
Clear: 'Clear',
Down: 'ArrowDown',
End: 'End',
Enter: 'Enter',
Escape: 'Escape',
Home: 'Home',
Left: 'ArrowLeft',
PageDown: 'PageDown',
PageUp: 'PageUp',
Right: 'ArrowRight',
Space: ' ',
Tab: 'Tab',
Up: 'ArrowUp'
}
const MenuActions = {
Close: 0,
CloseSelect: 1,
First: 2,
Last: 3,
Next: 4,
Open: 5,
Previous: 6,
Select: 7,
Space: 8,
Type: 9
}
// filter an array of options against an input string
// returns an array of options that begin with the filter string, case-independent
function filterOptions(options = [], filter, exclude = []) {
return options.filter((option) => {
const matches = option.toLowerCase().indexOf(filter.toLowerCase()) === 0;
return matches && exclude.indexOf(option) < 0;
});
}
// return an array of exact option name matches from a comma-separated string
function findMatches(options, search) {
const names = search.split(',');
return names.map((name) => {
const match = options.filter((option) => name.trim().toLowerCase() === option.toLowerCase());
return match.length > 0 ? match[0] : null;
})
.filter((option) => option !== null);
}
// return combobox action from key press
function getActionFromKey(key, menuOpen) {
// handle opening when closed
if (!menuOpen && key === Keys.Down) {
return MenuActions.Open;
}
// handle keys when open
if (key === Keys.Down) {
return MenuActions.Next;
}
else if (key === Keys.Up) {
return MenuActions.Previous;
}
else if (key === Keys.Home) {
return MenuActions.First;
}
else if (key === Keys.End) {
return MenuActions.Last;
}
else if (key === Keys.Escape) {
return MenuActions.Close;
}
else if (key === Keys.Enter) {
return MenuActions.CloseSelect;
}
else if (key === Keys.Backspace || key === Keys.Clear || key.length === 1) {
return MenuActions.Type;
}
}
// get index of option that matches a string
function getIndexByLetter(options, filter) {
const firstMatch = filterOptions(options, filter)[0];
return firstMatch ? options.indexOf(firstMatch) : -1;
}
// get updated option index
function getUpdatedIndex(current, max, action) {
switch(action) {
case MenuActions.First:
return 0;
case MenuActions.Last:
return max;
case MenuActions.Previous:
return Math.max(0, current - 1);
case MenuActions.Next:
return Math.min(max, current + 1);
default:
return current;
}
}
// check if an element is currently scrollable
function isScrollable(element) {
return element && element.clientHeight < element.scrollHeight;
}
// ensure given child element is within the parent's visible scroll area
function maintainScrollVisibility(activeElement, scrollParent) {
const { offsetHeight, offsetTop } = activeElement;
const { offsetHeight: parentOffsetHeight, scrollTop } = scrollParent;
const isAbove = offsetTop < scrollTop;
const isBelow = (offsetTop + offsetHeight) > (scrollTop + parentOffsetHeight);
if (isAbove) {
scrollParent.scrollTo(0, offsetTop);
}
else if (isBelow) {
scrollParent.scrollTo(0, offsetTop - parentOffsetHeight + offsetHeight);
}
}
/*
* ARIA 1.1 Readonly Input code
*/
const ARIA11Combo = function(el, options) {
// element refs
this.el = el;
this.comboEl = el.querySelector('[role=combobox]')
this.inputEl = el.querySelector('input');
this.listboxEl = el.querySelector('[role=listbox]');
// data
this.idBase = this.inputEl.id;
this.options = options;
// state
this.activeIndex = 0;
this.open = false;
}
ARIA11Combo.prototype.init = function() {
this.inputEl.addEventListener('input', this.onInput.bind(this));
this.inputEl.addEventListener('blur', this.onInputBlur.bind(this));
this.inputEl.addEventListener('click', () => this.updateMenuState(true));
this.inputEl.addEventListener('keydown', this.onInputKeyDown.bind(this));
this.options.map((option, index) => {
const optionEl = document.createElement('div');
optionEl.setAttribute('role', 'option');
optionEl.id = `${this.idBase}-${index}`;
optionEl.className = index === 0 ? 'combo-option option-current' : 'combo-option';
optionEl.setAttribute('aria-selected', 'false');
optionEl.innerText = option;
optionEl.addEventListener('click', () => { this.onOptionClick(index); });
optionEl.addEventListener('mousedown', this.onOptionMouseDown.bind(this));
this.listboxEl.appendChild(optionEl);
});
}
ARIA11Combo.prototype.onInput = function() {
const curValue = this.inputEl.value;
const matches = filterOptions(this.options, curValue);
// set activeIndex to first matching option
// (or leave it alone, if the active option is already in the matching set)
const filterCurrentOption = matches.filter((option) => option === this.options[this.activeIndex]);
if (matches.length > 0 && !filterCurrentOption.length) {
this.onOptionChange(this.options.indexOf(matches[0]));
}
const menuState = this.options.length > 0;
if (this.open !== menuState) {
this.updateMenuState(menuState, false);
}
}
ARIA11Combo.prototype.onInputKeyDown = function(event) {
const { key } = event;
const max = this.options.length - 1;
const action = getActionFromKey(key, this.open);
switch(action) {
case MenuActions.Next:
case MenuActions.Last:
case MenuActions.First:
case MenuActions.Previous:
event.preventDefault();
return this.onOptionChange(getUpdatedIndex(this.activeIndex, max, action));
case MenuActions.CloseSelect:
event.preventDefault();
this.selectOption(this.activeIndex);
return this.updateMenuState(false);
case MenuActions.Close:
event.preventDefault();
return this.updateMenuState(false);
case MenuActions.Type:
this.onOptionChange(Math.max(0, getIndexByLetter(this.options, key)));
case MenuActions.Open:
return this.updateMenuState(true);
}
}
ARIA11Combo.prototype.onInputBlur = function() {
if (this.ignoreBlur) {
this.ignoreBlur = false;
return;
}
if (this.open) {
this.updateMenuState(false, false);
}
}
ARIA11Combo.prototype.onOptionChange = function(index) {
this.activeIndex = index;
this.inputEl.setAttribute('aria-activedescendant', `${this.idBase}-${index}`);
// update active style
const options = this.el.querySelectorAll('[role=option]');
[...options].forEach((optionEl) => {
optionEl.classList.remove('option-current');
});
options[index].classList.add('option-current');
if (this.open && isScrollable(this.listboxEl)) {
maintainScrollVisibility(options[index], this.listboxEl);
}
}
ARIA11Combo.prototype.onOptionClick = function(index) {
this.onOptionChange(index);
this.selectOption(index);
this.updateMenuState(false);
}
ARIA11Combo.prototype.onOptionMouseDown = function() {
this.ignoreBlur = true;
}
ARIA11Combo.prototype.selectOption = function(index) {
const selected = this.options[index];
this.inputEl.value = selected;
this.activeIndex = index;
// update aria-selected
const options = this.el.querySelectorAll('[role=option]');
[...options].forEach((optionEl) => {
optionEl.setAttribute('aria-selected', 'false');
});
options[index].setAttribute('aria-selected', 'true');
}
ARIA11Combo.prototype.updateMenuState = function(open, callFocus = true) {
this.open = open;
this.comboEl.setAttribute('aria-expanded', `${open}`);
open ? this.el.classList.add('open') : this.el.classList.remove('open');
callFocus && this.inputEl.focus();
}
// init combo
const aria11El = document.querySelector('.js-aria11');
const options = ['Apple', 'Banana', 'Cherry', 'Durian', 'Eggplant', 'Fig', 'Guava', 'Huckleberry'];
const aria11Component = new ARIA11Combo(aria11El, options);
aria11Component.init();
/*
* Read-only combobox with no input code
*/
const NoInput = function(el, options) {
// element refs
this.el = el;
this.comboEl = el.querySelector('[role=combobox]');
this.listboxEl = el.querySelector('[role=listbox]');
// data
this.idBase = this.comboEl.id;
this.options = options;
// state
this.activeIndex = 0;
this.open = false;
}
NoInput.prototype.init = function() {
this.comboEl.addEventListener('blur', this.onComboBlur.bind(this));
this.comboEl.addEventListener('click', () => this.updateMenuState(true));
this.comboEl.addEventListener('keydown', this.onComboKeyDown.bind(this));
this.options.map((option, index) => {
const optionEl = document.createElement('div');
optionEl.setAttribute('role', 'option');
optionEl.id = `${this.idBase}-${index}`;
optionEl.className = index === 0 ? 'combo-option option-current' : 'combo-option';
optionEl.setAttribute('aria-selected', 'false');
optionEl.innerText = option;
optionEl.addEventListener('click', (event) => {
event.stopPropagation();
this.onOptionClick(index);
});
optionEl.addEventListener('mousedown', this.onOptionMouseDown.bind(this));
this.listboxEl.appendChild(optionEl);
});
}
NoInput.prototype.onComboKeyDown = function(event) {
const { key } = event;
const max = this.options.length - 1;
const action = getActionFromKey(key, this.open);
switch(action) {
case MenuActions.Next:
case MenuActions.Last:
case MenuActions.First:
case MenuActions.Previous:
event.preventDefault();
return this.onOptionChange(getUpdatedIndex(this.activeIndex, max, action));
case MenuActions.CloseSelect:
case MenuActions.Space:
event.preventDefault();
this.selectOption(this.activeIndex);
case MenuActions.Close:
event.preventDefault();
return this.updateMenuState(false);
case MenuActions.Type:
this.updateMenuState(true);
return this.onOptionChange(Math.max(0, getIndexByLetter(this.options, key)));
case MenuActions.Open:
event.preventDefault();
return this.updateMenuState(true);
}
}
NoInput.prototype.onComboBlur = function() {
if (this.ignoreBlur) {
this.ignoreBlur = false;
return;
}
if (this.open) {
this.updateMenuState(false, false);
}
}
NoInput.prototype.onOptionChange = function(index) {
this.activeIndex = index;
this.comboEl.setAttribute('aria-activedescendant', `${this.idBase}-${index}`);
// update active style
const options = this.el.querySelectorAll('[role=option]');
[...options].forEach((optionEl) => {
optionEl.classList.remove('option-current');
});
options[index].classList.add('option-current');
if (isScrollable(this.listboxEl)) {
maintainScrollVisibility(options[index], this.listboxEl);
}
}
NoInput.prototype.onOptionClick = function(index) {
this.onOptionChange(index);
this.selectOption(index);
this.updateMenuState(false);
}
NoInput.prototype.onOptionMouseDown = function() {
this.ignoreBlur = true;
}
NoInput.prototype.selectOption = function(index) {
const selected = this.options[index];
this.comboEl.innerHTML = selected;
this.activeIndex = index;
// update aria-selected
const options = this.el.querySelectorAll('[role=option]');
[...options].forEach((optionEl) => {
optionEl.setAttribute('aria-selected', 'false');
});
options[index].setAttribute('aria-selected', 'true');
}
NoInput.prototype.updateMenuState = function(open, callFocus = true) {
this.open = open;
this.comboEl.setAttribute('aria-expanded', `${open}`);
open ? this.el.classList.add('open') : this.el.classList.remove('open');
callFocus && this.comboEl.focus();
}
// init select
const selectEl = document.querySelector('.js-select');
const selectComponent = new NoInput(selectEl, options);
selectComponent.init();
/*
* Popup button (mac pattern) code
*/
const PopupButton = function(el, options) {
// element refs
this.el = el;
this.buttonEl = el.querySelector('button');
this.listboxEl = el.querySelector('[role=listbox]');
// data
this.idBase = this.buttonEl.id;
this.options = options;
// state
this.activeIndex = 0;
this.open = false;
}
PopupButton.prototype.init = function() {
this.buttonEl.addEventListener('blur', this.onComboBlur.bind(this));
this.buttonEl.addEventListener('click', () => this.updateMenuState(true));
this.buttonEl.addEventListener('keydown', this.onButtonKeyDown.bind(this));
this.listboxEl.addEventListener('blur', this.onComboBlur.bind(this));
this.listboxEl.addEventListener('keydown', this.onListboxKeyDown.bind(this));
this.options.map((option, index) => {
const optionEl = document.createElement('div');
optionEl.setAttribute('role', 'option');
optionEl.id = `${this.idBase}-${index}`;
optionEl.className = index === 0 ? 'combo-option option-current' : 'combo-option';
optionEl.setAttribute('aria-selected', 'false');
optionEl.innerText = option;
optionEl.addEventListener('click', () => { this.onOptionClick(index); });
optionEl.addEventListener('mousedown', this.onOptionMouseDown.bind(this));
this.listboxEl.appendChild(optionEl);
});
}
PopupButton.prototype.onButtonKeyDown = function(event) {
const { key } = event;
const action = getActionFromKey(key, this.open);
switch(action) {
case MenuActions.Close:
event.preventDefault();
return this.updateMenuState(false);
case MenuActions.Type:
this.onOptionChange(Math.max(0, getIndexByLetter(this.options, key)));
case MenuActions.Open:
return this.updateMenuState(true);
}
}
PopupButton.prototype.onComboBlur = function() {
if (this.ignoreBlur) {
this.ignoreBlur = false;
return;
}
this.updateMenuState(false, false);
}
PopupButton.prototype.onListboxKeyDown = function(event) {
const { key } = event;
const max = this.options.length - 1;
const action = getActionFromKey(key, this.open);
switch(action) {
case MenuActions.Next:
case MenuActions.Last:
case MenuActions.First:
case MenuActions.Previous:
event.preventDefault();
return this.onOptionChange(getUpdatedIndex(this.activeIndex, max, action));
case MenuActions.CloseSelect:
this.selectOption(this.activeIndex);
case MenuActions.Close:
event.preventDefault();
return this.updateMenuState(false);
case MenuActions.Type:
this.onOptionChange(Math.max(0, getIndexByLetter(this.options, key)));
break;
}
}
PopupButton.prototype.onOptionChange = function(index) {
this.activeIndex = index;
this.listboxEl.setAttribute('aria-activedescendant', `${this.idBase}-${index}`);
// update active style
const options = this.el.querySelectorAll('[role=option]');
[...options].forEach((optionEl) => {
optionEl.classList.remove('option-current');
});
options[index].classList.add('option-current');
if (this.open && isScrollable(this.listboxEl)) {
maintainScrollVisibility(options[index], this.listboxEl);
}
}
PopupButton.prototype.onOptionClick = function(index) {
this.onOptionChange(index);
this.selectOption(index);
this.updateMenuState(false);
}
PopupButton.prototype.onOptionMouseDown = function() {
this.ignoreBlur = true;
}
PopupButton.prototype.selectOption = function(index) {
const selected = this.options[index];
this.buttonEl.innerHTML = selected;
this.activeIndex = index;
// update aria-selected
const options = this.el.querySelectorAll('[role=option]');
[...options].forEach((optionEl) => {
optionEl.setAttribute('aria-selected', 'false');
});
options[index].setAttribute('aria-selected', 'true');
}
PopupButton.prototype.updateMenuState = function(open, callFocus = true) {
this.open = open;
this.buttonEl.setAttribute('aria-expanded', `${open}`);
open ? this.el.classList.add('open') : this.el.classList.remove('open');
const focusEl = open ? this.listboxEl : this.buttonEl;
this.ignoreBlur = callFocus;
callFocus && focusEl.focus();
}
// init combo
const popupEl = document.querySelector('.js-popup-button');
const popupButtonComponent = new PopupButton(popupEl, options);
popupButtonComponent.init();
/*
* Expanding Listbox code
*/
const ListboxDropdown = function(el, options) {
// element refs
this.el = el;
this.listboxEl = el.querySelector('[role=listbox]');
this.valueEl = el.querySelector('.combo-input');
this.optionsEl = el.querySelector('.combo-menu');
// data
this.idBase = this.listboxEl.id;
this.options = options;
// state
this.activeIndex = 0;
this.open = false;
}
ListboxDropdown.prototype.init = function() {
this.listboxEl.addEventListener('blur', this.onListboxBlur.bind(this));
this.listboxEl.addEventListener('keydown', this.onListboxKeyDown.bind(this));
this.valueEl.addEventListener('click', () => this.updateMenuState(true));
this.options.map((option, index) => {
const optionEl = document.createElement('div');
optionEl.setAttribute('role', 'option');
optionEl.id = `${this.idBase}-${index}`;
optionEl.className = index === 0 ? 'combo-option option-current' : 'combo-option';
optionEl.setAttribute('aria-selected', 'false');
optionEl.innerText = option;
optionEl.addEventListener('click', () => { this.onOptionClick(index); });
optionEl.addEventListener('mousedown', this.onOptionMouseDown.bind(this));
this.optionsEl.appendChild(optionEl);
});
}
ListboxDropdown.prototype.onListboxBlur = function() {
if (this.ignoreBlur) {
this.ignoreBlur = false;
return;
}
this.updateMenuState(false);
}
ListboxDropdown.prototype.onListboxKeyDown = function(event) {
const { key } = event;
const max = this.options.length - 1;
const action = getActionFromKey(key, this.open);
switch(action) {
case MenuActions.Open:
return this.updateMenuState(true);
case MenuActions.Next:
case MenuActions.Last:
case MenuActions.First:
case MenuActions.Previous:
event.preventDefault();
return this.onOptionChange(getUpdatedIndex(this.activeIndex, max, action));
case MenuActions.CloseSelect:
this.selectOption(this.activeIndex);
case MenuActions.Close:
event.preventDefault();
return this.updateMenuState(false);
case MenuActions.Type:
this.onOptionChange(Math.max(0, getIndexByLetter(this.options, key)));
break;
}
}
ListboxDropdown.prototype.onOptionChange = function(index) {
this.activeIndex = index;
this.listboxEl.setAttribute('aria-activedescendant', `${this.idBase}-${index}`);
// update active style
const options = this.el.querySelectorAll('[role=option]');
[...options].forEach((optionEl) => {
optionEl.classList.remove('option-current');
});
options[index].classList.add('option-current');
if (this.open && isScrollable(this.optionsEl)) {
maintainScrollVisibility(options[index], this.optionsEl);
}
}
ListboxDropdown.prototype.onOptionClick = function(index) {
this.onOptionChange(index);
this.selectOption(index);
this.updateMenuState(false);
}
ListboxDropdown.prototype.onOptionMouseDown = function() {
this.ignoreBlur = true;
}
ListboxDropdown.prototype.selectOption = function(index) {
const selected = this.options[index];
this.valueEl.innerHTML = selected;
this.activeIndex = index;
// update aria-selected
const options = this.el.querySelectorAll('[role=option]');
[...options].forEach((optionEl) => {
optionEl.setAttribute('aria-selected', 'false');
});
options[index].setAttribute('aria-selected', 'true');
}
ListboxDropdown.prototype.updateMenuState = function(open) {
this.open = open;
this.listboxEl.setAttribute('aria-expanded', `${open}`);
open ? this.el.classList.add('open') : this.el.classList.remove('open');
}
// init combo
const listboxEl = document.querySelector('.js-listbox');
const listboxComponent = new ListboxDropdown(listboxEl, options);
listboxComponent.init();
Also see: Tab Triggers