|
| 1 | +import {ModalBackground} from './ModalBackground' |
| 2 | +import {IComponent} from "vanilla-typescript" |
| 3 | +import {constants} from "./constants" |
| 4 | +import './SolarPopup.pcss' |
| 5 | + |
| 6 | +/** |
| 7 | + * A Popup that can take any content |
| 8 | + * |
| 9 | + * Features |
| 10 | + * Closes in response to [ESC] keypress, submit events. |
| 11 | + * Adds modal background that fades in with CSS3 transitions |
| 12 | + * Popup itself slides in with CSS3 transitions |
| 13 | + * |
| 14 | + * @constructor |
| 15 | + */ |
| 16 | +export default class SolarPopup implements IComponent { |
| 17 | + destroyBoundWithThis = this.destroy.bind(this) |
| 18 | + modalBackground = new ModalBackground() |
| 19 | + private hostElement: HTMLElement |
| 20 | + private child: HTMLElement |
| 21 | + |
| 22 | + constructor(child: HTMLElement) { |
| 23 | + this.child = child |
| 24 | + } |
| 25 | + |
| 26 | + |
| 27 | + /** |
| 28 | + * Shows |
| 29 | + * @param {Element} child we need to keep the reference to keep custom functionality in the child |
| 30 | + */ |
| 31 | + show() { |
| 32 | + |
| 33 | + const tempElement:HTMLElement = document.createElement('DIV') |
| 34 | + |
| 35 | + tempElement.innerHTML = |
| 36 | + `<article class='solar-popup' data-is-initialising="true"> |
| 37 | + <a class='close'><!--❌-->✖</a> |
| 38 | + <div class="childContainer"></div> |
| 39 | + </article>` |
| 40 | + tempElement.querySelector(".childContainer").appendChild(this.child) |
| 41 | + |
| 42 | + this.hostElement = <HTMLElement>tempElement.firstChild |
| 43 | + document.body.appendChild(this.hostElement) |
| 44 | + let currentWidth = window.getComputedStyle(document.querySelector("p")) |
| 45 | + |
| 46 | + this.modalBackground.render() |
| 47 | + |
| 48 | + setTimeout(() => { |
| 49 | + //this triggers a css change |
| 50 | + //let currentWidth = window.getComputedStyle(document.querySelector("p")) }, 50) |
| 51 | + this.hostElement.dataset["isInitialising"] = "false" |
| 52 | + }) |
| 53 | + this.addListeners() |
| 54 | + } |
| 55 | + |
| 56 | + addListeners() { |
| 57 | + const closeElement = this.hostElement.querySelector('a') |
| 58 | + closeElement.addEventListener('click', this.destroyBoundWithThis) |
| 59 | + this.hostElement.classList.remove('offscreen') |
| 60 | + |
| 61 | + document.addEventListener('keyup', function (event) { |
| 62 | + if (event.keyCode === constants.KEYS.ESC) { |
| 63 | + this.destroyBoundWithThis() |
| 64 | + } |
| 65 | + }.bind(this)) |
| 66 | + |
| 67 | + this.hostElement.addEventListener('submit', function (event) { |
| 68 | + this.destroyBoundWithThis() |
| 69 | + event.preventDefault() |
| 70 | + }.bind(this)) |
| 71 | + |
| 72 | + |
| 73 | + // handle the first child submit button click, close popup by default |
| 74 | + // this is a convention that gets popup to behave in sensible way |
| 75 | + const submitBtn = this.hostElement.querySelector('button[type="submit"]') |
| 76 | + if (submitBtn) { |
| 77 | + submitBtn.addEventListener("click", this.destroyBoundWithThis) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + destroy() { |
| 82 | + //vistual indicator for this element and delegate to the modal |
| 83 | + this.hostElement.dataset["isDestructing"] = "true" |
| 84 | + this.modalBackground.destroy() |
| 85 | + setTimeout(function () { |
| 86 | + this.hostElement.parentElement.removeChild(this.hostElement) |
| 87 | + }.bind(this), constants.TRANSITION_TIMES) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | + |
0 commit comments