diff --git a/README.md b/README.md index ce58fb2..bef60a8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Vanilla JS and CSS Pop-up library -[![MIT license](https://img.shields.io/badge/License-MIT-blue.svg?style=for-the-badge)](https://lbesson.mit-license.org/)  [![GitHub release](https://img.shields.io/github/release/nimajneBG/Pop-up-Library.svg?style=for-the-badge)](https://github.com/nimajneBG/Pop-up-Library) +[![MIT license](https://img.shields.io/badge/License-MIT-blue.svg?style=for-the-badge)](https://lbesson.mit-license.org/)  [![GitHub release](https://img.shields.io/github/release/nimajneBG/Pop-up-Library.svg?style=for-the-badge)](https://github.com/nimajneBG/Pop-up-Library)  ![Using JS](https://img.shields.io/badge/Unsing-JS-green?style=for-the-badge)  ![Using CSS](https://img.shields.io/badge/Unsing-CSS-green?style=for-the-badge) Library for pop-ups and toasts that look much nicer than `alert()`. @@ -12,6 +12,8 @@ You don't need any special HTML, because the required HTML is generated automati ## Usage ### Include the library + +**Normal files** ```HTML @@ -20,11 +22,28 @@ You don't need any special HTML, because the required HTML is generated automati ``` +**Minified files** +```HTML + + + + + +``` + ### Use the predefined functions for pop-ups ```JavaScript popUpInfo("Message"); //Displays an information pop-up with an OK button and a close button. popUpError("Error message"); //Displays an error pop-up with an OK button and a close button. -popUpConfirm("Question"); +popUpConfirm("Question").then((result) => { + if (result == "ok") { + //What should happen if ok was pressed + } else if (result == "cancel") { + //What should happen if cancel was pressed + } +}).catch((err) => { + console.error(err); //What should happen if something goes wrong +}); /*Displays an confirm pop-up with an OK button and a cancel button. Returns a promise which gets fulfilled when the user clicks one of the buttons. The promise returns "ok" or "cancel".*/ @@ -34,17 +53,17 @@ The promise returns "ok" or "cancel".*/ ```HTML @@ -62,13 +81,14 @@ The promise returns "ok" or "cancel".*/ ```HTML diff --git a/pop-up.js b/pop-up.js index 97b873d..c6517d5 100644 --- a/pop-up.js +++ b/pop-up.js @@ -25,9 +25,8 @@ class PopUp { create() { // Error detection - if (this.input.ok != true && this.input.cancel != true && this.input.custom != true) { - console.error("At least one sort of button have to be selected."); - return 1; + if (!this.input.ok && !this.input.cancel && !this.input.custom) { + return console.error("At least one sort of button have to be selected."); } @@ -70,35 +69,33 @@ class PopUp { } + createButton(name, text) { + this[name] = document.createElement("BUTTON"); + this[name].innerHTML = text; + this[name].classList.add("standard-button"); + this.buttonLine.appendChild(this[name]); + } + createButtons() { // Create buttons - var buttonLine = document.createElement("DIV"); - buttonLine.classList.add("button-line"); - this.popUp.appendChild(buttonLine); + this.buttonLine = document.createElement("DIV"); + this.buttonLine.classList.add("button-line"); + this.popUp.appendChild(this.buttonLine); // OK Button if (this.input.ok) { - this.btnOk = document.createElement("BUTTON"); - this.btnOk.innerHTML = "OK"; - this.btnOk.classList.add("standard-button"); - buttonLine.appendChild(this.btnOk); + this.createButton('btnOk', 'OK'); } // Cancel Button if (this.input.cancel) { - this.btnCancel = document.createElement("BUTTON"); - this.btnCancel.innerHTML = "Cancel"; - this.btnCancel.classList.add("standard-button"); - buttonLine.appendChild(this.btnCancel); + this.createButton('btnCancel', 'Cancel'); } // Custom Button if (this.input.custom) { - this.btnCustom = document.createElement("BUTTON"); - this.btnCustom.innerHTML = this.input.text; - this.btnCustom.classList.add("standard-button"); - buttonLine.appendChild(this.btnCustom); + this.createButton('btnCustom', this.input.text); } } @@ -113,44 +110,32 @@ class PopUp { } events() { - const bg = this.popUpBg; - const btnOk = this.btnOk; - const inBtnOk = this.input.ok; - const btnCancel = this.btnCancel; - const inBtnCancel = this.input.cancel; - const btnCustom = this.btnCustom; - const inBtnCustom = this.input.custom; - const closeX = this.closeX; - const inCloseX = this.input.close; - - var promise = new Promise(function (resolve, reject) { - if (inCloseX == true) { - closeX.onclick = () => { - bg.remove(); - } + let promise = new Promise((resolve, reject) => { + if (this.input.close == true) { + this.closeX.onclick = () => this.close(); } - if (inBtnOk == true) { - btnOk.onclick = () => { - bg.remove(); + if (this.input.ok) { + this.btnOk.onclick = () => { + this.close(); resolve("ok"); } } - if (inBtnCancel == true) { - btnCancel.onclick = () => { - bg.remove(); + if (this.input.cancel) { + this.btnCancel.onclick = () => { + this.close(); resolve("cancel"); } } - if (inBtnCustom == true) { - btnCustom.onclick = () => { - bg.remove(); + if (this.input.custom) { + this.btnCustom.onclick = () => { + this.close(); resolve("custom"); } } - if (inBtnCustom != true && inBtnOk != true && inBtnCancel != true) { + if (!this.input.custom && !this.input.ok && !this.input.cancel) { reject(Error("It broke")); } @@ -203,91 +188,77 @@ class Toast { this.toastText.innerHTML = text; } + createButton(name, text) { + this[name] = document.createElement("BUTTON"); + this[name].innerHTML = text; + this[name].classList.add("standard-button"); + this.buttonLine.appendChild(this[name]); + } + createButtons() { // Create buttons - var buttonLine = document.createElement("DIV"); - buttonLine.classList.add("button-line"); - this.toast.appendChild(buttonLine); + this.buttonLine = document.createElement("DIV"); + this.buttonLine.classList.add("button-line"); + this.toast.appendChild(this.buttonLine); // OK Button if (this.input.ok) { - this.btnOk = document.createElement("BUTTON"); - this.btnOk.innerHTML = "OK"; - this.btnOk.classList.add("standard-button"); - buttonLine.appendChild(this.btnOk); + this.createButton('btnOk', 'OK'); } // Cancel Button if (this.input.cancel) { - this.btnCancel = document.createElement("BUTTON"); - this.btnCancel.innerHTML = "Cancel"; - this.btnCancel.classList.add("standard-button"); - buttonLine.appendChild(this.btnCancel); + this.createButton('btnCancel', 'Cancel'); } // Custom Button if (this.input.custom) { - this.btnCustom = document.createElement("BUTTON"); - this.btnCustom.innerHTML = this.input.text; - this.btnCustom.classList.add("standard-button"); - buttonLine.appendChild(this.btnCustom); + this.createButton('btnCustom', this.input.text); } // Close x if (this.input.close == true) { this.closeX = document.createElement("i"); this.closeX.classList.add("cross"); - buttonLine.appendChild(this.closeX); + this.buttonLine.appendChild(this.closeX); } } events() { - const bg = this.toastBg; - const btnOk = this.btnOk; - const inBtnOk = this.input.ok; - const btnCancel = this.btnCancel; - const inBtnCancel = this.input.cancel; - const btnCustom = this.btnCustom; - const inBtnCustom = this.input.custom; - const closeX = this.closeX; - const inCloseX = this.input.close; - // Decay - if (this.input.decay && this.input.time != undefined) { - setTimeout(() => { - this.close() - }, this.input.time * 1000) + if (this.input.decay) { + if (typeof this.input.decay === 'number') { + setTimeout(() => this.close(), this.input.time * 1000) + } } // Buttons clicked - let promise = new Promise(function (resolve, reject) { - if (inCloseX == true) { - closeX.onclick = () => { - bg.remove(); - } + let promise = new Promise((resolve, reject) => { + if (this.input.close == true) { + this.closeX.onclick = () => this.close(); } - if (inBtnOk == true) { - btnOk.onclick = () => { - bg.remove(); + if (this.input.ok) { + this.btnOk.onclick = () => { + this.close(); resolve("ok"); } } - if (inBtnCancel == true) { - btnCancel.onclick = () => { - bg.remove(); + if (this.input.cancel) { + this.btnCancel.onclick = () => { + this.close(); resolve("cancel"); } } - if (inBtnCustom == true) { - btnCustom.onclick = () => { - bg.remove(); + if (this.input.custom) { + this.btnCustom.onclick = () => { + this.close(); resolve("custom"); } } - if (inBtnCustom != true && inBtnOk != true && inBtnCancel != true) { + if (!this.input.custom && !this.input.ok && !this.input.cancel) { reject(Error("It broke")); } @@ -311,7 +282,7 @@ function popUpInfo(message) { "close": true, "icon": "📣" }); - p.create().then(function (result) { }, function (err) { console.log(err); }); + p.create().then((result) => { }).catch((err) => console.log(err)); } function popUpError(message) { @@ -323,7 +294,7 @@ function popUpError(message) { "close": true, "icon": "🛑" }); - p.create().then(function (result) { }, function (err) { console.log(err); }); + p.create().then((result) => { }).catch((err) => console.log(err)); } function popUpConfirm(message) { diff --git a/pop-up.min.css b/pop-up.min.css new file mode 100644 index 0000000..938495b --- /dev/null +++ b/pop-up.min.css @@ -0,0 +1,12 @@ +/* +The MIT License (MIT) + +Copyright © 2020 Benjamin Grau & Gregor Parzefall + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ +:root{--popUpBg:rgba(26, 26, 26, 0.7);--popUpWindowBg:#fff;--popUpBtnLineColor:lightgrey;--popUpBtnBg:#3498db;--popUpBtnHoverBg:#2e80b7;--popUpBtnTextColor:#fff;--popUpTextColor:#000;--popUpCloseColor:lightgrey;--popUpCloseHoverColor:black;--shadow:#ccc}.pop-up-bg{position:fixed;display:flex;top:0;left:0;width:100%;height:100%;z-index:4;align-items:center;justify-content:center;background-color:var(--popUpBg)}.pop-up-bg~.pop-up-bg{display:none}.toast-bg{position:fixed;bottom:12px;left:0;width:100%;z-index:10}.toast-bg~.toast-bg{display:none}.pop-up,.toast{min-width:200px;height:auto;background-color:var(--popUpWindowBg);text-align:left;border-radius:2px;z-index:5;color:var(--popUpTextColor);font-family:Lato, -apple-system, BlinkMacSystemFont, system-ui, Segoe UI, Roboto, Helvetica, Arial, sans-serif;animation:zoom 0.2s}.pop-up{max-width:500px;max-height:300px;margin:15px !important;padding:10px 15px}.toast{margin:0 auto;box-shadow:4px 5px 8px 4px var(--shadow);max-width:80%;width:max-content;padding:8px;display:flex;flex-direction:row;align-items:center}.toast p.toast-text{flex-grow:1;display:block;margin:0 10px 0 0}.pop-up .button-line{text-align:right;border-top:1px solid var(--popUpBtnLineColor);padding-top:10px;margin-top:18px}.toast .button-line{width:max-content;display:flex;flex-direction:row;align-items:center}.pop-up .pop-up-icon{margin:0;font-size:48pt;text-align:center;user-select:none}.pop-up .standard-button,.toast .standard-button{font-family:inherit;padding:10px;font-size:12pt;border:1px solid var(--popUpBtnBg);cursor:pointer;color:var(--popUpBtnTextColor);background-color:var(--popUpBtnBg);border-radius:2px;outline:none;transition:background-color 0.2s, border-color 0.2s;margin-left:5px}.pop-up .standard-button:hover,.toast .standard-button:hover{background-color:var(--popUpBtnHoverBg);border-color:var(--popUpBtnHoverBg)}.pop-up div.popup-close{text-align:right;float:right;margin:5px 5px 0 0}.pop-up div.popup-close,.toast i.cross{cursor:pointer;transition:transform 0.2s, color 0.2s;color:var(--popUpCloseColor)}.pop-up div.popup-close i.cross,.toast i.cross{width:1em;height:1em;background:linear-gradient(45deg, transparent 45%, currentColor 45%, currentColor 55%, transparent 55%), linear-gradient(135deg, transparent 45%, currentColor 45%, currentColor 55%, transparent 55%)}.pop-up div.popup-close i.cross{display:inline-block}.toast i.cross{display:block;margin-left:5px}.pop-up div.popup-close:hover{transform:scale(1.1);color:var(--popUpCloseHoverColor)}@keyframes zoom{from{transform:scale(0.8)}to{transform:scale(1)}} \ No newline at end of file diff --git a/pop-up.min.js b/pop-up.min.js new file mode 100644 index 0000000..eb24c1c --- /dev/null +++ b/pop-up.min.js @@ -0,0 +1,12 @@ +/* +The MIT License (MIT) + +Copyright © 2020 Benjamin Grau & Gregor Parzefall + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ +class PopUp{constructor(args){this.input=args;this.popUpBg;this.popUp;this.btnOk;this.btnCancel;this.btnCustom;this.closeX}create(){if(!this.input.ok&&!this.input.cancel&&!this.input.custom){return console.error("At least one sort of button have to be selected.")}this.popUpBg=document.createElement("DIV");this.popUpBg.classList.add("pop-up-bg");document.body.appendChild(this.popUpBg);this.popUp=document.createElement("DIV");this.popUp.classList.add("pop-up");this.popUpBg.appendChild(this.popUp);if(this.input.close==true){this.closeX=document.createElement("DIV");this.closeX.classList.add("popup-close");this.closeX.innerHTML='';this.popUp.appendChild(this.closeX)}if(this.input.icon!=false){var icon=document.createElement("P");icon.classList.add("pop-up-icon");icon.innerHTML=this.input.icon;this.popUp.appendChild(icon)}this.popUpText=document.createElement("P");this.popUpText.innerHTML=this.input.message;this.popUp.appendChild(this.popUpText);this.createButtons();return this.events()}createButton(name,text){this[name]=document.createElement("BUTTON");this[name].innerHTML=text;this[name].classList.add("standard-button");this.buttonLine.appendChild(this[name])}createButtons(){this.buttonLine=document.createElement("DIV");this.buttonLine.classList.add("button-line");this.popUp.appendChild(this.buttonLine);if(this.input.ok){this.createButton('btnOk','OK')}if(this.input.cancel){this.createButton('btnCancel','Cancel')}if(this.input.custom){this.createButton('btnCustom',this.input.text)}}close(){this.popUpBg.remove()}setText(text=String){this.input.message=text;this.popUpText.innerHTML=this.input.message}events(){let promise=new Promise((resolve,reject)=>{if(this.input.close==true){this.closeX.onclick=()=>this.close()}if(this.input.ok){this.btnOk.onclick=()=>{this.close();resolve("ok")}}if(this.input.cancel){this.btnCancel.onclick=()=>{this.close();resolve("cancel")}}if(this.input.custom){this.btnCustom.onclick=()=>{this.close();resolve("custom")}}if(!this.input.custom&&!this.input.ok&&!this.input.cancel){reject(Error("It broke"))}});return promise}}class Toast{constructor(args){this.input=args;this.toast}create(){this.toastBg=document.createElement('DIV');this.toastBg.classList.add('toast-bg');document.body.appendChild(this.toastBg);this.toast=document.createElement('DIV');this.toast.classList.add('toast');this.toastBg.appendChild(this.toast);this.toastText=document.createElement('P');this.toastText.classList.add('toast-text');this.toastText.innerHTML=this.input.message;this.toast.appendChild(this.toastText);this.createButtons();return this.events()}close(){this.toastBg.remove()}setText(text){this.input.message=text;this.toastText.innerHTML=text}createButton(name,text){this[name]=document.createElement("BUTTON");this[name].innerHTML=text;this[name].classList.add("standard-button");this.buttonLine.appendChild(this[name])}createButtons(){this.buttonLine=document.createElement("DIV");this.buttonLine.classList.add("button-line");this.toast.appendChild(this.buttonLine);if(this.input.ok){this.createButton('btnOk','OK')}if(this.input.cancel){this.createButton('btnCancel','Cancel')}if(this.input.custom){this.createButton('btnCustom',this.input.text)}if(this.input.close==true){this.closeX=document.createElement("i");this.closeX.classList.add("cross");this.buttonLine.appendChild(this.closeX)}}events(){if(this.input.decay){if(typeof this.input.decay==='number'){setTimeout(()=>this.close(),this.input.time*1000)}}let promise=new Promise((resolve,reject)=>{if(this.input.close==true){this.closeX.onclick=()=>this.close()}if(this.input.ok){this.btnOk.onclick=()=>{this.close();resolve("ok")}}if(this.input.cancel){this.btnCancel.onclick=()=>{this.close();resolve("cancel")}}if(this.input.custom){this.btnCustom.onclick=()=>{this.close();resolve("custom")}}if(!this.input.custom&&!this.input.ok&&!this.input.cancel){reject(Error("It broke"))}});return promise}}function popUpInfo(message){var p=new PopUp({"message":message,"ok":true,"cancel":false,"custom":false,"close":true,"icon":"📣"});p.create().then((result)=>{}).catch((err)=>console.log(err))}function popUpError(message){var p=new PopUp({"message":message,"ok":true,"cancel":false,"custom":false,"close":true,"icon":"🛑"});p.create().then((result)=>{}).catch((err)=>console.log(err))}function popUpConfirm(message){var p=new PopUp({"message":message,"ok":true,"cancel":true,"custom":false,"close":false,"icon":"❓"});return p.create()} \ No newline at end of file