2021-12-17 22:26:52 +01:00
|
|
|
import { ClassNames } from '../interfaces/class-names';
|
|
|
|
import { PassedElementType } from '../interfaces/passed-element-type';
|
2019-11-03 14:18:16 +01:00
|
|
|
|
2017-08-16 13:40:09 +02:00
|
|
|
export default class Dropdown {
|
2019-12-23 19:22:54 +01:00
|
|
|
element: HTMLElement;
|
2021-12-17 22:26:52 +01:00
|
|
|
|
|
|
|
type: PassedElementType;
|
|
|
|
|
2019-12-23 19:22:54 +01:00
|
|
|
classNames: ClassNames;
|
2021-12-17 22:26:52 +01:00
|
|
|
|
2019-12-23 19:22:54 +01:00
|
|
|
isActive: boolean;
|
|
|
|
|
|
|
|
constructor({
|
|
|
|
element,
|
|
|
|
type,
|
|
|
|
classNames,
|
|
|
|
}: {
|
|
|
|
element: HTMLElement;
|
2021-12-17 22:26:52 +01:00
|
|
|
type: PassedElementType;
|
2019-12-23 19:22:54 +01:00
|
|
|
classNames: ClassNames;
|
|
|
|
}) {
|
2019-11-03 14:18:16 +01:00
|
|
|
this.element = element;
|
|
|
|
this.classNames = classNames;
|
|
|
|
this.type = type;
|
2017-08-16 17:59:10 +02:00
|
|
|
this.isActive = false;
|
|
|
|
}
|
|
|
|
|
2017-08-29 13:56:54 +02:00
|
|
|
/**
|
2019-10-30 18:28:15 +01:00
|
|
|
* Bottom position of dropdown in viewport coordinates
|
2017-08-29 13:56:54 +02:00
|
|
|
*/
|
2019-12-23 19:22:54 +01:00
|
|
|
get distanceFromTopWindow(): number {
|
2019-10-30 18:28:15 +01:00
|
|
|
return this.element.getBoundingClientRect().bottom;
|
2017-08-16 13:40:09 +02:00
|
|
|
}
|
|
|
|
|
2019-12-23 19:22:54 +01:00
|
|
|
getChild(selector: string): HTMLElement | null {
|
2017-08-29 13:56:54 +02:00
|
|
|
return this.element.querySelector(selector);
|
2017-08-16 13:40:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show dropdown to user by adding active state class
|
|
|
|
*/
|
2019-12-23 19:22:54 +01:00
|
|
|
show(): this {
|
2017-08-16 14:01:17 +02:00
|
|
|
this.element.classList.add(this.classNames.activeState);
|
|
|
|
this.element.setAttribute('aria-expanded', 'true');
|
2017-08-16 13:40:09 +02:00
|
|
|
this.isActive = true;
|
2019-10-29 19:26:11 +01:00
|
|
|
|
2018-05-21 18:01:03 +02:00
|
|
|
return this;
|
2017-08-16 13:40:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hide dropdown from user
|
|
|
|
*/
|
2019-12-23 19:22:54 +01:00
|
|
|
hide(): this {
|
2017-08-16 14:01:17 +02:00
|
|
|
this.element.classList.remove(this.classNames.activeState);
|
2017-08-16 13:40:09 +02:00
|
|
|
this.element.setAttribute('aria-expanded', 'false');
|
|
|
|
this.isActive = false;
|
2019-10-29 19:26:11 +01:00
|
|
|
|
2018-05-21 18:01:03 +02:00
|
|
|
return this;
|
2017-08-16 13:40:09 +02:00
|
|
|
}
|
|
|
|
}
|