Skip to content

Instantly share code, notes, and snippets.

@caglarorhan
Last active June 30, 2021 10:07
Show Gist options
  • Save caglarorhan/95cedfdeaa3dd53b766398c909040522 to your computer and use it in GitHub Desktop.
Save caglarorhan/95cedfdeaa3dd53b766398c909040522 to your computer and use it in GitHub Desktop.
Simple Modal Window Creator
.modal {
position: fixed;
z-index: 1;
padding-top: 150px;
left: 0;
top: 0;
width: 100vw;
height: 100vh;
overflow: auto;
background-color: rgba(0,0,0,0.5);
}
.modal .modal-content {
background-color: white;
margin: auto;
padding: 10px;
border: 1px solid grey;
border-radius: 8px;
}
const modalWindowCreator = ({id='test',width=500, height=500, unit='px', ingredient='This is a test modal!', display='none'}={})=>{
if(document.getElementById(id)){
return document.getElementById(id);
}
let theModal = document.createElement('div');
theModal.id = id;
theModal.className='modal';
theModal.style.display = display;
document.body.append(theModal);
//
let theModalContent = document.createElement('div');
theModalContent.className='modal-content';
theModalContent.style.cssText=`width:${width}${unit}; height:${height}${unit}`;
theModal.append(theModalContent);
//
window.addEventListener('click',(event)=>{
if(event.target.id===id){
theModal.switch('off')
}
})
//
theModalContent.innerHTML = ingredient;
//
theModal.switch=(position)=>{
if(!position){
theModal.style.display = (theModal.style.display==='none')?'block':'none';
}else{
theModal.style.display=(position==='on')?'block':'none';
}
}
//
theModal.destroy = ()=>{
theModal.remove();
}
return theModal;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple Modal</title>
<link rel="stylesheet" href="public/stylesheets/modal.css">
<script src="public/javascripts/modalWindowCreator.js"></script>
</head>
<body>
<button onclick="modalWindowCreator()">JUST CREATE</button>
<button onclick="modalWindowCreator({id:'testA'}).switch('on')">CREATE AND OPEN</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment