Choices/src/scripts/components/dropdown.js

62 lines
1.4 KiB
JavaScript
Raw Normal View History

/**
* @typedef {import('../../../types/index').Choices.passedElement} passedElement
* @typedef {import('../../../types/index').Choices.ClassNames} ClassNames
*/
export default class Dropdown {
/**
* @param {{
* element: HTMLElement,
* type: passedElement['type'],
* classNames: ClassNames,
* }} args
*/
2018-05-21 18:01:03 +02:00
constructor({ element, type, classNames }) {
this.element = element;
this.classNames = classNames;
this.type = type;
this.isActive = false;
}
2017-08-29 13:56:54 +02:00
/**
* Bottom position of dropdown in viewport coordinates
* @returns {number} Vertical position
2017-08-29 13:56:54 +02:00
*/
get distanceFromTopWindow() {
return this.element.getBoundingClientRect().bottom;
}
2017-08-29 13:56:54 +02:00
/**
* Find element that matches passed selector
* @param {string} selector
* @returns {HTMLElement | null}
2017-08-29 13:56:54 +02:00
*/
getChild(selector) {
return this.element.querySelector(selector);
}
/**
* Show dropdown to user by adding active state class
* @returns {this}
*/
show() {
2017-08-16 14:01:17 +02:00
this.element.classList.add(this.classNames.activeState);
this.element.setAttribute('aria-expanded', 'true');
this.isActive = true;
2018-05-21 18:01:03 +02:00
return this;
}
/**
* Hide dropdown from user
* @returns {this}
*/
hide() {
2017-08-16 14:01:17 +02:00
this.element.classList.remove(this.classNames.activeState);
this.element.setAttribute('aria-expanded', 'false');
this.isActive = false;
2018-05-21 18:01:03 +02:00
return this;
}
}