Sha256: 71065a6a1a9aeccd210febcc87deb480728221898e3e741b6ffa3b5be44f2873

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

import React from "react";
import ModalStore from "./ModalStore";

export default class Modal extends React.Component {
  constructor(props) {
    super(props);
    this.state = { component: null };
    this.store = ModalStore;
    this.closeModal = this.closeModal.bind(this);
    this.handleChange = this.handleChange.bind(this);
    this.handleKeypress = this.handleKeypress.bind(this);
  }

  componentDidMount() {
    this.unsubscribe = this.store.subscribe(this.handleChange);
    window.addEventListener("keypress", this.handleKeypress);
  }

  componentWillUnmount() {
    this.unsubscribe();
    window.removeEventListener("keypress", this.handleKeypress);
  }

  closeModal(evt) {
    evt.stopPropagation();
    evt.preventDefault();
    ModalStore.dispatch({ type: "CLOSE" });
  }

  handleChange() {
    this.setState({ ...this.store.getState() });
  }

  handleKeypress(evt) {
    if (this.state.component && (evt.key == "Escape" || evt.keyCode === 27)) {
      this.closeModal(evt);
    }
  }

  render() {
    let component = this.state.component;

    if (component) {
      document.body.classList.add("modal");
    } else {
      document.body.classList.remove("modal");
      return (<div className="modal-wrapper"></div>);
    }

    return (
      <div className="modal-wrapper open">
        <div className="background" onClick={this.closeModal} />
        <div className="modal">
          {component}
        </div>
      </div>
    );
  }
}

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pages_core-3.8.1 app/javascript/components/Modal.jsx
pages_core-3.8.0 app/javascript/components/Modal.jsx