From c52f8817b2780a9867547e9b04055096a7847255 Mon Sep 17 00:00:00 2001 From: Script47 Date: Fri, 26 Jun 2020 20:51:54 +0100 Subject: [PATCH] Positioning (#26) Introduces positioning for top-center and bottom-center. --- README.md | 2 +- dist/toast.min.css | 2 +- dist/toast.min.js | 12 ++++++------ example/index.html | 4 ++-- package-lock.json | 2 +- package.json | 2 +- src/css/toast.css | 14 +++++++++++++- src/js/toast.js | 4 ++-- 8 files changed, 27 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index c67b286..7cd371e 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Modify the global variables to apply specific rules/styles to all your toasts. ```javascript $.toastDefaults = { - position: 'top-right', /** top-left/top-right/bottom-left/bottom-right - Where the toast will show up **/ + position: 'top-right', /** top-left/top-right/top-center/bottom-left/bottom-right/bottom-center - Where the toast will show up **/ dismissible: true, /** true/false - If you want to show the button to dismiss the toast manually **/ stackable: true, /** true/false - If you want the toasts to be stackable **/ pauseDelayOnHover: true, /** true/false - If you want to pause the delay of toast when hovering over the toast **/ diff --git a/dist/toast.min.css b/dist/toast.min.css index a6fe570..819cea8 100644 --- a/dist/toast.min.css +++ b/dist/toast.min.css @@ -1 +1 @@ -.toast-container{position:fixed;z-index:1055;margin:5px}.top-right{top:0;right:0}.top-left{top:0;left:0}.bottom-right{right:0;bottom:0}.bottom-left{left:0;bottom:0}.toast-container>.toast{min-width:150px;background:0 0;border:none}.toast-container>.toast>.toast-header{border:none}.toast-container>.toast>.toast-header strong{padding-right:20px}.toast-container>.toast>.toast-body{background:#fff} +.toast-container{position:fixed;z-index:1055;margin:5px}.top-right{top:0;right:0}.top-left{top:0;left:0}.top-center{transform:translateX(-50%);top:0;left:50%}.bottom-right{right:0;bottom:0}.bottom-left{left:0;bottom:0}.bottom-center{transform:translateX(-50%);bottom:0;left:50%}.toast-container>.toast{min-width:150px;background:0 0;border:none}.toast-container>.toast>.toast-header{border:none}.toast-container>.toast>.toast-header strong{padding-right:20px}.toast-container>.toast>.toast-body{background:#fff} diff --git a/dist/toast.min.js b/dist/toast.min.js index 3e3c088..e47adde 100644 --- a/dist/toast.min.js +++ b/dist/toast.min.js @@ -1,6 +1,6 @@ -(function(a){function f(c){if(!a("#toast-container").length){var d=["top-right","top-left","bottom-right","bottom-left"].includes(a.toastDefaults.position)?a.toastDefaults.position:"top-right";a("body").prepend('
');a("#toast-container").addClass(d)}d=a("#toast-container");var b="",e=b="",g="toast-"+l,f=c.type,t=c.title,m=c.subtitle,n=c.content,h=c.img,p=c.delay?'data-delay="'+c.delay+'"':'data-autohide="false"', - q="",r=a.toastDefaults.dismissible,u=a.toastDefaults.style.toast,k=!1;"undefined"!==typeof c.dismissible&&(r=c.dismissible);switch(f){case "info":e=a.toastDefaults.style.info||"bg-info";b=a.toastDefaults.style.info||"text-white";break;case "success":e=a.toastDefaults.style.success||"bg-success";b=a.toastDefaults.style.info||"text-white";break;case "warning":e=a.toastDefaults.style.warning||"bg-warning";b=a.toastDefaults.style.warning||"text-white";break;case "error":e=a.toastDefaults.style.error|| - "bg-danger",b=a.toastDefaults.style.error||"text-white"}a.toastDefaults.pauseDelayOnHover&&c.delay&&(p='data-autohide="false"',q='data-hide-after="'+(Math.floor(Date.now()/1E3)+c.delay/1E3)+'"');b='";a.toastDefaults.stackable||d.find(".toast").each(function(){a(this).remove()});d.append(b);d.find(".toast:last").toast("show");a.toastDefaults.pauseDelayOnHover&& -(setTimeout(function(){k||a("#"+g).toast("hide")},c.delay),a("body").on("mouseover","#"+g,function(){k=!0}),a(document).on("mouseleave","#"+g,function(){var b=Math.floor(Date.now()/1E3),c=parseInt(a(this).data("hideAfter"));k=!1;b>=c&&a(this).toast("hide")}));l++}a.toastDefaults={position:"top-right",dismissible:!0,stackable:!0,pauseDelayOnHover:!0,style:{toast:"",info:"",success:"",warning:"",error:""}};a("body").on("hidden.bs.toast",".toast",function(){a(this).remove()});var l=1;a.snack=function(a, - d,b){return f({type:a,title:d,delay:b})};a.toast=function(a){return f(a)}})(jQuery); +(function(a){function f(c){if(!a("#toast-container").length){var d="top-right top-left top-center bottom-right bottom-left bottom-center".split(" ").includes(a.toastDefaults.position)?a.toastDefaults.position:"top-right";a("body").prepend('
');a("#toast-container").addClass(d)}d=a("#toast-container");var b="",e=b="",g="toast-"+l,f=c.type,t=c.title,m=c.subtitle,n=c.content,h=c.img,p=c.delay?'data-delay="'+ + c.delay+'"':'data-autohide="false"',q="",r=a.toastDefaults.dismissible,u=a.toastDefaults.style.toast,k=!1;"undefined"!==typeof c.dismissible&&(r=c.dismissible);switch(f){case "info":e=a.toastDefaults.style.info||"bg-info";b=a.toastDefaults.style.info||"text-white";break;case "success":e=a.toastDefaults.style.success||"bg-success";b=a.toastDefaults.style.info||"text-white";break;case "warning":e=a.toastDefaults.style.warning||"bg-warning";b=a.toastDefaults.style.warning||"text-white";break;case "error":e= + a.toastDefaults.style.error||"bg-danger",b=a.toastDefaults.style.error||"text-white"}a.toastDefaults.pauseDelayOnHover&&c.delay&&(p='data-autohide="false"',q='data-hide-after="'+(Math.floor(Date.now()/1E3)+c.delay/1E3)+'"');b='";a.toastDefaults.stackable||d.find(".toast").each(function(){a(this).remove()});d.append(b);d.find(".toast:last").toast("show"); + a.toastDefaults.pauseDelayOnHover&&(setTimeout(function(){k||a("#"+g).toast("hide")},c.delay),a("body").on("mouseover","#"+g,function(){k=!0}),a(document).on("mouseleave","#"+g,function(){var b=Math.floor(Date.now()/1E3),c=parseInt(a(this).data("hideAfter"));k=!1;b>=c&&a(this).toast("hide")}));l++}a.toastDefaults={position:"top-right",dismissible:!0,stackable:!0,pauseDelayOnHover:!0,style:{toast:"",info:"",success:"",warning:"",error:""}};a("body").on("hidden.bs.toast",".toast",function(){a(this).remove()}); + var l=1;a.snack=function(a,d,b){return f({type:a,title:d,delay:b})};a.toast=function(a){return f(a)}})(jQuery); diff --git a/example/index.html b/example/index.html index 3464f0c..56b217f 100644 --- a/example/index.html +++ b/example/index.html @@ -163,9 +163,9 @@

A jQuery plugin for Bootstrap 4.2+

'warning': 'It\'s all about to go wrong', 'error': 'It all went wrong.' }, - POSITION = ['top-right', 'top-left', 'bottom-right', 'bottom-left']; + POSITION = ['top-right', 'top-left', 'top-center', 'bottom-right', 'bottom-left', 'bottom-center']; - $.toastDefaults.position = POSITION[Math.floor(Math.random() * POSITION.length)]; + $.toastDefaults.position = 'bottom-center'; $.toastDefaults.dismissible = true; $.toastDefaults.stackable = true; $.toastDefaults.pauseDelayOnHover = true; diff --git a/package-lock.json b/package-lock.json index 12be420..b25aece 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "bs4-toast", - "version": "1.0.0", + "version": "1.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index fe542f5..a31aea7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bs4-toast", - "version": "1.0.0", + "version": "1.1.0", "description": "Toast - A Bootstrap 4.2+ jQuery plugin for the toast component", "main": "dist/toast.min.js", "repository": { diff --git a/src/css/toast.css b/src/css/toast.css index bba7a20..d5ed583 100644 --- a/src/css/toast.css +++ b/src/css/toast.css @@ -1,7 +1,7 @@ /** * @author Script47 (https://github.com/Script47/Toast) * @description Toast - A Bootstrap 4.2+ jQuery plugin for the toast component - * @version 1.0.0 + * @version 1.1.0 **/ .toast-container { position: fixed; @@ -19,6 +19,12 @@ left: 0; } +.top-center { + transform: translateX(-50%); + top: 0; + left: 50%; +} + .bottom-right { right: 0; bottom: 0; @@ -29,6 +35,12 @@ bottom: 0; } +.bottom-center { + transform: translateX(-50%); + bottom: 0; + left: 50%; +} + .toast-container > .toast { min-width: 150px; background: transparent; diff --git a/src/js/toast.js b/src/js/toast.js index 97b23e7..c8ae2e8 100644 --- a/src/js/toast.js +++ b/src/js/toast.js @@ -1,7 +1,7 @@ /** * @author Script47 (https://github.com/Script47/Toast) * @description Toast - A Bootstrap 4.2+ jQuery plugin for the toast component - * @version 1.0.0 + * @version 1.1.0 **/ (function ($) { const TOAST_CONTAINER_HTML = `
`; @@ -29,7 +29,7 @@ function render(opts) { /** No container, create our own **/ if (!$('#toast-container').length) { - const position = ['top-right', 'top-left', 'bottom-right', 'bottom-left'].includes($.toastDefaults.position) ? $.toastDefaults.position : 'top-right'; + const position = ['top-right', 'top-left', 'top-center', 'bottom-right', 'bottom-left', 'bottom-center'].includes($.toastDefaults.position) ? $.toastDefaults.position : 'top-right'; $('body').prepend(TOAST_CONTAINER_HTML); $('#toast-container').addClass(position);