JSFiddle - React, Tailwind, and code Playground
by jleyva
HTML
<a href="#" id="openDialog">Click to open modal window</a>
<a href="#" id="openDialog2">Click to open modal window 2</a>
<div id="app-dialog">
<div>
<div class="modalHeader">
</div>
<div class="modalContent">
</div>
<div class="modalFooter">
</div>
<div class="modalClear"></div>
</div>
</div>
CSS
#app-dialog {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(0,0,0,0.2);
z-index: 99999;
opacity:0;
-webkit-transition: opacity 400ms ease-in;
-moz-transition: opacity 400ms ease-in;
transition: opacity 400ms ease-in;
pointer-events: none;
}
#app-dialog > div {
width: auto;
position: relative;
margin: 10% auto;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
background: #fa8f1c;
}
#app-dialog .modalHeader {
padding: 10px;
font-size: 1em;
color: #fff;
}
#app-dialog .modalContent {
background: #fff;
padding: 5px;
margin-left: 1px;
margin-right: 1px;
text-align: center;
}
#app-dialog .modalFooter {
background: #fff;
padding: 2px;
margin-left: 1px;
margin-right: 1px;
text-align: center;
-webkit-border-radius: 0 0 5px 5px;
-moz-border-radius: 0 0 5px 5px;
border-radius: 0 0 5px 5px;
}
#app-dialog .modalFooter button{
}
#app-dialog .modalClear {
float:none;
clear: both;
}
button {
padding-left: 13pt;
padding-right: 13pt;
background: -webkit-linear-gradient(top, #FEFEFE, #E7E7E7);
border-radius: 4pt;
border: 1pt solid #CCC;
border-bottom: #B3B3B3;
height: 30pt;
width: 40%;
}
JavaScript
$("#openDialog").on('click', function(e) {
var options = {
title: "Mi titulo",
autoclose: 0,
buttons: {}
};
options.buttons['Yes'] = function() { alert("Yai"); };
options.buttons['No'] = dialogClose;
dialog('hola', options);
});
$("#openDialog2").on('click', function(e) {
var options = {
title: "Mi titulo",
autoclose: 0,
buttons: {}
};
options.buttons['Close'] = dialogClose;
dialog('hola', options);
});
$(".cancel").on('click', function(e) {
dialogClose();
});
function dialog(text, options) {
if (!options) {
options = {
title: "",
autoclose: 0,
buttons: {
'close': dialogClose
}
}
}
if (!options.buttons) {
options.buttons = {};
options.buttons['close'] = dialogClose;
}
$("#app-dialog .modalHeader").html(options.title);
$("#app-dialog .modalContent").html(text);
if (options.buttons) {
var buttons = "";
var els = 0;
$.each(options.buttons, function (key, value) {
buttons += "<button class=\"modal-button-" + els + "\" style=\"width: _width_%\"> " + key + " </button>"
els++;
});
var width = 100 / els;
buttons = buttons.replace(/_width_/ig, width);
$("#app-dialog .modalFooter").html(buttons);
// Handlers for buttons.
els = 0;
$.each(options.buttons, function (key, value) {
$(".modal-button-" + els).click(function(e) {
value();
});
els++;
});
}
// Show the div.
$("#app-dialog").css('opacity', '1');
// Allow mouse events in this div.
$("#app-dialog").css('pointer-events', 'auto');
if (options.autoclose) {
setTimeout(function() {
dialogClose();
}, options.autoclose);
}
}
function dialogClose() {
// Hide the div.
...