Skip to content

Instantly share code, notes, and snippets.

@mmintel
Last active June 21, 2018 08:43
Show Gist options
  • Select an option

  • Save mmintel/6da3ed56129933c064d9631b97dfe666 to your computer and use it in GitHub Desktop.

Select an option

Save mmintel/6da3ed56129933c064d9631b97dfe666 to your computer and use it in GitHub Desktop.
import View from './view';
export default class ModalView extends View {
constructor(controller, options) {
super(controller);
this.node = options.node;
this.overlay = options.overlay;
this.triggers = options.triggers;
this.init();
}
init() {
super.init();
this.addEventListeners();
}
update = (data) => {
if (data.isOpen) {
this.open();
} else {
this.close();
}
}
open() {
this.node.classList.add('is-open');
this.overlay.classList.add('is-open');
}
close() {
this.node.classList.remove('is-open');
this.overlay.classList.remove('is-open');
this.node.classList.add('is-closed');
this.overlay.classList.add('is-closed');
}
handleOverlayClick = (e) => {
if (e.target !== e.currentTarget) return;
this.controller.close();
}
addEventListeners = () => {
this.triggers.open.forEach(node => node.addEventListener('click', this.controller.open));
this.triggers.close.forEach(node => node.addEventListener('click', this.controller.close));
document.addEventListener('keydown', this.handleKeypress);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment