Skip to content

Instantly share code, notes, and snippets.

@Tiny-Giant
Created September 24, 2019 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tiny-Giant/c091f8fd9aaf3fda4dc659462114afb4 to your computer and use it in GitHub Desktop.
Save Tiny-Giant/c091f8fd9aaf3fda4dc659462114afb4 to your computer and use it in GitHub Desktop.
JS BinModal with event-driven paradigm// source https://jsbin.com/pahakiq
html, body {
margin: 0;
height: 100%;
}
modal-wrapper {
position: absolute;
height: 100%;
width: 100%;
top: 0;
left: 0;
padding: 25px;
box-sizing: border-box;
background: #000000BB;
white-space: nowrap;
font-size: 0;
}
modal-wrapper::before {
content: '';
height: 100%;
}
modal-wrapper::before,
modal-dialog {
display: inline-block;
vertical-align: middle;
}
modal-dialog {
position: relative;
width: 100%;
padding: 20px;
box-sizing: border-box;
background: #FFFFFF;
white-space: initial;
font-size: 12pt;
max-height: 100%;
overflow: auto;
}
modal-close {
position: absolute;
top: 0;
right: 0;
padding: 0px 7px;
border-bottom: 1px solid #BBBBBB;
border-left: 1px solid #BBBBBB;
border-bottom-left-radius: 5px;
}
modal-close:hover {
cursor: pointer;
background: red;
color: white;
}
class LoadEvent extends Event {
constructor(type = 'load', { bubbles = true, ...rest } = {}) {
super(type, { bubbles, ...rest })
}
}
class OpenEvent extends Event {
constructor(type = 'open', { bubbles = true, ...rest } = {}) {
super(type, { bubbles, ...rest })
}
}
class CloseEvent extends Event {
constructor(type = 'close', { bubbles = true, ...rest } = {}) {
super(type, { bubbles, ...rest })
}
}
class ModalDialog extends HTMLElement {
constructor() {
super()
setTimeout(_ => this.init(), 0)
}
async init() {
const loaded = Promise.all([
new Promise(resolve => {
this.close_button = document.createElement('modal-close')
this.close_button.addEventListener('load', _ => {
this.appendChild(this.close_button)
resolve(true)
})
}),
new Promise(resolve => {
this.content = document.createElement('modal-content')
this.content.addEventListener('load', _ => {
this.appendChild(this.content)
resolve(true)
})
})
])
this.addEventListener('click', event => {
event.stopPropagation()
})
await loaded
this.dispatchEvent(new LoadEvent())
}
open() {
this.dispatchEvent(new OpenEvent())
}
close() {
this.dispatchEvent(new CloseEvent())
}
}
customElements.define('modal-dialog', ModalDialog)
class ModalContent extends HTMLElement {
constructor() {
super()
setTimeout(_ => this.init(), 0)
}
async init() {
this.dispatchEvent(new LoadEvent())
}
}
customElements.define('modal-content', ModalContent)
class ModalWrapper extends HTMLElement {
constructor() {
super()
setTimeout(_ => this.init(),0)
}
async init() {
this.wrapper = this
const loaded = Promise.all([
new Promise(resolve => {
this.dialog = document.createElement('modal-dialog')
this.dialog.addEventListener('load', _ => {
this.appendChild(this.dialog)
resolve(true)
})
})
])
this.addEventListener('click', event => this.close(event))
this.addEventListener('close', event => {
document.body.removeChild(this)
})
this.addEventListener('open', event => {
document.body.appendChild(this)
})
await loaded
this.dispatchEvent(new LoadEvent())
}
open() {
this.dispatchEvent(new OpenEvent())
}
close() {
this.dispatchEvent(new CloseEvent())
}
}
customElements.define('modal-wrapper', ModalWrapper)
class ModalCloseButton extends HTMLElement {
constructor() {
super()
setTimeout(_ => this.init(),0)
}
init() {
this.textContent = '×'
this.addEventListener('click', event => this.dispatchEvent(new CloseEvent()))
this.dispatchEvent(new LoadEvent())
}
}
customElements.define('modal-close', ModalCloseButton)
const modal1 = document.createElement('modal-wrapper');
modal1.addEventListener('load', event => {
modal1.dialog.content.innerHTML = `
<h1>What is Lorem Ipsum?</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<h1>Why do we use it?</h1>
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
<h1>Where does it come from?</h1>
<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</P>
<p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>
`
const button = document.createElement('button')
button.textContent = 'Open Modal 1'
button.addEventListener('click', _ => modal1.open())
document.body.appendChild(button)
})
const modal2 = document.createElement('modal-wrapper');
modal2.addEventListener('load', event => {
modal2.dialog.content.textContent = 'Hello World 2!'
const button = document.createElement('button')
button.textContent = 'Open Modal 2'
button.addEventListener('click', _ => modal2.open())
document.body.appendChild(button)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment