Choices/src/scripts/components/dropdown.ts

61 lines
1.3 KiB
TypeScript
Raw Normal View History

import { ClassNames } from '../interfaces/class-names';
import { PassedElementType } from '../interfaces/passed-element-type';
export default class Dropdown {
element: HTMLElement;
type: PassedElementType;
classNames: ClassNames;
isActive: boolean;
constructor({
element,
type,
classNames,
}: {
element: HTMLElement;
type: PassedElementType;
classNames: 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
2017-08-29 13:56:54 +02:00
*/
get distanceFromTopWindow(): number {
return this.element.getBoundingClientRect().bottom;
}
getChild(selector: string): HTMLElement | null {
2017-08-29 13:56:54 +02:00
return this.element.querySelector(selector);
}
/**
* Show dropdown to user by adding active state class
*/
show(): this {
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
*/
hide(): this {
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;
}
}