Add modal utility

pull/261/head
Cory LaViska 2020-10-15 13:54:20 -04:00
rodzic 494ed99b36
commit df0101cad2
1 zmienionych plików z 42 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,42 @@
export interface ModalOptions {
onFocusOut?: (event: Event) => any;
}
let activeModals: HTMLElement[] = [];
export default class Modal {
element: HTMLElement;
options: ModalOptions;
constructor(element: HTMLElement, options?: ModalOptions) {
this.element = element;
this.options = options;
this.handleFocusIn = this.handleFocusIn.bind(this);
}
activate() {
activeModals.push(this.element);
document.addEventListener('focusin', this.handleFocusIn);
}
deactivate() {
activeModals = activeModals.filter(modal => modal !== this.element);
document.removeEventListener('focusin', this.handleFocusIn);
}
isActive() {
// The "active" modal is always the most recent one shown
return activeModals[activeModals.length - 1] === this.element;
}
handleFocusIn(event: Event) {
console.log(this.element);
const target = event.target as HTMLElement;
const tagName = this.element.tagName.toLowerCase();
// If focus is lost while the modal is active, run the onFocusOut callback
if (this.isActive() && target.closest(tagName) !== this.element && typeof this.options.onFocusOut === 'function') {
this.options.onFocusOut(event);
}
}
}